⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wallcontainer.java

📁 j2me的手机源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.lang.*;
import com.mascotcapsule.micro3d.v3.*;
import java.util.*;

/*
 * This class builds road with triangle floor and side borders.
 * Only map has full information no the whole road.
 * Only curently visible part of road is actually created.
 * Road is constantly updated according to car movement and map.
 * First map element is always element 0.
 * Information kept in map is actualy a connection list:
 * - which road unit type should be connected to the current unit 
 *   (according to GetNextUnitType function)
 * - how it should be positioned in relation to the current unit (according to 
 *   GetNextUnitPos function)
 *
 * Map keeps connection, i.e: map[i] == 1 means that we have unit type 1 to
 * which unit type 0 is connected. 
 * Unit type 0 should be moved KUnitDistance on Y in reletion to unit type 1.
*/
public class WallContainer
{
  private Effect3D refEffect;       // reference to effect that should be used

  // triangle's tables references
  int[] refTriNorm = RoadDef.mTriNorm;      // normal vectors
  int[] refTriTex = RoadDef.mTriTex;        // texture coordinates
  // side border's tables references
  int[] refSideNorm = RoadDef.mSideNorm;    // normal vectors
  int[] refSideTex = RoadDef.mSideTex;      // texture coordinates

  private final int COLOR[] = {0x000000};

  // temporary variable, used in calculations
  private AffineTrans tmpAT = new AffineTrans();

////////////////////////////////////////////////////////////////////////////////
// common definition for all roads' triangles
//
  private Texture textureTab;
  private final int COMMAND_TRI = Graphics3D.PDATA_NORMAL_NONE | 
                                  Graphics3D.PDATA_TEXURE_COORD | 
                                  Graphics3D.PRIMITVE_TRIANGLES;

///////////////////////////////////////////////////////////////////////////////
// common definition for all road borders' sides
//
  private Texture textureSide;
  protected static final int COMMAND_SIDE = Graphics3D.PRIMITVE_TRIANGLES | 
                                            Graphics3D.PDATA_NORMAL_PER_VERTEX |
                                            Graphics3D.PDATA_TEXURE_COORD | 
                                            Graphics3D.PDATA_COLOR_NONE;

///////////////////////////////////////////////////////////////////////////////
// members

  // exact distance between folowing road unints
  private final static int KUnitDistance = 2*RoadDef.KSize;
  // screen radius - used to determine when road unit is not visible
  private static int KOutOfScreen = 0;
  // stack to store road units
  private Stack road = new Stack();
  // number of elements in currently created map
  private int mapSize = 0;
  // map array holding information about the order of road units' creation.
  private int[] map;

///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//                             FUNCTIONS                                     //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

  // get no of road units
  public int getRoadCount()          { return road.size(); }
  // get road unit at given index
  public RoadUnit getUnit(int aIter) { return (RoadUnit)road.elementAt(aIter); }

  public WallContainer(int aWidth, int aHeight, AffineTrans aCamTrans, Effect3D aEffect)
  {
    refEffect = aEffect;
    KOutOfScreen = RoadDef.KOutScreen;
    try {
      textureTab = new Texture("/res/wall_down.bmp", true);
      textureSide = new Texture("/res/wall_side.bmp", true);
      // load map of the road
      loadMap();
      // build initial visible part of the road
      buildRoadOnConstruct(aWidth, aHeight, aCamTrans);
    }
    catch(Exception e) {
      System.out.println("Failed to create Road");
    }
    System.out.println("Road created.");
  }

  // car shouldn't leave track at the begining of the track
  public boolean isOutOnStart(int aCarY)
  {
    RoadUnit unit = (RoadUnit)road.firstElement();
    if( unit.getMapPosition() == -1 && aCarY < unit.triangle.move.m13 )
      return true;
    // did not return before, so is not out of track at its begin.
    return false;
  }

  // car shouldn't leave track at the end of the track
  public boolean isOutOnFinish(int aCarY)
  {
    RoadUnit unit = (RoadUnit)road.lastElement();
    if( unit.getMapPosition() == mapSize-1 && aCarY >= unit.triangle.move.m13 )
      return true;
    // did not return before, so is not out of track at its finish.
    return false;
  }

  // draw road init (triangle floor and side border)
  public void render(Graphics3D g3d)
  {
    RoadUnit unit;
    int elemNo = road.size();
    for( int i=0; i<elemNo; ++i )
    {
      unit = (RoadUnit)road.elementAt(i);
      g3d.renderPrimitives(textureTab, 0, 0, unit.triangle.getLayout(), refEffect,
                           COMMAND_TRI, 1, unit.refVertTri, refTriNorm, refTriTex,
                           COLOR);
      g3d.renderPrimitives(textureSide, 0, 0, unit.side.getLayout(), refEffect,
                           COMMAND_SIDE, 6, unit.refVertSide, refSideNorm, refSideTex,
                           COLOR);
    }
  }

