1b: Factory handles obj creation

This commit is contained in:
Daniel Langbein 2024-11-28 18:29:28 +01:00
parent fd9fd294db
commit 4fd87e2c51
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
12 changed files with 109 additions and 114 deletions

View File

@ -10,12 +10,4 @@ package fr.enssat.BoulderDash.models.displayableElement;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class BoulderModel extends DisplayableElementModel { public class BoulderModel extends DisplayableElementModel {
BoulderModel(boolean convertible) {
super("boulder", false,true);
setPriority(2);
setCollideSound("die");
setConvertibleValue(convertible);
loadSprite("boulder");
}
} }

View File

@ -10,11 +10,4 @@ package fr.enssat.BoulderDash.models.displayableElement;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class BrickWallModel extends DisplayableElementModel { public class BrickWallModel extends DisplayableElementModel {
BrickWallModel() {
super("brickwall", true, false);
setPriority(3);
setCollideSound("touch");
loadSprite("brickwall");
}
} }

View File

@ -10,8 +10,4 @@ package fr.enssat.BoulderDash.models.displayableElement;
* @since 2015-06-22 * @since 2015-06-22
*/ */
public class CursorModel extends DisplayableElementModel { public class CursorModel extends DisplayableElementModel {
CursorModel() {
super("cursor");
this.loadSprite("cursor");
}
} }

View File

