Commit f3c25cb0 authored by Arnaud Guerquin's avatar Arnaud Guerquin

Trigger end. Test needed

parent 868f22c7
......@@ -17,7 +17,6 @@ public class CommonCharacterIDListFilter {
* @return A CharacterIDListFilter that only remove elements equal to characterIDToIgnore
*/
static UnaryOperator<List<Integer>> allExceptOne(int characterIDToIgnore) {
return charaIDList -> charaIDList.stream().filter(integer -> (integer!=characterIDToIgnore)).collect(Collectors.toList());
return charaIDList -> {charaIDList.removeIf(x->(x==characterIDToIgnore));return charaIDList;};
}
}
package core.event;
import core.gamestate.Entity;
import core.gamestate.GameContent;
import core.gamestate.Trigger;
public class AddedTriggerEvent implements ClientEvent{
private static final long serialVersionUID = 1L;
int targetID;
Trigger trigger;
@Override
public boolean execute(GameContent gameContent) {
Entity e=gameContent.getGameState().getEntity(targetID);
if(e==null)
return false;
if(e.addTriggers(trigger)){
gameContent.addTriggerToCheck(e);
}
return true;
}
@Override
public Event resolve(GameContent gameContent) {
return EmptyEvent.empty;
}
}
package core.event;
import core.gamestate.Entity;
import core.gamestate.GameContent;
public class RemovedTriggerEvent implements ClientEvent{
private static final long serialVersionUID = 1L;
int targetID;
int triggerID;
@Override
public boolean execute(GameContent gameContent) {
Entity e=gameContent.getGameState().getEntity(targetID);
if(e==null)
return false;
if(e.removeTriggers(triggerID)){
gameContent.removeTriggerToCheck(e);
}
return true;
}
@Override
public Event resolve(GameContent gameContent) {
return EmptyEvent.empty;
}
}
package core.event;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import core.abilities.effects.Effect;
import core.gamestate.Character;
import core.gamestate.GameContent;
import core.gamestate.Trigger;
public class TriggerEvent implements ClientEvent {
/**
*
*/
private static final long serialVersionUID = 1L;
private Trigger trigger;
private int casterID;
public TriggerEvent(Trigger trigger, int casterID) {
this.trigger=Objects.requireNonNull(trigger);
this.casterID=casterID;
}
@Override
public boolean execute(GameContent gameContent) {
List<Character> list=gameContent.getGameState().getCharacterWhoseHitboxIntersectsWith(trigger.getEffectZone());
list.removeIf(e->e.getID()==casterID);
if(!list.isEmpty()){
ArrayList<Integer> targets=new ArrayList<>(list.size());
for(Character c:list){
targets.add(c.getID());
}
for(Effect effect:trigger.getEffectList()){
effect.effect(targets, gameContent, casterID);
}
}
return true;
}
@Override
public Event resolve(GameContent gameContent) {
return EmptyEvent.empty;
}
}
......@@ -5,6 +5,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import core.abilities.effects.CommonCharacterIDListFilter;
import core.abilities.effects.Effect;
import core.event.TriggerEvent;
import core.zone.Direction;
import core.zone.Point;
import core.zone.Translation;
......@@ -14,6 +17,8 @@ import graphics.guiSkeleton.entityDisplayer.EntityDisplayerProvider;
import graphics.guiSkeleton.entityDisplayer.EntityDisplayerType;
import map_generation.map.Map;
import map_generation.tiles.TilePropertyVector;
import network.NetworkConnection;
import network.NetworkObject;
/**
* This interface is used to define one object in the GameState.
......@@ -224,11 +229,13 @@ public class Entity implements Serializable{
}
public boolean addTriggers(Trigger trigger){
return triggers.add(trigger);
triggers.add(trigger);
return triggers.size()==1;
}
public boolean removeTriggers(Trigger trigger){
return triggers.remove(trigger);
public boolean removeTriggers(int triggerID){
triggers.removeIf(x->x.id==triggerID);
return triggers.size()==0;
}
/**
......@@ -328,6 +335,26 @@ public class Entity implements Serializable{
boolean hackGamestate(GameState gameState) {
throw new IllegalStateException("The gamestate doesn't know the given subclass");
}
public void checkTrigger(GameContent gameContent, NetworkConnection<NetworkObject> networkConnection) {
for(Trigger trigger:triggers){
List<Character> list=gameContent.getGameState().getCharacterWhoseHitboxIntersectsWith(trigger.getEffectZone());
list.removeIf(e->e.getID()==ID);
if(!list.isEmpty()){
ArrayList<Integer> targets=new ArrayList<>(list.size());
for(Character c:list){
targets.add(c.getID());
}
list.clear();
networkConnection.sendEvent(new TriggerEvent(trigger,ID));
for(Effect effect:trigger.getEffectList()){
effect.effect(targets, gameContent, ID);
}
targets.clear();
}
}
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import core.zone.Point;
import core.zone.Zone;
import graphics.guiSkeleton.entityDisplayer.EntityDisplayerType;
import map_generation.tiles.TilePropertyVector;
......@@ -57,4 +58,9 @@ public class EntitySpecies{
return new Entity(name,this.name,posX, posY, id,owner, collisionBox, tpv, visibility, entityDisplayerType,triggers);
}
//TODO change this
public boolean canSpawn(Point p) {
return true;
}
}
package core.gamestate;
import map_generation.map.Map;
import network.NetworkConnection;
import network.NetworkObject;
import java.io.Serializable;
import java.util.ArrayList;
......@@ -25,6 +27,7 @@ public class GameContent implements Serializable{
private Map map;
private GameState gameState;
private ArrayList<Character> players;
private ArrayList<Entity> triggerEntities=new ArrayList<>();
public GameContent(Map map,int playersNumber) {
this.map=Objects.requireNonNull(map);
this.gameState=new GameState();
......@@ -92,4 +95,19 @@ public class GameContent implements Serializable{
return players.get(i);
}
public void applyTrigger(NetworkConnection<NetworkObject> networkConnection) {
for(Entity e:triggerEntities){
e.checkTrigger(this,networkConnection);
}
}
public void addTriggerToCheck(Entity e) {
triggerEntities.add(e);
}
public void removeTriggerToCheck(Entity e) {
triggerEntities.remove(e);
}
}
......@@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.Objects;
import assets.UsedForLoadingSprites;
import core.zone.Point;
public class SpeciesArray {
static final HashMap<String,EntitySpecies> array=new HashMap<>();
......@@ -85,4 +86,9 @@ public class SpeciesArray {
throw new IllegalStateException("Can't initialise species." + System.lineSeparator()+
"Line"+lineError + message);
}
public static boolean canSpawn(Point p,String speciesName){
EntitySpecies species=array.get(speciesName);
return species!=null && species.canSpawn(p);
}
}
......@@ -11,12 +11,17 @@ import core.zone.Zone;
public class Trigger {
private Supplier<Zone> mappingFunction;
private ArrayList<Effect> effectList;
int id;
public Trigger(Supplier<Zone> mappingFunction, ArrayList<Effect> effectList) {
this.mappingFunction=mappingFunction;
this.effectList=effectList;
id=0;
}
public void setID(int id){
this.id=id;
}
static public Trigger construct(Entity entity,TriggerDescriptor triggerDescriptor){
ArrayList<Effect> effectList=new ArrayList<>();
for(EffectDescriptor e: triggerDescriptor.effects){
......@@ -33,4 +38,17 @@ public class Trigger {
public ArrayList<Effect> getEffectList(){
return effectList;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Trigger))
return false;
Trigger t=(Trigger)obj;
return t.id==id;
}
@Override
public int hashCode() {
return id;
}
}
......@@ -2,7 +2,6 @@ package core.zone;
import java.io.Serializable;
import graphics.graphical_utility.GraphicalBridgeConfiguration;
import map_generation.map.Map;
import map_generation.map.MapPoint;
import map_generation.tiles.TilePropertyVector;
......
package gameloop;
import artificial_intelligence.AIControler;
import core.event.EmptyEvent;
import core.event.Event;
import core.event.MapInit;
import core.gamestate.*;
......@@ -104,6 +105,7 @@ public class ServerLoop extends Thread {
event.apply(gameContent);
networkConnection.sendEvent(event);
}
gameContent.applyTrigger(networkConnection);
}
......
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