Commit 00af072c authored by Chouasne's avatar Chouasne

Corrected the code for getting red when HIT and optimisation

parent 09da0877
......@@ -13,7 +13,8 @@ import java.util.HashMap;
import java.util.Map;
import graphics.guiSkeleton.mapManagement.GraphicsMap;
import graphics.guiSkeleton.sprites.Animation;
import graphics.guiSkeleton.sprites.EntityAnimation;
import graphics.guiSkeleton.sprites.EntityAnimation;
import graphics.guiSkeleton.sprites.Sprite;
......@@ -28,13 +29,16 @@ public class EntityDisplayer implements Serializable {
private static final long serialVersionUID = 1L;
//The Entity this Displayer is associated to
private Entity associatedEntity;
private Map<Action, Animation>[] animationMapTab= new Map[Direction.values().length] ;
private Map<Action, EntityAnimation>[] EntityAnimationMapTab= new Map[Direction.values().length] ;
private char charForTerminalSprite;
private boolean HIT = false;
private int delay_HIT = 0;
private int delay_HIT_max = 5;
public EntityDisplayer(Entity associatedEntity, Map<Action, Animation>[] animationMapTab, char charForTerminalSprite) {
public EntityDisplayer(Entity associatedEntity, Map<Action, EntityAnimation>[] EntityAnimationMapTab, char charForTerminalSprite) {
this.associatedEntity = associatedEntity;
this.charForTerminalSprite = charForTerminalSprite;
this.animationMapTab = animationMapTab;
this.EntityAnimationMapTab = EntityAnimationMapTab;
}
/**
......@@ -42,36 +46,35 @@ public class EntityDisplayer implements Serializable {
* @param graphicsMap: the GraphicsMap of the map the associatedEntity is on
* @return The PositionedSprite that the gui should display to represent this entity
*/
public PositionedSprite getPositionedSprite(GraphicsMap graphicsMap) {
//Get the good image
int dir = associatedEntity.getDirection().ordinal();
Animation animation;
EntityAnimation entityAnimation;
if (associatedEntity.hasAction(Action.ATTACK)) {
animation = animationMapTab[dir].get(Action.ATTACK);
if (associatedEntity.hasAction(Action.HIT)) {
animation.setHIT(true);
associatedEntity.removeAction(Action.HIT);
}
entityAnimation = EntityAnimationMapTab[dir].get(Action.ATTACK);
associatedEntity.removeAction(Action.ATTACK);
}
else if (associatedEntity.hasAction(Action.WALK)) {
animation = animationMapTab[dir].get(Action.WALK);
if (associatedEntity.hasAction(Action.HIT)) {
animation.setHIT(true);
associatedEntity.removeAction(Action.HIT);
}
entityAnimation = EntityAnimationMapTab[dir].get(Action.WALK);
associatedEntity.removeAction(Action.WALK);
}
else {
animation = animationMapTab[dir].get(Action.NONE);
if (associatedEntity.hasAction(Action.HIT)) {
animation.setHIT(true);
associatedEntity.removeAction(Action.HIT);
}
entityAnimation = EntityAnimationMapTab[dir].get(Action.NONE);
}
Sprite sprite;
if (associatedEntity.hasAction(Action.HIT)) {
this.HIT = true;
associatedEntity.removeAction(Action.HIT);
}
if (this.HIT){
this.delay_HIT++;
if (this.delay_HIT > this.delay_HIT_max) {this.delay_HIT = 0; this.HIT = false;}
sprite = new Sprite(entityAnimation.getSpriteImageRed());
}
Sprite sprite = new Sprite(animation.getSpriteImage());
else sprite = new Sprite(entityAnimation.getSpriteImage());
GraphicsMapPoint entityPositionGraphicsMapPoint = GraphicalBridgeConfiguration.makeMapPointFromCoreCoordinates(associatedEntity.getX(), associatedEntity.getY());
GraphicsMapPoint spriteTopLeftGraphicsMapPoint = entityPositionGraphicsMapPoint.translate(-animation.getCurrentSpriteWidth() / 2, -animation.getCurrentSpriteHeight() / 2);
GraphicsMapPoint spriteTopLeftGraphicsMapPoint = entityPositionGraphicsMapPoint.translate(-entityAnimation.getCurrentSpriteWidth() / 2, -entityAnimation.getCurrentSpriteHeight() / 2);
return PositionedSprite.PositionSprite(sprite, spriteTopLeftGraphicsMapPoint);
}
......
package graphics.guiSkeleton.sprites;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.Serializable;
import java.util.ArrayList;
......@@ -22,9 +24,6 @@ public class Animation implements Serializable, Displayable
private int inFrameCounter = 0;
private int currentFrame = 0;
private List<Frame> frames = new ArrayList<Frame>();
private boolean HIT = false;
private int delay_HIT = 0;
private int delay_HIT_max = 5;
public Animation(Sprite spriteSheet, int[] delayTable)
{
......@@ -50,7 +49,7 @@ public class Animation implements Serializable, Displayable
{
int width = spriteSheet.getSpriteImage().getWidth()/nbFrames;
int height = spriteSheet.getSpriteImage().getHeight();
frames.add(new Frame(spriteSheet.getSpriteImage().getSubimage(i*width, 0, width, height), delay));
frames.add(new Frame(spriteSheet.getSpriteImage().getSubimage(i*width, 0, width, height), delay));;
}
}
......@@ -75,43 +74,17 @@ public class Animation implements Serializable, Displayable
}
}
public void setHIT (boolean b){
this.HIT = b;
}
public static void updateAnimation()
{
globalCounter++;
}
private static void colorInRed(BufferedImage sprite){
int width = sprite.getWidth();
int height = sprite.getHeight();
WritableRaster raster = sprite.getRaster();
for (int xx = 0; xx < width; xx++) {
for (int yy = 0; yy < height; yy++) {
int[] pixels = raster.getPixel(xx, yy, (int[]) null);
pixels[0] = 255;
pixels[1] = 0;
pixels[2] = 0;
raster.setPixel(xx, yy, pixels);
}
}
}
public BufferedImage getSpriteImage()
{
while (localCounter < globalCounter)
update();
BufferedImage sprite = frames.get(currentFrame).getSpriteImage();
if (HIT){ //si on est blessé on colore l'image en rouge pendant le tempsdelay_HIT_max
colorInRed(sprite);
this.delay_HIT++;
if (this.delay_HIT > this.delay_HIT_max) {this.delay_HIT = 0; this.HIT = false;}
}
return sprite;
BufferedImage sprite = frames.get(currentFrame).getSpriteImage();
return sprite;
}
public void restart()
......
package graphics.guiSkeleton.sprites;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Colin on 30/12/15.
* Instances of this class are looping animation (looping on the frame list).
* They can be constructed via spritesheet (where sprites appear horizontally, left to right), or via sprite list.
*/
public class EntityAnimation implements Serializable, Displayable
{
// global counter incremented each time we display game panel
private static int globalCounter = 0;
// local counter owned by the instance, used to know if the instance has to be updated
private int localCounter = globalCounter;
// local counter used to know when we are in the displaying of a frame
private int inFrameCounter = 0;
private int currentFrame = 0;
private List<Frame> frames = new ArrayList<Frame>();
private List<Frame> framesRed = new ArrayList<Frame>();
private static void colorInRed(BufferedImage sprite){
int width = sprite.getWidth();
int height = sprite.getHeight();
WritableRaster raster = sprite.getRaster();
for (int xx = 0; xx < width; xx++) {
for (int yy = 0; yy < height; yy++) {
int[] pixels = raster.getPixel(xx, yy, (int[]) null);
pixels[0] = 255;
pixels[1] = 0;
pixels[2] = 0;
raster.setPixel(xx, yy, pixels);
}
}
}
public EntityAnimation(BufferedImage spriteCouleur, BufferedImage spriteRed, int width, int height, int nbFrames, int delay, int line)
{
for (int i = 0 ; i < nbFrames ; i++)
{
frames.add(new Frame(spriteCouleur.getSubimage(i*width, line*height, width, height), delay));;
}
colorInRed(spriteRed);
for (int i = 0 ; i < nbFrames ; i++) {
framesRed.add(new Frame(spriteRed.getSubimage(i * width, line*height, width, height), delay));
}
}
private void update()
{
localCounter ++;
inFrameCounter ++;
if (inFrameCounter > frames.get(currentFrame).getDelay())
{
inFrameCounter = 0;
currentFrame ++;
if (currentFrame >= frames.size())
currentFrame = 0;
}
}
public static void updateAnimation()
{
globalCounter++;
}
public BufferedImage getSpriteImage()
{
while (localCounter < globalCounter)
update();
BufferedImage sprite = frames.get(currentFrame).getSpriteImage();
return sprite;
}
public BufferedImage getSpriteImageRed() {
while (localCounter < globalCounter)
update();
BufferedImage spritered = framesRed.get(currentFrame).getSpriteImage();
return spritered;
}
public void restart()
{
currentFrame = 0;
inFrameCounter = 0;
}
public int getCurrentSpriteHeight(){return frames.get(currentFrame).getSpriteImage().getHeight();}
public int getCurrentSpriteWidth(){return frames.get(currentFrame).getSpriteImage().getWidth();}
}
......@@ -4,6 +4,7 @@ import assets.UsedForLoadingSprites;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;
/**
......@@ -17,6 +18,8 @@ public class SpriteStorage {
public static SpriteStorage getInstance() {
return ourInstance;
}
private SpriteStorage() {
try {
chatRose = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("chatrose.png")));
......@@ -29,6 +32,16 @@ public class SpriteStorage {
firebolt = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("firebolt.png")));
frostbolt = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("frostbolt.png")));
gold = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("gold.png")));
chatRoseRED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("chatrose.png")));
ronflexRED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("ronflex.png")));
healthBar1RED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("HealthBar1.png")));
healthBar1ContainerRED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("HealthBar1_Container.png")));
invisibleRED = new Sprite(new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB));
ponytaRED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("ponyta.png")));
diaglaRED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("diagla.png")));
fireboltRED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("firebolt.png")));
frostboltRED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("frostbolt.png")));
goldRED = new Sprite(ImageIO.read(UsedForLoadingSprites.getInstance().getClass().getResource("gold.png")));
} catch (IOException e) {
e.printStackTrace();
......@@ -45,6 +58,16 @@ public class SpriteStorage {
private Sprite firebolt;
private Sprite frostbolt;
private Sprite gold;
private Sprite chatRoseRED;
private Sprite healthBar1RED;
private Sprite healthBar1ContainerRED;
private Sprite ronflexRED;
private Sprite invisibleRED;
private Sprite ponytaRED;
private Sprite diaglaRED;
private Sprite fireboltRED;
private Sprite frostboltRED;
private Sprite goldRED;
public Sprite getHealthBar1Container() {
return healthBar1Container;
......@@ -84,4 +107,40 @@ public class SpriteStorage {
return frostbolt;
}
public Sprite getHealthBar1ContainerRED() {
return healthBar1ContainerRED;
}
public Sprite getHealthBar1RED() {
return healthBar1RED;
}
public Sprite getChatRoseRED() {
return chatRoseRED;
}
public Sprite getRonflexRED() {
return ronflexRED;
}
public Sprite getInvisibleRED() {
return invisibleRED;
}
public Sprite getPonytaRED() {
return ponytaRED;
}
public Sprite getGoldRED(){
return goldRED;
}
public Sprite getDiaglaRED(){
return diaglaRED;
}
public Sprite getFireboltRED(){
return fireboltRED;
}
public Sprite getFrostboltRED(){ return frostboltRED; }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment