Fix restart bug caused by unique instances of player and portal objects

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2020-02-25 14:05:33 -03:00
parent e820ad1a79
commit 21f54beca5
3 changed files with 9 additions and 53 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019 Chris Cromer * Copyright 2020 Chris Cromer
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* *
@ -135,7 +135,7 @@ public class Scene extends JComponent implements Constants {
cells.get(x).add(cell); cells.get(x).add(cell);
if (jsonCells[x][y].type.equals(Player.class.getName())) { if (jsonCells[x][y].type.equals(Player.class.getName())) {
cells.get(x).get(y).setObject(Player.getInstance(null, cells.get(x).get(y))); cells.get(x).get(y).setObject(new Player(null, cells.get(x).get(y)));
} }
else if (jsonCells[x][y].type.equals(Enemy.class.getName())) { 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)); cells.get(x).get(y).setObject(new Enemy(null, cells.get(x).get(y), null));
@ -153,7 +153,7 @@ public class Scene extends JComponent implements Constants {
cells.get(x).get(y).setObject(new Obstacle(null, cells.get(x).get(y))); cells.get(x).get(y).setObject(new Obstacle(null, cells.get(x).get(y)));
} }
else if (jsonCells[x][y].type.equals(Portal.class.getName())) { else if (jsonCells[x][y].type.equals(Portal.class.getName())) {
cells.get(x).get(y).setObject(Portal.getInstance(null, cells.get(x).get(y))); cells.get(x).get(y).setObject(new Portal(null, cells.get(x).get(y)));
} }
for (int k = 0; k < jsonCells[x][y].textures.size(); k++) { for (int k = 0; k < jsonCells[x][y].textures.size(); k++) {
@ -178,7 +178,7 @@ public class Scene extends JComponent implements Constants {
List<Object> objectArrayList = new ArrayList<>(); List<Object> objectArrayList = new ArrayList<>();
// The player has a fixed position // The player has a fixed position
cells.get(2).get(1).setObject(Player.getInstance(this, cells.get(2).get(1))); cells.get(2).get(1).setObject(new Player(this, cells.get(2).get(1)));
objectArrayList.add(cells.get(2).get(1).getObject()); objectArrayList.add(cells.get(2).get(1).getObject());
for (int i = 0; i < OBSTACLES; i++) { for (int i = 0; i < OBSTACLES; i++) {
@ -201,7 +201,7 @@ public class Scene extends JComponent implements Constants {
} }
random = randomCoordinates(); random = randomCoordinates();
cells.get(random[0]).get(random[1]).setObjectOnBottom(Portal.getInstance(this, cells.get(random[0]).get(random[1]))); cells.get(random[0]).get(random[1]).setObjectOnBottom(new Portal(this, cells.get(random[0]).get(random[1])));
objectArrayList.add(cells.get(random[0]).get(random[1]).getObjectOnBottom()); objectArrayList.add(cells.get(random[0]).get(random[1]).getObjectOnBottom());
// Generate enough keys for the chests that will exist // Generate enough keys for the chests that will exist

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019 Chris Cromer * Copyright 2020 Chris Cromer
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* *
@ -36,10 +36,6 @@ public class Player extends Object implements Constants {
* The maximum health of the player * The maximum health of the player
*/ */
public final static int MAX_HEALTH = 20; public final static int MAX_HEALTH = 20;
/**
* There can be only 1 player
*/
private static Player instance = null;
/** /**
* Objects that the player is carrying * Objects that the player is carrying
*/ */
@ -59,7 +55,7 @@ public class Player extends Object implements Constants {
* @param scene The scene the player is in * @param scene The scene the player is in
* @param cell The cell the player is in * @param cell The cell the player is in
*/ */
private Player(Scene scene, Cell cell) { public Player(Scene scene, Cell cell) {
super(scene, cell); super(scene, cell);
setLogger(getLogger(this.getClass(), LogLevel.PLAYER)); setLogger(getLogger(this.getClass(), LogLevel.PLAYER));
loadPlayerAnimation(); loadPlayerAnimation();
@ -76,24 +72,6 @@ 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 * Load the player animation
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019 Chris Cromer * Copyright 2020 Chris Cromer
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* *
@ -29,10 +29,6 @@ import java.util.List;
* This class handles the portal functionality * This class handles the portal functionality
*/ */
public class Portal extends Object implements Constants { public class Portal extends Object implements Constants {
/**
* There can be only 1 portal
*/
private static Portal instance = null;
/** /**
* The current state of the portal * The current state of the portal
*/ */
@ -56,30 +52,12 @@ public class Portal extends Object implements Constants {
* @param scene The scene that contains the portal * @param scene The scene that contains the portal
* @param cell The cell the portal is in * @param cell The cell the portal is in
*/ */
private Portal(Scene scene, Cell cell) { public Portal(Scene scene, Cell cell) {
super(scene, cell); super(scene, cell);
setLogger(getLogger(this.getClass(), LogLevel.PORTAL)); setLogger(getLogger(this.getClass(), LogLevel.PORTAL));
loadPortalAnimations(); 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 * Load the portal animation
*/ */