  public void updateCamPosition(AffineTrans aCamTrans)
  {
    RoadUnit unit;
    int elemNo = road.size();
    for( int i=0; i<elemNo; ++i )
    {
      unit = (RoadUnit)road.elementAt(i);
      unit.updateCamPosition(aCamTrans, tmpAT);
    }
  }

  private void buildFirstRoadUnit(int aWidth, int aHeight, AffineTrans aCamTrans)
  {
    RoadUnit unit;
    unit = new RoadUnit( 0, -1, 0, RoadDef.mGameInitY, aWidth, aHeight, aCamTrans, tmpAT);
    road.insertElementAt(unit, 0);
  }

  private void buildRoadOnConstruct(int aWidth, int aHeight, AffineTrans aCamTrans)
  {
    int[] pos = new int[2];
    pos[0] = 0;
    pos[1] = RoadDef.mGameInitY;
    int conNo = 0;
    int unitType = 0;
    // build road
    RoadUnit unit;
    buildFirstRoadUnit(aWidth, aHeight, aCamTrans);

    for( int i=0; i<mapSize; ++i )
    {
      conNo = map[i];
      GetNextUnitPos(conNo, pos);
      if( !isVisible(0, RoadDef.mGameInitY, pos, KOutOfScreen))
        break;
      unitType = GetNextUnitType(conNo);
      unit = new RoadUnit(unitType, i, pos[0], pos[1], aWidth, aHeight, aCamTrans, tmpAT);
      int tmp = i+1;
      road.addElement(unit);
    }
  }

  // this function updates road while car is moving so only visible road units
  // are actually created and taken into account when drawing or colliding.
  private int nextPos[] = new int[2];
  private int[] dif = new int[2];
  public void updateRoad(boolean aMoveForward, int aCarPosX, int aCarPosY,
                         int aWidth, int aHeight, AffineTrans aCamTrans)
  {
    RoadUnit unit;
    int mapPos = 0;
    int conNo = 0;
    int unitType = 0;
    if(aMoveForward)
    {
      // Check if road unit going from begining of road stack becomes invisible,
      // if it does - remove it.
      // Than retry again until stack is empty or there is a visible unit found.
      while(road.size() > 0)
      {
        unit = (RoadUnit)road.firstElement();
        if( !unit.isVisible(aCarPosX, aCarPosY, KOutOfScreen) ) {
          road.removeElementAt(0);
        }
        else
          break;
      }
      // Check if any new road unit at the end of stack becomes visible.
      // Add it on the end if it does.
      // Retry until finding invisible unit or no more elements.
      while(true)
      {
        try {
          unit = (RoadUnit)road.lastElement();
        }
        catch (Exception e) {
          break;
        }
        mapPos = unit.getMapPosition();
        // safety check -> if this is first road unit element (mapPosit == -1)
        if(mapPos == -1)
          mapPos = 0;
        ++mapPos;
        if( mapPos >= mapSize )
          break;
        conNo = map[mapPos];
        nextPos[0] = unit.triangle.move.m03;
        nextPos[1] = unit.triangle.move.m13;
        GetNextUnitPos( conNo, nextPos);
        if(isVisible(aCarPosX, aCarPosY, nextPos, KOutOfScreen) )
        {
          unitType = GetNextUnitType(conNo);
          unit = new RoadUnit(unitType, mapPos, nextPos[0], nextPos[1], 
                              aWidth, aHeight, aCamTrans, tmpAT);
          road.addElement(unit);
        }
        else {
          break;
        }
      }
    } // END if(aMoveForward)
    else 
    {
      // Check if road unit from the end of road stack becomes invisible,
      // if it does - remove it.
      // Than retry again until stack is empty or visible unit id found.
      while(road.size() > 0)
      {
        unit = (RoadUnit)road.lastElement();
        if( !unit.isVisible(aCarPosX, aCarPosY, KOutOfScreen) )
          road.pop();
        else
          break;
      }
      // Check if any new road unit at the start of stack becomes visible.
      // Add it on at begining if it does.
      // Retry until found invisible or no more elements.
      while(true)
      {
        try {
          unit = (RoadUnit)road.firstElement();
        }
        catch (Exception e) {
          break;
        }
        mapPos = unit.getMapPosition();
        // safety check -> if it is first road unit element (mapPosit == -1)
        // than no new one can be put in front
        if(mapPos == -1)
          break;
        conNo = map[mapPos];
        // get the distance that this unit was moved with in relation to its previous unit
        dif[0] = 0;
        dif[1] = 0;
        GetNextUnitPos(conNo, dif);
        // the previous unit gets its position by moving backwards
        nextPos[0] = unit.triangle.move.m03 - dif[0];
        nextPos[1] = unit.triangle.move.m13 - dif[1];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -