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

Commit

Permalink
Ajout de l'animation d'invincibilité
Browse files Browse the repository at this point in the history
  • Loading branch information
naulan-chrzaszcz committed Jun 17, 2022
1 parent 1c5cc1b commit 089f865
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/main/java/fr/sae/terraria/modele/entities/Slime.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.sae.terraria.modele.Environment;
import fr.sae.terraria.modele.entities.entity.*;
import fr.sae.terraria.modele.entities.player.Player;
import fr.sae.terraria.modele.entities.player.inventory.Stack;
import fr.sae.terraria.modele.entities.tools.Tool;

Expand All @@ -13,7 +14,6 @@ public class Slime extends EntityMovable implements CollideObjectType, Collapsib
{
public static final int WHEN_SPAWN_A_SLIME = 2_500;
public static final double SLIME_SPAWN_RATE = .2;
public static final int TIME_BEFORE_HITTING_AGAIN_THE_PLAYER = 100;


public Slime(Environment environment, int x, int y)
Expand Down Expand Up @@ -67,10 +67,10 @@ else if (environment.getPlayer().getX() < this.x.getValue())

if (environment.getPlayer().getRect().collideRect(this.getRect()) && !environment.getPlayer().getHit()){
environment.getPlayer().hit();
environment.getPlayer().setInvicibilityFrame(this.environment.getTicks());
environment.getPlayer().setInvisibilityFrame(this.environment.getTicks());
}

if (environment.getPlayer().getHit() && this.environment.getTicks() - environment.getPlayer().getInvicibilityFrame() == TIME_BEFORE_HITTING_AGAIN_THE_PLAYER)
if (environment.getPlayer().getHit() && this.environment.getTicks() - environment.getPlayer().getInvisibilityFrame() == Player.TIME_BEFORE_HITTING_AGAIN_THE_PLAYER)
environment.getPlayer().setHit(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import fr.sae.terraria.modele.entities.entity.StowableObjectType;
import fr.sae.terraria.modele.entities.player.Player;
import fr.sae.terraria.modele.entities.player.inventory.Inventory;
import fr.sae.terraria.modele.entities.player.inventory.Stack;


public class Meat implements ConsumableObjectType, StowableObjectType
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/fr/sae/terraria/modele/entities/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import fr.sae.terraria.modele.Environment;
import fr.sae.terraria.modele.TileMaps;
import fr.sae.terraria.modele.entities.entity.*;
import fr.sae.terraria.modele.entities.items.Item;
import fr.sae.terraria.modele.entities.player.inventory.Inventory;
import fr.sae.terraria.modele.entities.player.inventory.Stack;
import fr.sae.terraria.vue.View;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.Image;
Expand All @@ -20,24 +20,26 @@

public class Player extends EntityMovable implements CollideObjectType, CollapsibleObjectType, SpawnableObjectType
{
public static final int TIME_BEFORE_HITTING_AGAIN_THE_PLAYER = 100;
public static final int BREAK_BLOCK_DISTANCE = 1;

private final EnumMap<KeyCode, Boolean> keysInput;
private final EnumMap<MouseButton, Boolean> mouseInput;
private Boolean hit;
private int invicibilityFrame;

private final SimpleBooleanProperty drunk;
private final BooleanProperty drunk;
private final BooleanProperty hit;

private final Inventory inventory;
private Stack stackSelected;

private int invisibilityFrame;


public Player(final Environment environment)
{
super(environment, 0, 0);
this.invicibilityFrame = 0;
this.hit = false;
this.invisibilityFrame = 0;
this.hit = new SimpleBooleanProperty(false);
this.drunk = new SimpleBooleanProperty(false);
this.inventory = new Inventory(this);

Expand Down Expand Up @@ -86,7 +88,7 @@ public Player(final Environment environment)
@Override public void hit()
{
this.pv.set(this.getPv() - 1);
this.hit = true;
this.hit.set(true);
}

@Override public void spawn(int x, int y)
Expand Down Expand Up @@ -165,17 +167,18 @@ else if (key == KeyCode.Q)

public void pickup(StowableObjectType pickupObj) { if (!Objects.isNull(pickupObj)) this.inventory.put(pickupObj); }

public SimpleBooleanProperty drunkProperty() { return drunk; }
public BooleanProperty drunkProperty() { return this.drunk; }
public BooleanProperty hitProperty() { return this.hit; }


public Map<MouseButton, Boolean> getMouseInput() { return this.mouseInput; }
public Map<KeyCode, Boolean> getKeysInput() { return this.keysInput; }
public Stack getStackSelected() { return this.stackSelected; }
public Inventory getInventory() { return this.inventory; }
public boolean getHit() { return this.hit; }
public int getInvicibilityFrame() { return invicibilityFrame; }
public boolean getHit() { return this.hit.get(); }
public int getInvisibilityFrame() { return invisibilityFrame; }

public void setStackSelected(Stack stackSelected) { this.stackSelected = stackSelected; }
public void setHit(boolean b) { this.hit = b; }
public void setInvicibilityFrame(int ticks2) { this.invicibilityFrame = ticks2; }
public void setHit(boolean b) { this.hit.set(b); }
public void setInvisibilityFrame(int ticks2) { this.invisibilityFrame = ticks2; }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package fr.sae.terraria.modele.entities.player.inventory;

import fr.sae.terraria.modele.entities.entity.StowableObjectType;
import fr.sae.terraria.modele.entities.items.Item;
import fr.sae.terraria.modele.entities.player.Player;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.input.KeyCode;

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/fr/sae/terraria/vue/entities/PlayerView.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package fr.sae.terraria.vue.entities;

import fr.sae.terraria.Terraria;
import fr.sae.terraria.modele.entities.player.Player;
import fr.sae.terraria.vue.View;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.Image;
import javafx.util.Duration;


public class PlayerView extends EntityView
Expand All @@ -27,6 +31,22 @@ public PlayerView(final Player player, double scaleMultiplicatorWidth, double sc

this.widthPlayer = (int) this.playerIdleImg.getWidth();
this.heightPlayer = (int) this.playerIdleImg.getHeight();

boolean[] visible = new boolean[] {true};
player.hitProperty().addListener((obs, oldBool, newBool) -> {
if (newBool.booleanValue()) {
Timeline timeLine = new Timeline();
timeLine.setCycleCount(8);

KeyFrame key = new KeyFrame(Duration.seconds((Player.TIME_BEFORE_HITTING_AGAIN_THE_PLAYER * Terraria.TARGET_FPS)/8), b -> {
visible[0] = !visible[0];
this.imgView.setVisible(visible[0]);
});

timeLine.getKeyFrames().add(key);
timeLine.play();
}
});
}

@Override protected void animation(int frame)
Expand Down

0 comments on commit 089f865

Please sign in to comment.