Commit 6ed013af authored by Arnaud Guerquin's avatar Arnaud Guerquin

Resolved issues #13 and #15

parent 416341be
package artificial_intelligence;
import core.gamestate.Entity;
import core.gamestate.GameState;
import core.relayer.Relayer;
import core.zone.Direction;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import graphics.guiSkeleton.DummyForTest.DummyEntity;
import map_generation.map.Map;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Math.abs;
......@@ -27,7 +28,7 @@ public class AI_Entity extends Thread {
//TODO and the final version is not yet known.
//TODO This will be fixed when other groups will be sure of the type of the objects they will use.
private GameState gameState;
private ArrayList<DummyEntity> entityList;
private List<Entity> entityList;
private GameContent gameContent;
private Relayer relayer;
......@@ -58,7 +59,7 @@ public class AI_Entity extends Thread {
* @param ArrayList<DummyEntity> the list of the entities of the game.
* @param Relayer the relayer to contact.
*/
public AI_Entity(Map map, ArrayList<DummyEntity> entityList, Relayer relayer) {
public AI_Entity(Map map, List<Entity> entityList, Relayer relayer) {
this.map = map;
this.entityList = entityList;
this.relayer = relayer;
......@@ -79,23 +80,6 @@ public class AI_Entity extends Thread {
this.relayer = relayer;
}
/**
* Inner enumeration to define the value associated to each direction.
*/
private enum Directions {
UP, LEFT, RIGHT, DOWN;
public int getValue() {
switch(this) {
case UP: return 90;
case LEFT: return 180;
case RIGHT: return 0;
case DOWN: return 270;
default: return 0;
}
}
}
/**
* Select the target.
*
......@@ -105,7 +89,7 @@ public class AI_Entity extends Thread {
float dmin =0 ;
int victim = -1;
for (int i=0;i<this.entityList.size();i++) {
DummyEntity act_entity = this.entityList.get(i);
Entity act_entity = this.entityList.get(i);
if (/*act_entity.getFaction() != this.relayer.getEntity().getFaction()*/true) {
float d = (act_entity.getX() - this.relayer.getEntity().getX()) ^ 2 + (act_entity.getY() - this.relayer.getEntity().getY()) ^ 2;
if (dmin == 0) {
......@@ -129,25 +113,25 @@ public class AI_Entity extends Thread {
*/
public void move_to_victim(int victim_i){
if(victim_i!=-1){
DummyEntity victim= this.entityList.get(victim_i);
Entity victim= this.entityList.get(victim_i);
// X is UP-DOWN
int Delta_x = this.relayer.getEntity().getX()-victim.getX();
// Y is LEFT-RIGHT
int Delta_y = this.relayer.getEntity().getY()-victim.getY();
if(abs(Delta_x)>abs(Delta_y)){
if(Delta_x>0) {
this.relayer.move(Directions.UP.getValue());
this.relayer.move(Direction.UP);
}
else {
this.relayer.move(Directions.DOWN.getValue());
this.relayer.move(Direction.DOWN);
}
}
else{ // REPAIRE INVERSE !
if (Delta_y>0) {
this.relayer.move(Directions.LEFT.getValue());
this.relayer.move(Direction.LEFT);
}
else{
this.relayer.move(Directions.RIGHT.getValue());
this.relayer.move(Direction.RIGHT);
}
}
}
......
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class AttackEvent implements Event {
@Override
public boolean execute(GameContent gameContent) {
// TODO Auto-generated method stub
return false;
}
@Override
public Event apply(GameContent gameContent) {
public Event resolve(GameContent gameContent) {
// TODO Auto-generated method stub
return null;
}
......
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class CreationEvent implements Event {
@Override
public boolean execute(GameContent gameContent) {
// TODO Auto-generated method stub
return false;
}
@Override
public Event apply(GameContent gameContent) {
public Event resolve(GameContent gameContent) {
// TODO Auto-generated method stub
return null;
}
......
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class DeathEvent implements Event {
@Override
public boolean execute(GameContent gameContent) {
// TODO Auto-generated method stub
return false;
}
@Override
public Event apply(GameContent gameContent) {
public Event resolve(GameContent gameContent) {
// TODO Auto-generated method stub
return null;
}
......
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class EmptyEvent implements Event {
......@@ -11,10 +10,15 @@ public class EmptyEvent implements Event {
private EmptyEvent(){
}
@Override
public boolean execute(GameContent gameContent) {
return false;
}
@Override
public Event apply(GameContent gameContent) {
return null;
public Event resolve(GameContent gameContent) {
return this;
}
}
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
import java.io.Serializable;
import java.util.Optional;
/**
* This interface is used to define the message transmitted between the Relayer,
......@@ -43,12 +40,21 @@ public interface Event extends Serializable {
return false;
}
boolean execute(GameContent gameContent);
Event resolve(GameContent gameContent);
/**
*
* @param gameContent
* Applies the event on the given gameContent.
* @param gameContent the gameContent on which the event will be applied.
* @return If modification to the gameState occured, it returns an Event whose apply method will perform the same modification
*/
public Event apply(GameContent gameContent);
default public Event apply(GameContent gameContent){
if(execute(gameContent))
return resolve(gameContent);
return FailedEvent.failed;
}
}
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class FailedEvent implements Event{
static final public Event failed=new FailedEvent();
private FailedEvent(){}
// @Override
// public boolean execute(GameState gameState, Map map) {
// return false;
// }
//
// @Override
// public Event resolve(GameState gameState, Map map) {
// // TODO Auto-generated method stub
// return this;
// }
@Override
public boolean execute(GameContent gameContent) {
return false;
}
@Override
public Event apply(GameContent gameContent) {
return null;
public Event resolve(GameContent gameContent) {
return this;
}
}
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class InventoryEvent implements Event {
@Override
public boolean execute(GameContent gameContent) {
return false;
}
@Override
public Event apply(GameContent gameContent) {
public Event resolve(GameContent gameContent) {
return null;
}
}
package core.event;
import core.gamestate.Entity;
import gameloop.DummyLocalGameLoop;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import graphics.guiSkeleton.DummyForTest.DummyEntity;
/**
* Created by dupriez on 22/11/15.
......@@ -14,21 +14,28 @@ import graphics.guiSkeleton.DummyForTest.DummyEntity;
public class MapInit implements Event {
private GameContent gameContent;
private DummyEntity followedDummyEntity;
private Entity followedEntity;
public MapInit(GameContent gameContent, DummyEntity dummyEntity) {
public MapInit(GameContent gameContent, Entity entity) {
this.gameContent = gameContent;
followedDummyEntity=dummyEntity;
followedEntity=entity;
}
public Entity getFollowedDummyEntity()
{
return followedEntity;
}
@Override
public Event apply(GameContent gameContent) {
public boolean execute(GameContent gameContent) {
DummyLocalGameLoop.getInstance().setContent(this.gameContent);
return null;
return true;
}
public DummyEntity getFollowedDummyEntity()
{
return followedDummyEntity;
@Override
public Event resolve(GameContent gameContent) {
// TODO Auto-generated method stub
return EmptyEvent.empty;
}
}
......@@ -2,12 +2,9 @@ package core.event;
import core.gamestate.Entity;
import core.gamestate.GameState;
import core.zone.Direction;
import core.zone.Point;
import core.zone.Translation;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class MoveEvent implements Event {
......@@ -20,26 +17,16 @@ public class MoveEvent implements Event {
}
@Override
public Event apply(GameContent gameContent) {
public boolean execute(GameContent gameContent){
Entity target =gameContent.getGameState().getEntity(targetID);
Point p =target.expectedMove(translation,gameContent.getGameState(),gameContent.getMap());
target.setCenter(p);
return new MoveEvent(targetID, translation);
return true;
}
// public boolean execute(GameState gameState, Map map) {
// Entity target =gameState.getEntity(targetID);
// Point p=target.expectedMove(translation,gameState,map);
// if(!target.isCenter(p)){
// target.setCenter(p);
// return true;
// }
// return false;
// }
//
// public Event resolve(GameState gameState, Map map) {
// // TODO Auto-generated method stub
// return null;
// }
@Override
public Event resolve(GameContent gameContent) {
return this;
}
}
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class SequentialEvent implements Event {
private Event first;
......@@ -16,19 +14,16 @@ public class SequentialEvent implements Event {
firstConsequence=null;
}
//TODO: deal with this
public boolean execute(GameState gameState, Map map) {
throw new IllegalArgumentException("Not implemented method\n");
}
//TODO: deal with this
public Event resolve(GameState gameState, Map map) {
throw new IllegalArgumentException("Not implemented method\n");
@Override
public boolean execute(GameContent gameContent) {
// TODO Auto-generated method stub
return false;
}
@Override
public Event apply(GameContent gameContent) {
public Event resolve(GameContent gameContent) {
// TODO Auto-generated method stub
return null;
}
}
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class SimultaneousEvent implements Event {
private Event first;
......@@ -12,25 +10,15 @@ public class SimultaneousEvent implements Event {
this.first=first;
this.second=second;
}
//TODO: deal with this
public boolean execute(GameState gameState, Map map) {
//Merge patch
// return first.execute(gameState, map)||second.execute(gameState, map);
return false;
@Override
public boolean execute(GameContent gameContent) {
return first.execute(gameContent)||second.execute(gameContent);
}
//TODO: deal with this
public Event resolve(GameState gameState, Map map) {
//Merge patch
// return first.resolve(gameState, map).simultaneousMerge(second.resolve(gameState, map));
return null;
}
@Override
public Event apply(GameContent gameContent) {
return null;
public Event resolve(GameContent gameContent) {
return first.resolve(gameContent).simultaneousMerge(second.resolve(gameContent));
}
}
package core.event;
import core.gamestate.GameState;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import map_generation.map.Map;
public class StatEvent implements Event {
//TODO: deal with this
public boolean execute(GameState gameState, Map map) {
@Override
public boolean execute(GameContent gameContent) {
// TODO Auto-generated method stub
return false;
}
//TODO: deal with this
public Event resolve(GameState gameState, Map map) {
// TODO Auto-generated method stub
return null;
}
@Override
public Event apply(GameContent gameContent) {
public Event resolve(GameContent gameContent) {
// TODO Auto-generated method stub
return null;
}
......
......@@ -17,7 +17,7 @@ public class Bearer extends Being{
public Bearer(int posX, int posY, int ID, int owner,Species species) {
super(posX, posY, ID, owner,species);
this.inventory=new ArrayList<Item>();
this.inventory=new ArrayList<>();
}
final boolean addItem(Item item){
......
......@@ -10,6 +10,8 @@ import core.zone.Point;
import core.zone.Rectangle;
import core.zone.Translation;
import core.zone.Zone;
import graphics.guiSkeleton.EntityDisplayer;
import graphics.guiSkeleton.DummyForTest.DotyEntityDisplayer;
import map_generation.map.BasicMapGenerators;
import map_generation.map.Map;
import map_generation.tiles.Tile;
......@@ -33,8 +35,9 @@ public abstract class Entity implements Serializable{
private Zone visibility;
private Zone collisionBox;
private TilePropertyVector tpv;
private int speed=30;
private int speed=5;
private Action action=Action.NONE;
private EntityDisplayer entityDisplayer=new DotyEntityDisplayer(this);
/**
* Constructor for an entity. This constructor is used by Being and its subclasses.
* @param posX the entity's center X position.
......@@ -87,6 +90,10 @@ public abstract class Entity implements Serializable{
this.tpv=tpv.clone();
}
public EntityDisplayer getEntityDisplayer() {
return entityDisplayer;
}
/**
* Returns the entity's center X position.
* @return the entity's center X position.
......@@ -274,28 +281,23 @@ public abstract class Entity implements Serializable{
if(!(Point.construct(x+tx,y)).equals(entity.expectedMove(Translation.construct(Direction.DOWN,tx), null, map))){
System.out.println("Small X move failed.");
return;
}else{
System.out.println("Ok small X.");
}
System.out.println("Ok small X.");
if(!(Point.construct(x,y+ty)).equals(entity.expectedMove(Translation.construct(Direction.RIGHT,ty), null, map))){
System.out.println("Small Y move failed.");
return;
}else{
System.out.println("Ok small Y.");
}
System.out.println("Ok small Y.");
if(!(Point.construct(x-tx,y)).equals(entity.expectedMove(Translation.construct(Direction.UP,tx), null, map))){
System.out.println("Small negative X move failed.");
return;
}else{
System.out.println("Ok small negative X.");
}
System.out.println("Ok small negative X.");
if(!(Point.construct(x,y-ty)).equals(entity.expectedMove(Translation.construct(Direction.LEFT,ty), null, map))){
System.out.println("Small negative Y move failed.");
return;
}else{
System.out.println("Ok small negative Y.");
}
System.out.println("Ok small negative Y.");
}
public Point getTopLeft() {
......
......@@ -23,10 +23,7 @@ public class Relayer {
private Entity entity;
private GameContent gameContent;
private Event moveEvent;
private int moveDirection;
public final static int DirectionNull = -1;
private Direction moveDirection;
public Relayer(int ID,Entity entity, GameContent gameContent){
this.ID=ID;
......@@ -38,60 +35,24 @@ public class Relayer {
return ID;
}
public void move(int direction) {
System.out.println("***************call move**********************");
public void move(Direction direction) {
Event event = null;
Event e1,e2;
if(direction != moveDirection)
stopMove();
switch(direction) {
case 0:
event = new MoveEvent(entity.getID(), Translation.construct(Direction.RIGHT,entity.getSpeed()));
break;
case 45:
e1 = new MoveEvent(entity.getID(), Translation.construct(Direction.RIGHT,entity.getSpeed()));
e2 = new MoveEvent(entity.getID(), Translation.construct(Direction.UP,entity.getSpeed()));
event = new SimultaneousEvent(e1,e2);
break;
case 90:
event = new MoveEvent(entity.getID(), Translation.construct(Direction.UP,entity.getSpeed()));
break;
case 135:
e1 = new MoveEvent(entity.getID(), Translation.construct(Direction.LEFT,entity.getSpeed()));
e2 = new MoveEvent(entity.getID(), Translation.construct(Direction.UP,entity.getSpeed()));
event = new SimultaneousEvent(e1,e2);
break;
case 180:
event = new MoveEvent(entity.getID(), Translation.construct(Direction.LEFT,entity.getSpeed()));
break;
case 225:
e1 = new MoveEvent(entity.getID(), Translation.construct(Direction.LEFT,entity.getSpeed()));
e2 = new MoveEvent(entity.getID(), Translation.construct(Direction.DOWN,entity.getSpeed()));
event = new SimultaneousEvent(e1,e2);
break;
case 270:
event = new MoveEvent(entity.getID(), Translation.construct(Direction.DOWN,entity.getSpeed()));
break;
case 315:
e1 = new MoveEvent(entity.getID(), Translation.construct(Direction.RIGHT,entity.getSpeed()));
e2 = new MoveEvent(entity.getID(), Translation.construct(Direction.DOWN,entity.getSpeed()));
event = new SimultaneousEvent(e1,e2);
break;
case DirectionNull:
return;
}
moveEvent = event;
moveDirection = direction;
if(event != null) {
if(direction!=Direction.NONE){
event = new MoveEvent(entity.getID(), Translation.construct(direction,entity.getSpeed()));
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
}
moveDirection = direction;
}