Commit 59bc1185 authored by Myriam Begel's avatar Myriam Begel

Merge branch 'death'

parents 1cb20b68 916892dc
......@@ -40,7 +40,7 @@ public class AIControler {
ai = new DefenderEntity(gameContent,dmr);
break;
default:
LOGGER.warning("Trying to instanciate unrecognized type of AI.");
LOGGER.severe("Trying to instanciate unrecognized type of AI.");
break;
}
list.add(ai);
......
package core.event;
import core.gamestate.GameContent;
/**
* Event received by Client when an entity die.
* @author Begel Myriam
* Just remove it from the GameState.
*/
public class ToClientDeathEvent implements ClientEvent {
private int entityID;
public ToClientDeathEvent(int entityID){
this.entityID = entityID;
}
public int getEntityID(){
return entityID;
}
private static final long serialVersionUID = 1L;
@Override
public boolean execute(GameContent gameContent) {
return gameContent.getGameState().removeEntity(entityID);
}
@Override
public Event resolve(GameContent gameContent) {
return null;
}
}
package core.event;
import core.gamestate.Entity;
import artificial_intelligence.AIControler;
import core.gamestate.GameContent;
/**
* This event is executed when an entity dies.
* @author Guerquin Arnaud
*
* This event is executed by Server when an entity dies.
* @author Begel Myriam
* We kill the AI if it is one (if not, the method just do nothing).
* In resolve, we send to the client almost the same Event: AI is not on ClientSide
* so we don't kill AI.
*/
public class ToServerDeathEvent implements ClientEvent {
public class ToServerDeathEvent implements ServerEvent {
private int entityID;
public ToServerDeathEvent(int entityID){
......@@ -18,17 +20,13 @@ public class ToServerDeathEvent implements ClientEvent {
private static final long serialVersionUID = 1L;
@Override
public boolean execute(GameContent gameContent) {
LOGGER.info(entityID+" is dead");
//if (AIControler.killOne(entityID);
Entity entity = gameContent.getGameState().getEntity(entityID);
//gameContent.getGameState().removeEntity(entity);
return false;
AIControler.killOne(entityID);
return gameContent.getGameState().removeEntity(entityID);
}
@Override
public Event resolve(GameContent gameContent) {
// TODO Auto-generated method stub
return null;
return new ToClientDeathEvent(entityID);
}
}
......@@ -2,10 +2,18 @@ package core.gamestate;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import core.event.Event;
import core.event.ToServerDeathEvent;
import core.zone.Zone;
import gameloop.DummyLocalGameLoop;
import gameloop.ServerLoop;
import graphics.guiSkeleton.entityDisplayer.EntityDisplayerType;
import logging.Logging;
import map_generation.tiles.TilePropertyVector;
import network.NetworkConnection;
import network.NetworkObject;
/**
* This class represents an Entity able to be hit and die.
......@@ -39,15 +47,25 @@ public class Being extends Entity {
private DamageTypeVector dtv;
/**
* Being's constructor
* @param name the Being's name.
* @param speciesName the name of the species of the Being.
* @param posX the Being's center X position. Must be in Point Coordinate.
* @param posY the Being's center Y position. Must be in Point Coordinate.
* @param ID the Being's ID.
* @param owner the Being's Relayer ID.
* @param species the Being's species.
*/
* @param collisionBox
* @param tpv
* @param visibility
* @param type
* @param triggers
* @param HP
* @param def
* @param mental
* @param dtv
* @param hitbox
*/
public Being(String name,String speciesName,int posX,int posY,int ID,int owner,Zone collisionBox,TilePropertyVector tpv,Zone visibility,EntityDisplayerType type,List<TriggerDescriptor> triggers,
int HP,int def,int mental,DamageTypeVector dtv,Zone hitbox) {
super(name,speciesName,posX, posY, ID,owner, collisionBox, tpv, visibility, type,triggers);
......@@ -80,6 +98,18 @@ public class Being extends Entity {
public boolean takeDamage(int damage){
HP=Math.min(maxHP, Math.max(HP-damage, 0));
if (HP==0){
Event event = new ToServerDeathEvent(this.getID());
Logger LOGGER = new Logging().getLogger();
NetworkConnection<NetworkObject> network = DummyLocalGameLoop.getInstance().getNetworkConnection();
if (network==null){
LOGGER.severe("NetworkConnection null, Being"+this.getID()+" can't die");
} else {
LOGGER.info("Death of "+this.getID()+" is send");
network.sendEvent(event);
}
}
return isDead();
}
......@@ -116,24 +146,24 @@ public class Being extends Entity {
*/
public int getMental() {
return mental;
}
/**
* Returns the Being's current Exp
* @return the Being's current Exp
*/
//TODO : implement exp
public int getExp() {
return 50;
}
/**
* Returns the Being's maxExp
* @return Returns the Being's maxExp
*/
//TODO : implement maxExp
public int getMaxExp() {
return 100;
}
/**
* Returns the Being's current Exp
* @return the Being's current Exp
*/
//TODO : implement exp
public int getExp() {
return 50;
}
/**
* Returns the Being's maxExp
* @return Returns the Being's maxExp
*/
//TODO : implement maxExp
public int getMaxExp() {
return 100;
}
public Zone getHitbox() {
......
......@@ -4,7 +4,6 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import core.zone.Zone;
/**
......@@ -13,7 +12,6 @@ import core.zone.Zone;
*
*/
public class GameState implements Serializable{
private static final long serialVersionUID = 1L;
/**
* The list of entities in the gamestate.
......@@ -80,10 +78,12 @@ public class GameState implements Serializable{
* @return true if it was successfully removed. false otherwise.
*/
public boolean removeEntity(int id){
if(beings.removeIf(x->x.getID()==id))
if(beings.removeIf(x->x.getID()==id)){
return true;
if(characters.removeIf(x->x.getID()==id))
}
if(characters.removeIf(x->x.getID()==id)) {
return true;
}
return false;
}
......@@ -92,7 +92,7 @@ public class GameState implements Serializable{
* @param entity the entity to remove.
* @return true if it was successfully removed. false otherwise.
*/
boolean removeEntity(Entity entity){
public boolean removeEntity(Entity entity){
if(beings.remove(entity))
return true;
if(characters.remove(entity))
......
......@@ -2,9 +2,12 @@ package gameloop;
import core.event.Event;
import core.event.MapInit;
import core.event.ToClientDeathEvent;
import core.event.ToServerDeathEvent;
import core.gamestate.GameContent;
import core.relayer.Relayer;
import core.relayer.Relayers;
import graphics.graphical_abstraction.GraphicsMasterAbstraction;
import network.EndEvent;
import network.NetworkConnection;
import network.NetworkObject;
......@@ -146,6 +149,10 @@ public class DummyLocalGameLoop extends Thread{
setFollowedRelayer(firstEntityRelayer);
relayerSemaphore.release();
}
if (eventToReceive instanceof ToClientDeathEvent && ((ToClientDeathEvent) eventToReceive).getEntityID()==followedRelayer.getCharacter().getID()) {
GraphicsMasterAbstraction.getInstance().changeGUIStateTo(GraphicsMasterAbstraction.GUIStates.GAME_OVER);
}
}
if (eventToReceive instanceof EndEvent)
System.out.println("DummyLocalGameLoop Thread: End Event Received!");
......
......@@ -4,8 +4,10 @@ import artificial_intelligence.AIControler;
import core.event.EmptyEvent;
import core.event.Event;
import core.event.MapInit;
import core.event.ToServerDeathEvent;
import core.gamestate.*;
import core.relayer.Relayers;
import graphics.graphical_abstraction.GraphicsMasterAbstraction;
import map_generation.map.Map;
import map_generation.map.MapGeneration;
import network.EndEvent;
......@@ -29,8 +31,17 @@ public class ServerLoop extends Thread {
this.networkConnection=serverConnection;
}
public synchronized NetworkConnection<NetworkObject> getNetworkConnection()
{
return networkConnection;
}
private static ServerLoop ourInstance = new ServerLoop();
public static ServerLoop getInstance() {
return ourInstance;
}
private ServerLoop() {
}
int cframe = 0;
private GameContent load(){
Map newMap= MapGeneration.mapGeneration(15, 20);
int playerNumber;
......
......@@ -50,15 +50,13 @@ public class ClientOutputThread extends Thread{
public void run() {
try {
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
LOGGER.info("ClientOutputThread running");
try {
while (true)
{
LOGGER.info("ClientOutputThread running");
//retrieves and removes first element from the outgoing queue, blocking if necessary
NetworkObject event=outgoingEvents.takeFirst();
outputStream.writeObject(event);
LOGGER.info("Object written "+event);
}
} catch (InterruptedException e) {
LOGGER.severe("Problem with interrupted exception in network:ClientOutputThreadClass");
......
......@@ -46,7 +46,7 @@ public class LocalConnection<E extends NetworkObject> extends NetworkConnection<
else
{
if (notSerializable(event))
throw new AssertionError("networks: Please don't try to use unserializable objects");
throw new AssertionError("networks: Please don't try to use unserializable objects"+event);
try {
outgoingEvents.putLast(event);
......
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