Skip to content
This repository was archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
Fix SLime, deplacement code
Browse files Browse the repository at this point in the history
  • Loading branch information
naulan-chrzaszcz committed Jun 8, 2022
1 parent 48dfe22 commit 903958a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
7 changes: 0 additions & 7 deletions src/main/java/fr/sae/terraria/modele/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ private void gameLoop()

List<Entity> entitiesAtAdded = new ArrayList<>();
KeyFrame keyFrame = new KeyFrame(Duration.seconds(Terraria.TARGET_FPS), (ev -> {
this.player.offset[0] = Entity.IDLE;
this.player.eventInput();

// TODO TEST
if (!caught[0]) {
Torch torch = new Torch(this, 0, 0);
Expand Down Expand Up @@ -145,15 +142,11 @@ private void gameLoop()

// Updates toutes les entités
for (Entity entity : this.entities) {
if (entity instanceof CollideObjectType)
((CollideObjectType) entity).collide();
// ajoute les enfants des entités parent.
if (entity instanceof ReproductiveObjectType)
entitiesAtAdded.addAll(((ReproductiveObjectType) entity).reproduction(this));
entity.updates();
}

this.player.collide();
this.player.updates();

this.previousDays = this.clock.getDays();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/fr/sae/terraria/modele/entities/Rabbit.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public Rabbit(final Environment environment, int x, int y)
this.gravity.timer = .0;
}

this.collide();
this.move();
this.worldLimit();

Expand All @@ -63,11 +64,11 @@ public Rabbit(final Environment environment, int x, int y)
this.setX(this.x.get() + this.offset[0] * this.velocity);

if (this.offset[1] == Entity.IDLE && this.offset[0] != Entity.IDLE) {
int xProbablyVoid = (int) ((getX()+((this.offset[0] == Entity.IS_MOVING_LEFT) ? 0 : this.rect.getWidth()))/ this.environment.widthTile) + 1;
int yProbablyVoid = (int) ((getY() + (this.rect.getHeight()/2)) / environment.heightTile);
int xProbablyVoid = (int) ((getX()+((this.offset[0] == Entity.IS_MOVING_LEFT) ? 0 : this.environment.widthTile)) / this.environment.widthTile);
int yProbablyVoid = (int) (getY() / environment.heightTile);

// Si du vide risque d'y avoir lors de son déplacement
if (environment.getTileMaps().getTile(xProbablyVoid+((this.offset[0] == Entity.IS_MOVING_LEFT) ? 1 : (-1)), yProbablyVoid + 2) == TileMaps.SKY) {
if (environment.getTileMaps().getTile(xProbablyVoid, yProbablyVoid + 2) == TileMaps.SKY) {
this.offset[0] = (-1) * this.offset[0];
} else {
boolean mustJump = this.environment.getTicks() % Rabbit.JUMP_FREQUENCY == 0;
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/fr/sae/terraria/modele/entities/Slime.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

public class Slime extends Entity implements CollideObjectType, MovableObjectType, CollapsibleObjectType, SpawnableObjectType
{
public static final int WHEN_SPAWN_A_SLIME = 100;
public static final int WHEN_SPAWN_A_SLIME = 2_500;
public static final double SLIME_SPAWN_RATE = .2;

private final Environment environment;
private int nbIdle=0;


public Slime(Environment environment, int x, int y)
Expand All @@ -31,7 +30,7 @@ public Slime(Environment environment, int x, int y)

public Slime(Environment environment) { this(environment, 0, 0); }

@Override public void updates() { /* TODO document why this method is empty */
@Override public void updates() {
if (this.offset[1] == Entity.IDLE && !this.air) {
this.gravity.xInit = this.x.get();
this.gravity.yInit = this.y.get();
Expand All @@ -40,7 +39,16 @@ public Slime(Environment environment, int x, int y)
this.gravity.timer = .0;
}

this.offset[0] = Entity.IDLE;
if (this.offset[1] == Entity.IS_JUMPING) {
if (environment.getPlayer().getX() > this.x.getValue())
this.offset[0] = Entity.IS_MOVING_RIGHT;
else if (environment.getPlayer().getX() < this.x.getValue())
this.offset[0] = Entity.IS_MOVING_LEFT;
}

this.move();
this.collide(); // FIXME: 08/06/2022 : idk pourquoi le collide pose probleme pour le saut du slime
this.worldLimit();

if (!Objects.isNull(this.rect))
Expand All @@ -51,29 +59,18 @@ public Slime(Environment environment, int x, int y)
@Override public void collide() {
Map<String, Boolean> whereCollide = super.collide(this.environment);

if (!whereCollide.isEmpty()) {
if (!whereCollide.isEmpty())
if (whereCollide.get("left").equals(Boolean.TRUE) || whereCollide.get("right").equals(Boolean.TRUE))
this.offset[0] = Entity.IDLE;
}
}

@Override public void move()
{
if (environment.getPlayer().getX() > this.x.getValue())
this.offset[0] = IS_MOVING_RIGHT;
else if (environment.getPlayer().getX() < this.x.getValue())
this.offset[0] = IS_MOVING_LEFT;
else this.offset[0] = IDLE;

if (((int) (this.animation.getFrame()) == 3))
this.jump();

if (this.offset[1] == Entity.IS_JUMPING)
this.setX(this.getX() + this.offset[0] * this.velocity);
if (this.offset[1] != Entity.IS_FALLING) this.jump();
this.setX(this.getX() + (this.offset[0] * this.velocity));
}



@Override public void hit()
{
Environment.playSound("sound/daggerswipe.wav", false);
Expand All @@ -99,6 +96,4 @@ else if (environment.getPlayer().getX() < this.x.getValue())
@Override public void fall() { super.fall(); }

@Override public void worldLimit() { super.worldLimit(this.environment); }


}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public Player(final Environment environment)
this.gravity.timer = .0;
}

this.offset[0] = Entity.IDLE;
this.eventInput();
this.collide();
this.worldLimit();
this.move();

Expand Down

0 comments on commit 903958a

Please sign in to comment.