This commit is contained in:
Daniel Langbein 2024-11-02 13:33:25 +01:00
parent 8c4152c273
commit aa482c8e5d
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
3 changed files with 159 additions and 164 deletions

View File

@ -7,7 +7,7 @@ import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
/** /**
* ElementPositionUpdateHelper * ElementPositionUpdateHelper
* * <p>
* Updates position of all elements displayed on the map, according to their * Updates position of all elements displayed on the map, according to their
* next potential position. Each object has a weight, which is used to compare * next potential position. Each object has a weight, which is used to compare
* their power to destroy in the food chain. Sorry for that Darwinism. * their power to destroy in the food chain. Sorry for that Darwinism.
@ -16,149 +16,156 @@ import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
* @since 2015-06-19 * @since 2015-06-19
*/ */
public class BoulderAndDiamondController implements Runnable { public class BoulderAndDiamondController implements Runnable {
private LevelModel levelModel; private LevelModel levelModel;
private AudioLoadHelper audioLoadHelper; private AudioLoadHelper audioLoadHelper;
private Thread elementMovingThread; private Thread elementMovingThread;
/** /**
* Class constructor * Class constructor
* *
* @param levelModel Level model * @param levelModel Level model
*/ */
public BoulderAndDiamondController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) { public BoulderAndDiamondController(LevelModel levelModel, AudioLoadHelper audioLoadHelper) {
this.levelModel = levelModel; this.levelModel = levelModel;
this.audioLoadHelper = audioLoadHelper; this.audioLoadHelper = audioLoadHelper;
// Start thread // Start thread
this.elementMovingThread = new Thread(this); this.elementMovingThread = new Thread(this);
this.elementMovingThread.start(); this.elementMovingThread.start();
} }
/** /**
* Watches for elements to be moved * Watches for elements to be moved
*/ */
public void run() { public void run() {
while (this.levelModel.isGameRunning()) { while (this.levelModel.isGameRunning()) {
if(!this.levelModel.getGamePaused()){ if (!this.levelModel.getGamePaused()) {
this.manageFallingObject(); this.manageFallingObject();
} }
try { try {
Thread.sleep(250); Thread.sleep(250);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
/** /**
* Scan the ground to detect the boulders & the diamonds, then make them * Scan the ground to detect the boulders & the diamonds, then make them
* fall if necessary * fall if necessary
* Note: scan of the ground upside down: we want things to fall slowly ! * Note: scan of the ground upside down: we want things to fall slowly !
*/ */
private void manageFallingObject() { private void manageFallingObject() {
for (int x = this.levelModel.getSizeWidth() - 1; x >= 0; x--) { for (int x = this.levelModel.getSizeWidth() - 1; x >= 0; x--) {
for (int y = this.levelModel.getSizeHeight() - 1; y >= 0; y--) { for (int y = this.levelModel.getSizeHeight() - 1; y >= 0; y--) {
// Gets the spriteName of actual DisplayableElementModel object scanned // Gets the spriteName of actual DisplayableElementModel object scanned
DisplayableElementModel elementModel = this.levelModel.getGroundLevelModel()[x][y]; DisplayableElementModel elementModel = this.levelModel.getGroundLevelModel()[x][y];
if(elementModel == null) { if (elementModel == null) {
elementModel = new DirtModel(); elementModel = new DirtModel();
} }
String spriteName = elementModel.getSpriteName(); String spriteName = elementModel.getSpriteName();
// If it is a boulder or a diamond...
if (spriteName == "boulder" || spriteName == "diamond") {
this.manageFall(x, y);
} else if(spriteName == "expandingwall"){
if(this.expandWall(x,y).equals("left")){
x -= 1;
}
}
}
}
}
/** // If it is a boulder or a diamond...
* Expand the wall at left & right if (spriteName == "boulder" || spriteName == "diamond") {
this.manageFall(x, y);
} else if (spriteName == "expandingwall") {
if (this.expandWall(x, y).equals("left")) {
x -= 1;
}
}
}
}
}
private String getSpriteNameLeft(int x, int y) {
DisplayableElementModel elementLeft = this.levelModel.getGroundLevelModel()[x - 1][y];
return elementLeft.getSpriteName();
}
private String getSpriteNameRight(int x, int y) {
DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y];
return elementRight.getSpriteName();
}
private String getSpriteNameBelow(int x, int y) {
DisplayableElementModel elementBelow = getElementBelow(x, y);
return elementBelow.getSpriteName();
}
private DisplayableElementModel getElementBelow(int x, int y) {
return this.levelModel.getGroundLevelModel()[x][y + 1];
}
/**
* Expand the wall at left & right
* *
* @param x Horizontal position * @param x Horizontal position
* @param y Vertical position * @param y Vertical position
*/ */
private String expandWall(int x, int y) { private String expandWall(int x, int y) {
DisplayableElementModel elementLeft = this.levelModel.getGroundLevelModel()[x - 1][y]; String way = "";
DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y]; if (getSpriteNameLeft(x, y) == "black") {
String spriteNameLeft = elementLeft.getSpriteName(); this.levelModel.expandThisWallToLeft(x, y);
String spriteNameRight = elementRight.getSpriteName(); way = "left";
}
String way = ""; if (getSpriteNameRight(x, y) == "black") {
if(spriteNameLeft == "black"){ this.levelModel.expandThisWallToRight(x, y);
this.levelModel.expandThisWallToLeft(x,y); way = "right";
way = "left"; }
} return way;
if(spriteNameRight == "black"){ }
this.levelModel.expandThisWallToRight(x,y);
way = "right";
}
return way;
}
/** /**
* Manages the fall of elements * Manages the fall of elements
* *
* @param x Horizontal position * @param x Horizontal position
* @param y Vertical position * @param y Vertical position
*/ */
private void manageFall(int x, int y) { private void manageFall(int x, int y) {
// Get informed about Rockford surroundings
DisplayableElementModel elementBelow = this.levelModel.getGroundLevelModel()[x][y + 1];
DisplayableElementModel elementLeft = this.levelModel.getGroundLevelModel()[x - 1][y];
DisplayableElementModel elementRight = this.levelModel.getGroundLevelModel()[x + 1][y];
String spriteNameBelow = elementBelow.getSpriteName(); // Then, process in case of the surrounding
String spriteNameLeft = elementLeft.getSpriteName(); String spriteNameBelow = getSpriteNameBelow(x, y);
String spriteNameRight = elementRight.getSpriteName(); if (spriteNameBelow == "black") {
this.levelModel.makeThisDisplayableElementFall(x, y);
// Then, process in case of the surrounding } else if (spriteNameBelow == "boulder") {
if (spriteNameBelow == "black") { // Boulders have to roll if they hit another boulder
this.levelModel.makeThisDisplayableElementFall(x, y); if (this.levelModel.getGroundLevelModel()[x - 1][y + 1].getSpriteName() == "black") {
} else if (spriteNameBelow == "boulder") { this.levelModel.makeThisBoulderSlideLeft(x, y);
// Boulders have to roll if they hit another boulder } else if (this.levelModel.getGroundLevelModel()[x + 1][y + 1].getSpriteName() == "black") {
if (this.levelModel.getGroundLevelModel()[x - 1][y + 1].getSpriteName() == "black") { this.levelModel.makeThisBoulderSlideRight(x, y);
this.levelModel.makeThisBoulderSlideLeft(x, y); }
} else if (this.levelModel.getGroundLevelModel()[x + 1][y + 1].getSpriteName() == "black") { } else if (spriteNameBelow == "rockford" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) {
this.levelModel.makeThisBoulderSlideRight(x, y); this.levelModel.exploseGround(x, y + 1);
}
} else if (spriteNameBelow == "rockford" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) {
this.levelModel.exploseGround(x, y + 1);
this.audioLoadHelper.playSound("die"); this.audioLoadHelper.playSound("die");
try { try {
Thread.sleep(25); Thread.sleep(25);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
this.levelModel.setGameRunning(false); this.levelModel.setGameRunning(false);
} else if (spriteNameBelow == "magicwall") { } else if (spriteNameBelow == "magicwall") {
if (this.levelModel.getGroundLevelModel()[x][y].getSpriteName() == "boulder" if (this.levelModel.getGroundLevelModel()[x][y].getSpriteName() == "boulder"
&& (this.levelModel.getGroundLevelModel()[x][y+2].getSpriteName() == "dirt" || && (this.levelModel.getGroundLevelModel()[x][y + 2].getSpriteName() == "dirt" ||
this.levelModel.getGroundLevelModel()[x][y+2].getSpriteName() == "black")) { this.levelModel.getGroundLevelModel()[x][y + 2].getSpriteName() == "black")) {
if(this.levelModel.getGroundLevelModel()[x][y].isConvertible()) { if (this.levelModel.getGroundLevelModel()[x][y].isConvertible()) {
this.levelModel.transformThisBoulderIntoADiamond(x, y); this.levelModel.transformThisBoulderIntoADiamond(x, y);
} else { } else {
this.levelModel.deleteThisBoulder(x, y); this.levelModel.deleteThisBoulder(x, y);
} }
} }
} else if (elementBelow.isDestructible() && spriteNameBelow != "dirt" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) { } else if (getElementBelow(x, y).isDestructible() && spriteNameBelow != "dirt" && this.levelModel.getGroundLevelModel()[x][y].isFalling()) {
this.levelModel.exploseThisBrickWall(x, y); this.levelModel.exploseThisBrickWall(x, y);
} else if (spriteNameLeft == "rockford" && this.levelModel.getRockford().isRunningRight() && this.levelModel.getGroundLevelModel()[x + 1][y].getSpriteName() == "black") { } else if (getSpriteNameLeft(x, y) == "rockford" && this.levelModel.getRockford().isRunningRight() && this.levelModel.getGroundLevelModel()[x + 1][y].getSpriteName() == "black") {
this.levelModel.moveThisBoulderToRight(x, y); this.levelModel.moveThisBoulderToRight(x, y);
} else if (spriteNameRight == "rockford" && this.levelModel.getRockford().isRunningLeft() && this.levelModel.getGroundLevelModel()[x - 1][y].getSpriteName() == "black") { } else if (getSpriteNameRight(x, y) == "rockford" && this.levelModel.getRockford().isRunningLeft() && this.levelModel.getGroundLevelModel()[x - 1][y].getSpriteName() == "black") {
this.levelModel.moveThisBoulderToLeft(x, y); this.levelModel.moveThisBoulderToLeft(x, y);
} else { } else {
this.levelModel.getGroundLevelModel()[x][y].setFalling(false); this.levelModel.getGroundLevelModel()[x][y].setFalling(false);
} }
} }
} }

View File

@ -114,16 +114,22 @@ public class RockfordModel extends DisplayableElementModel {
} }
} }
/** private void resetMovement(){
* Stops the Rockford movement
*/
public void startStaying() {
isCollisionDone = false; isCollisionDone = false;
isStaying = true; isStaying = false;
isRunningLeft = false; isRunningLeft = false;
isRunningRight = false; isRunningRight = false;
isRunningUp = false; isRunningUp = false;
isRunningDown = false; isRunningDown = false;
}
/**
* Stops the Rockford movement
*/
public void startStaying() {
resetMovement();
isStaying = true;
previousTime = 0; previousTime = 0;
currentFrame = 0; currentFrame = 0;
} }
@ -132,12 +138,9 @@ public class RockfordModel extends DisplayableElementModel {
* Starts moving Rockford to the left * Starts moving Rockford to the left
*/ */
public void startRunningLeft() { public void startRunningLeft() {
isCollisionDone = false; resetMovement();
isStaying = false;
isRunningLeft = true; isRunningLeft = true;
isRunningRight = false;
isRunningUp = false;
isRunningDown = false;
previousTime = 0; previousTime = 0;
} }
@ -145,12 +148,9 @@ public class RockfordModel extends DisplayableElementModel {
* Starts moving Rockford to the right * Starts moving Rockford to the right
*/ */
public void startRunningRight() { public void startRunningRight() {
isCollisionDone = false; resetMovement();
isStaying = false;
isRunningLeft = false;
isRunningRight = true; isRunningRight = true;
isRunningUp = false;
isRunningDown = false;
previousTime = 0; previousTime = 0;
} }
@ -158,12 +158,9 @@ public class RockfordModel extends DisplayableElementModel {
* Rockford running up * Rockford running up
*/ */
public void startRunningUp() { public void startRunningUp() {
isCollisionDone = false; resetMovement();
isStaying = false;
isRunningLeft = false;
isRunningRight = false;
isRunningUp = true; isRunningUp = true;
isRunningDown = false;
previousTime = 0; previousTime = 0;
} }
@ -171,12 +168,9 @@ public class RockfordModel extends DisplayableElementModel {
* Rockford running down * Rockford running down
*/ */
public void startRunningDown() { public void startRunningDown() {
isCollisionDone = false; resetMovement();
isStaying = false;
isRunningLeft = false;
isRunningRight = false;
isRunningUp = false;
isRunningDown = true; isRunningDown = true;
previousTime = 0; previousTime = 0;
} }

View File

@ -43,13 +43,12 @@ public abstract class GroundView extends JPanel implements Observer {
*/ */
public void drawTerrain(int width, int height, Graphics g) { public void drawTerrain(int width, int height, Graphics g) {
// Draw items // Draw items
if (this.levelModel.getMode() == "game") { for (int x = 0; x < width; x++) {
for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) {
for (int y = 0; y < height; y++) { g.drawImage(this.levelModel.getImage(x, y), (x * 16), (y * 16), this);
g.drawImage(this.levelModel.getImage(x, y), (x * 16), (y * 16), this);
}
} }
}
if (this.levelModel.getMode() == "game") {
if(!this.levelModel.isGameRunning()) { if(!this.levelModel.isGameRunning()) {
if(!this.levelModel.getRockford().getHasExplosed()) { if(!this.levelModel.getRockford().getHasExplosed()) {
this.displayWin(); this.displayWin();
@ -58,11 +57,6 @@ public abstract class GroundView extends JPanel implements Observer {
} }
} }
} else { } else {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
g.drawImage(this.levelModel.getImage(x, y), (x * 16), (y * 16), this);
}
}
if (this.levelModel.getShowCursor()) { if (this.levelModel.getShowCursor()) {
g.drawImage( g.drawImage(
this.levelModel.getCursorImage(), this.levelModel.getCursorImage(),