Länkad lista.
Hej igen!
Nu har jag snickrat ihop en länkad lista för att hålla reda på labyrintens alla sektioner. Tanken är att man ska placera spelaren vid någon utav sektionerna, och testa så att spelaren håller sig inom sektionens begränsningar. När spelaren åker till nästa sektion så rör sig det lokala ljuset med spelaren och sektioner långt bakom/framför/under/över/sidan visas inte, precis som jag nämnde i förra blogginlägget.
När spelaren kommer in i en speciell typ av sektion så kommer det lokala ljuset lysa upp mer ett tag så att flera sektioner runt omkring lyser upp, vilket är vad andra spelare som är hyfsat i närheten kommer att se.

Skapar först ett MazeMaker objekt.
Här är koden för det..
public class MazeMaker
{
private static final int cSCALE = 5;
private MazeSection mMazeMapStart = null;
// Create the DEMO maze in the constructor.
public MazeMaker (float mazeStartPosX, float mazeStartPosY, float mazeStartPosZ)
{
// First create a demo maze.
// Start position in maze, the first section..
this.mMazeMapStart = new MazeSection();
this.mMazeMapStart.setPosX(mazeStartPosX);
this.mMazeMapStart.setPosY(mazeStartPosY);
this.mMazeMapStart.setPosZ(mazeStartPosZ);
this.mMazeMapStart.setScale(cSCALE * 2f);
this.mMazeMapStart.setType(0); // Forward.
// Set this first section as the parent.
MazeSection parent = this.mMazeMapStart;
// Create a corridor forward, 9 extra blocks..
for (int i=1;i<10;i++)
{
// Create a section..
// In front of the previous one.
MazeSection forward = new MazeSection();
forward.setPosX(mazeStartPosX);
forward.setPosY(mazeStartPosY);
forward.setPosZ(mazeStartPosZ + (cSCALE * 2 * i)); // Forward * a section size.
forward.setScale(cSCALE * 2f);
forward.setType(0); // Forward type.
// Set new section as forward for parent.
parent.setForwardSection(forward);
// Set the previous section
// as the backward section for the new one.
forward.setBackwardSection(parent);
// And the new forward section
// as the new parent before continue.
parent = forward; // parent.getForwardSection ();
}
// Now add a downturn section,
// before we start a downward corridor.
MazeSection dowTurn = new MazeSection();
dowTurn.setPosX(mazeStartPosX);
dowTurn.setPosY(mazeStartPosY);
dowTurn.setPosZ(mazeStartPosZ + (cSCALE * 2 * 10));
dowTurn.setScale(cSCALE * 2f);
dowTurn.setType(1); // Downturn from forward type.
dowTurn.setBackwardSection(parent); // Set the previous as backward section.
parent.setForwardSection(dowTurn);
parent = dowTurn; // This new section as parent.
// Create a downward corridor
// made of 9 sections..
for (int i=1;i<10;i++)
{
MazeSection down = new MazeSection();
down.setPosX(mazeStartPosX);
down.setPosY(mazeStartPosY + (-(cSCALE * 2 * i)));
down.setPosZ(mazeStartPosZ + (cSCALE * 2 * 10));
down.setScale(cSCALE * 2f);
down.setType(2); // Down section, from forward downturn type.
down.setUpSection(parent); // Same again, make previous up.
parent.setDownSection(down);
parent = down; // New section as parent, before loop.
}
}
}
MazeMaker objektet håller i sin tur ordning på alla sektioner.
Här är objekt-koden MazeMaker för det..
public class MazeSection
{
private float mScale = 0;
private float mDistanceToCamera = -1;
private int mIsVisible = 1;
private int mSectionType = -1;
private MazeSection mUp = null;
private MazeSection mDown = null;
private MazeSection mForward = null;
private MazeSection mBackward = null;
private MazeSection mRight = null;
private MazeSection mLeft = null;
private float mPosX = 0;
private float mPosY = 0;
private float mPosZ = 0;
public void setScale (float scale) { this.mScale = scale; }
public float getScale () { return this.mScale; }
public void setDistanceToCamera (float camPosX, float camPosY, float camPosZ)
{
// Calculate distance to camera.
float xL = this.mPosX - camPosX;
float yL = this.mPosY - camPosY;
float zL = this.mPosZ - camPosZ;
// Pythagora's thingy..
this.mDistanceToCamera = (float)Math.sqrt((double)(xL * xL + yL * yL + zL * zL));
}
public float getDistanceToCamera () { return this.mDistanceToCamera; }
public boolean isVisible () { if (this.mIsVisible == 1) return true; else return false; }
public void setVisibleState (int visibleState) { this.mIsVisible = visibleState; }
public int getType () { return this.mSectionType; }
public void setType (int sectionType) { this.mSectionType = sectionType; }
public MazeSection getUpSection () { return this.mUp; }
public MazeSection getForwardSection () { return this.mForward; }
public MazeSection getDownSection () { return this.mDown; }
public MazeSection getBackwardSection () { return this.mBackward; }
public MazeSection getLeftSection () { return this.mLeft; }
public MazeSection getRightSection () { return this.mRight; }
public void setUpSection (MazeSection upSection) { this.mUp = upSection; }
public void setForwardSection (MazeSection forwardSection) { this.mForward = forwardSection; }
public void setDownSection (MazeSection downSection) { this.mDown = downSection; }
public void setBackwardSection (MazeSection backwardSection) { this.mBackward = backwardSection; }
public void setLeftSection (MazeSection leftSection) { this.mLeft = leftSection; }
public void setRightSection (MazeSection rightSection) { this.mRight = rightSection; }
public float getPosX () { return this.mPosX; }
public float getPosY () { return this.mPosY; }
public float getPosZ () { return this.mPosZ; }
public void setPosX (float x) { this.mPosX = x; }
public void setPosY (float y) { this.mPosY = y; }
public void setPosZ (float z) { this.mPosZ = z; }
}
Fråga mig gärna varför jag använder for-loop istället för rekursiva funktionsanrop 😉
I nästa blogginlägg ska jag presentera resultatet utav detta i en liten film, håll tillgodo så hör vi.
Kommentarer
Skicka en kommentar