@ -22,13 +22,6 @@ public class DiamondModel extends DisplayableElementModel {
private ArrayList<BufferedImage> framesDiamond; private ArrayList<BufferedImage> framesDiamond;
DiamondModel() {
super("diamond", true, true);
setCollideSound("coin");
this.initSprites();
}
/** /**
* Updates the sprite (animation loop) * Updates the sprite (animation loop)
* *
@ -52,7 +45,7 @@ public class DiamondModel extends DisplayableElementModel {
* Initialize the sprites * Initialize the sprites
* This is an animated element, hence this method * This is an animated element, hence this method
*/ */
private void initSprites() { /* package-private */ void initSprites() {
/* Initialize object sprites */ /* Initialize object sprites */
this.framesDiamond = new ArrayList<BufferedImage>(); this.framesDiamond = new ArrayList<BufferedImage>();

View File

@ -10,9 +10,4 @@ package fr.enssat.BoulderDash.models.displayableElement;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class DirtModel extends DisplayableElementModel { public class DirtModel extends DisplayableElementModel {
DirtModel() {
super("dirt", true, false);
this.loadSprite("dirt");
}
} }

View File

@ -15,9 +15,9 @@ import java.io.IOException;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public abstract class DisplayableElementModel { public abstract class DisplayableElementModel {
protected final String spriteName; /* package-private */ String spriteName = null;
private final boolean destructible; private boolean destructible = false;
private final boolean moving; private boolean moving = false;
private int priority = 0; private int priority = 0;
private boolean falling = false; private boolean falling = false;
@ -26,59 +26,123 @@ public abstract class DisplayableElementModel {
private BufferedImage sprite; private BufferedImage sprite;
public DisplayableElementModel(final String spriteName, boolean destructible, boolean moving){
this.spriteName = spriteName;
this.destructible = destructible;
this.moving = moving;
}
public DisplayableElementModel(final String spriteName){
this(spriteName, false, false);
}
public static BoulderModel newBoulderModel(boolean convertible) { public static BoulderModel newBoulderModel(boolean convertible) {
return new BoulderModel(convertible); BoulderModel bm = new BoulderModel();
bm.setSpriteName("boulder");
bm.setMoving(true);
bm.setPriority(2);
bm.setCollideSound("die");
bm.setConvertibleValue(convertible);
bm.loadSprite("boulder");
return bm;
} }
public static BrickWallModel newBrickWallModel() { public static BrickWallModel newBrickWallModel() {
return new BrickWallModel(); BrickWallModel bwm = new BrickWallModel();
bwm.setSpriteName("brickwall");
bwm.setDestructible(true);
bwm.setPriority(3);
bwm.setCollideSound("touch");
bwm.loadSprite("brickwall");
return bwm;
} }
public static CursorModel newCursorModel() { public static CursorModel newCursorModel() {
return new CursorModel(); CursorModel cm = new CursorModel();
cm.setSpriteName("cursor");
cm.loadSprite("cursor");
return cm;
} }
public static DiamondModel newDiamondModel() { public static DiamondModel newDiamondModel() {
return new DiamondModel(); DiamondModel dm = new DiamondModel();
dm.setSpriteName("diamond");
dm.setDestructible(true);
dm.setMoving(true);
dm.setCollideSound("coin");
dm.initSprites();
return dm;
} }
public static DirtModel newDirtModel() { public static DirtModel newDirtModel() {
return new DirtModel(); DirtModel dm = new DirtModel();
dm.setSpriteName("dirt");
dm.setDestructible(true);
dm.loadSprite("dirt");
return dm;
} }
public static DoorModel newDoorModel() { public static DoorModel newDoorModel() {
return new DoorModel(); DoorModel dm = new DoorModel();
dm.setSpriteName("door");
dm.loadSprite("door");
return dm;
} }
public static EmptyModel newEmptyModel() { public static EmptyModel newEmptyModel() {
return new EmptyModel(); EmptyModel em = new EmptyModel();
em.setSpriteName("black");
em.loadSprite("black");
return em;
} }
public static ExpandingWallModel newExpandingWallModel() { public static ExpandingWallModel newExpandingWallModel() {
return new ExpandingWallModel(); ExpandingWallModel ewm = new ExpandingWallModel();
ewm.setSpriteName("expandingwall");
ewm.setPriority(10);
ewm.loadSprite("expandingwall");
return ewm;
} }
public static MagicWallModel newMagicWallModel() { public static MagicWallModel newMagicWallModel() {
return new MagicWallModel(); MagicWallModel mwm = new MagicWallModel();
mwm.setSpriteName("magicwall");
mwm.setPriority(3);
mwm.setCollideSound("touch");
mwm.initSprites();
return mwm;
} }
public static RockfordModel newRockfordModel() { public static RockfordModel newRockfordModel() {
return new RockfordModel(); RockfordModel rm = new RockfordModel();
rm.setSpriteName("rockford");
rm.setDestructible(true);
rm.setMoving(true);
rm.setPriority(1);
rm.initSprites();
return rm;
} }
public static SteelWallModel newSteelWallModel() { public static SteelWallModel newSteelWallModel() {
return new SteelWallModel(); SteelWallModel swm = new SteelWallModel();
swm.setSpriteName("steelwall");
swm.setPriority(3);
swm.setCollideSound("touch");
swm.loadSprite("steelwall");
return swm;
} }
/** /**
@ -90,6 +154,10 @@ public abstract class DisplayableElementModel {
return this.destructible; return this.destructible;
} }
public void setDestructible(boolean destructible) {
this.destructible = destructible;
}
/** /**
* Gets the 'moving' value * Gets the 'moving' value
* *
@ -99,6 +167,10 @@ public abstract class DisplayableElementModel {
return this.moving; return this.moving;
} }
public void setMoving(boolean moving) {
this.moving = moving;
}
/** /**
* Gets the sprite name value * Gets the sprite name value
* *
@ -108,6 +180,10 @@ public abstract class DisplayableElementModel {
return this.spriteName; return this.spriteName;
} }
public void setSpriteName(String spriteName) {
this.spriteName = spriteName;
}
/** /**
* Gets the folder path of the sprite storage * Gets the folder path of the sprite storage
* *

View File

@ -10,8 +10,4 @@ package fr.enssat.BoulderDash.models.displayableElement;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class DoorModel extends DisplayableElementModel { public class DoorModel extends DisplayableElementModel {
DoorModel() {
super("door");
this.loadSprite("door");
}
} }

View File

@ -10,8 +10,4 @@ package fr.enssat.BoulderDash.models.displayableElement;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class EmptyModel extends DisplayableElementModel { public class EmptyModel extends DisplayableElementModel {
EmptyModel() {
super("black");
this.loadSprite("black");
}
} }

View File

@ -10,10 +10,4 @@ package fr.enssat.BoulderDash.models.displayableElement;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class ExpandingWallModel extends DisplayableElementModel { public class ExpandingWallModel extends DisplayableElementModel {
ExpandingWallModel() {
super("expandingwall");
setPriority(10);
this.loadSprite("expandingwall");
}
} }

View File

@ -20,24 +20,14 @@ public class MagicWallModel extends DisplayableElementModel {
private ArrayList<BufferedImage> framesMagicWall; private ArrayList<BufferedImage> framesMagicWall;
private long previousTime; private long previousTime;
private int currentFrame; private int currentFrame = 0;
private long speed;
MagicWallModel() {
super("magicwall");
setPriority(3);
setCollideSound("touch");
this.currentFrame = 0;
this.speed = 100;
this.initSprites();
}
/** /**
* Function to animate the sprite * Function to animate the sprite
*/ */
public void update(long time) { public void update(long time) {
if (time - previousTime >= speed) { long speed = 100;
if (time - previousTime >= speed) {
// Update animation // Update animation
previousTime = time; previousTime = time;
@ -54,7 +44,7 @@ public class MagicWallModel extends DisplayableElementModel {
/** /**
* Init the subimages * Init the subimages
*/ */
private void initSprites() { /* package-private */ void initSprites() {
this.framesMagicWall = new ArrayList<BufferedImage>(); this.framesMagicWall = new ArrayList<BufferedImage>();
/* INIT SPRITE FOR DIAMOND */ /* INIT SPRITE FOR DIAMOND */
framesMagicWall.add(grabSprite(loadSprite(spriteName), 0, 0, 16, 16)); framesMagicWall.add(grabSprite(loadSprite(spriteName), 0, 0, 16, 16));

View File

@ -27,11 +27,6 @@ public class RockfordModel extends DisplayableElementModel {
private final int SIZ_X_OF_SPRITE = 16; private final int SIZ_X_OF_SPRITE = 16;
private final int SIZ_Y_OF_SPRITE = 16; private final int SIZ_Y_OF_SPRITE = 16;
/**
* Defines the current speed of the object
*/
private long speed;
/** /**
* Maps possible states for Rockford * Maps possible states for Rockford
*/ */
@ -44,29 +39,15 @@ public class RockfordModel extends DisplayableElementModel {
private long previousTime; private long previousTime;
private int currentFrame; private int currentFrame;
private boolean hasExploded; private boolean hasExploded = false;
RockfordModel() {
super("rockford", true, true);
setPriority(1);
// Speed of the animation of the sprite
this.setSpeed(100);
// Init the sprites in arrays
this.initSprites();
this.hasExploded = false;
}
public void setSpeed(int speed) {
this.speed = speed;
}
/** /**
* Updates the sprite animation * Updates the sprite animation
* (And only that single thing) * (And only that single thing)
*/ */
public void update(long time) { public void update(long time) {
if (time - this.previousTime >= this.speed) { long speed = 100;
if (time - this.previousTime >= speed) {
// Update the animation // Update the animation
this.previousTime = time; this.previousTime = time;
try { try {
@ -229,7 +210,7 @@ public class RockfordModel extends DisplayableElementModel {
* Initializes all sprites from the main image * Initializes all sprites from the main image
* Takes the sub images and append them into storage arrays * Takes the sub images and append them into storage arrays
*/ */
private void initSprites() { /* package-private */ void initSprites() {
framesBlinking = new ArrayList<BufferedImage>(); framesBlinking = new ArrayList<BufferedImage>();
framesRunningLeft = new ArrayList<BufferedImage>(); framesRunningLeft = new ArrayList<BufferedImage>();
framesRunningRight = new ArrayList<BufferedImage>(); framesRunningRight = new ArrayList<BufferedImage>();

View File

@ -10,11 +10,4 @@ package fr.enssat.BoulderDash.models.displayableElement;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class SteelWallModel extends DisplayableElementModel { public class SteelWallModel extends DisplayableElementModel {
SteelWallModel() {
super("steelwall");
setPriority(3);
setCollideSound("touch");
this.loadSprite("steelwall");
}
} }