Implement singleton for player and portal
Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
parent
a092dee1e5
commit
ba5bb7b090
@ -60,7 +60,6 @@ public class Scene extends JComponent implements Constants {
|
||||
/**
|
||||
* The cells of the game
|
||||
*/
|
||||
//private final Cell[][] cells;
|
||||
private final CopyOnWriteArrayList<CopyOnWriteArrayList<Cell>> cells;
|
||||
/**
|
||||
* The logger
|
||||
@ -89,7 +88,6 @@ public class Scene extends JComponent implements Constants {
|
||||
this.canvas = canvas;
|
||||
loadTextures();
|
||||
|
||||
//cells = new Cell[HORIZONTAL_CELLS][VERTICAL_CELLS];
|
||||
cells = new CopyOnWriteArrayList<>();
|
||||
|
||||
if (GENERATE_SCENE) {
|
||||
@ -137,7 +135,7 @@ public class Scene extends JComponent implements Constants {
|
||||
cells.get(x).add(cell);
|
||||
|
||||
if (jsonCells[x][y].type.equals(Player.class.getName())) {
|
||||
cells.get(x).get(y).setObject(new Player(null, cells.get(x).get(y)));
|
||||
cells.get(x).get(y).setObject(Player.getInstance(null, cells.get(x).get(y)));
|
||||
}
|
||||
else if (jsonCells[x][y].type.equals(Enemy.class.getName())) {
|
||||
cells.get(x).get(y).setObject(new Enemy(null, cells.get(x).get(y), null));
|
||||
@ -155,7 +153,7 @@ public class Scene extends JComponent implements Constants {
|
||||
cells.get(x).get(y).setObject(new Obstacle(null, cells.get(x).get(y)));
|
||||
}
|
||||
else if (jsonCells[x][y].type.equals(Portal.class.getName())) {
|
||||
cells.get(x).get(y).setObject(new Portal(null, cells.get(x).get(y)));
|
||||
cells.get(x).get(y).setObject(Portal.getInstance(null, cells.get(x).get(y)));
|
||||
}
|
||||
|
||||
for (int k = 0; k < jsonCells[x][y].textures.size(); k++) {
|
||||
@ -180,7 +178,7 @@ public class Scene extends JComponent implements Constants {
|
||||
List<Object> objectArrayList = new ArrayList<>();
|
||||
|
||||
// The player has a fixed position
|
||||
cells.get(2).get(1).setObject(new Player(this, cells.get(2).get(1)));
|
||||
cells.get(2).get(1).setObject(Player.getInstance(this, cells.get(2).get(1)));
|
||||
objectArrayList.add(cells.get(2).get(1).getObject());
|
||||
|
||||
for (int i = 0; i < OBSTACLES; i++) {
|
||||
@ -203,7 +201,7 @@ public class Scene extends JComponent implements Constants {
|
||||
}
|
||||
|
||||
random = randomCoordinates();
|
||||
cells.get(random[0]).get(random[1]).setObjectOnBottom(new Portal(this, cells.get(random[0]).get(random[1])));
|
||||
cells.get(random[0]).get(random[1]).setObjectOnBottom(Portal.getInstance(this, cells.get(random[0]).get(random[1])));
|
||||
objectArrayList.add(cells.get(random[0]).get(random[1]).getObjectOnBottom());
|
||||
|
||||
// Generate enough keys for the chests that will exist
|
||||
|
@ -36,6 +36,10 @@ public class Player extends Object implements Constants {
|
||||
* The maximum health of the player
|
||||
*/
|
||||
public final static int MAX_HEALTH = 20;
|
||||
/**
|
||||
* There can be only 1 player
|
||||
*/
|
||||
private static Player instance = null;
|
||||
/**
|
||||
* Objects that the player is carrying
|
||||
*/
|
||||
@ -55,7 +59,7 @@ public class Player extends Object implements Constants {
|
||||
* @param scene The scene the player is in
|
||||
* @param cell The cell the player is in
|
||||
*/
|
||||
public Player(Scene scene, Cell cell) {
|
||||
private Player(Scene scene, Cell cell) {
|
||||
super(scene, cell);
|
||||
setLogger(getLogger(this.getClass(), LogLevel.PLAYER));
|
||||
loadPlayerAnimation();
|
||||
@ -72,6 +76,24 @@ public class Player extends Object implements Constants {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a unique instance of the player
|
||||
*
|
||||
* @param scene The scene the player is in
|
||||
* @param cell The cell the player is in
|
||||
* @return Returns the Player instance
|
||||
*/
|
||||
public static Player getInstance(Scene scene, Cell cell) {
|
||||
if (instance == null) {
|
||||
synchronized (Player.class) {
|
||||
if (instance == null) {
|
||||
instance = new Player(scene, cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the player animation
|
||||
*/
|
||||
|
@ -29,6 +29,10 @@ import java.util.List;
|
||||
* This class handles the portal functionality
|
||||
*/
|
||||
public class Portal extends Object implements Constants {
|
||||
/**
|
||||
* There can be only 1 portal
|
||||
*/
|
||||
private static Portal instance = null;
|
||||
/**
|
||||
* The current state of the portal
|
||||
*/
|
||||
@ -52,12 +56,30 @@ public class Portal extends Object implements Constants {
|
||||
* @param scene The scene that contains the portal
|
||||
* @param cell The cell the portal is in
|
||||
*/
|
||||
public Portal(Scene scene, Cell cell) {
|
||||
private Portal(Scene scene, Cell cell) {
|
||||
super(scene, cell);
|
||||
setLogger(getLogger(this.getClass(), LogLevel.PORTAL));
|
||||
loadPortalAnimations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a unique instance of the portal
|
||||
*
|
||||
* @param scene The scene the portal is in
|
||||
* @param cell The cell the portal is in
|
||||
* @return Returns the Portal instance
|
||||
*/
|
||||
public static Portal getInstance(Scene scene, Cell cell) {
|
||||
if (instance == null) {
|
||||
synchronized (Portal.class) {
|
||||
if (instance == null) {
|
||||
instance = new Portal(scene, cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the portal animation
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user