Commit e3e9ed45 authored by Myriam Begel's avatar Myriam Begel

Death ok for singleplayer but don't find the networkConnection in multiplayer...

Death ok for singleplayer but don't find the networkConnection in multiplayer so NullPointerException
parent aa403564
......@@ -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;
/**
* @author Begel Myriam
*/
public class ToClientDeathEvent implements ClientEvent {
private int entityID;
public ToClientDeathEvent(int entityID){
this.entityID = 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 artificial_intelligence.AIControler;
import core.gamestate.Entity;
import core.gamestate.GameContent;
/**
* This event is executed when an entity dies.
* @author Guerquin Arnaud
* @author Begel Myriam
*
*/
public class ToServerDeathEvent implements ClientEvent {
public class ToServerDeathEvent implements ServerEvent {
private int entityID;
public ToServerDeathEvent(int entityID){
this.entityID = entityID;
}
public int getEntityID(){
return entityID;
}
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,15 @@ 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();
LOGGER.info(this.getID()+" is dead");
DummyLocalGameLoop temp1 = DummyLocalGameLoop.getInstance();
NetworkConnection<NetworkObject> temp2 = temp1.getNetworkConnection();
LOGGER.info((temp2==null)+" hope false");
temp2.sendEvent(event);
}
return isDead();
}
......@@ -116,24 +143,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))
......
......@@ -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;
......@@ -30,7 +32,6 @@ public class ServerLoop extends Thread {
}
int cframe = 0;
private GameContent load(){
Map newMap= MapGeneration.mapGeneration(15, 20);
int playerNumber;
......@@ -72,6 +73,9 @@ public class ServerLoop extends Thread {
/*Add all event received to the queue*/
for(Event event=(Event)networkConnection.receiveEvent(false);event!=null;event=(Event)networkConnection.receiveEvent(false)){
queue.addEvent(event, cframe);
if (event instanceof ToServerDeathEvent && ((ToServerDeathEvent) event).getEntityID()==0) {
GraphicsMasterAbstraction.getInstance().changeGUIStateTo(GraphicsMasterAbstraction.GUIStates.GAME_OVER);
}
if (event instanceof EndEvent) {
System.out.println("DummyServerLoop: Just received an EndEvent");
//this is so bad because if this thread sleeps for too long it's a disaster
......
......@@ -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");
......
......@@ -47,7 +47,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