Commit 00834334 authored by bogdanbear's avatar bogdanbear

Relayers and Entities do not access the network directly, they speak with the...

Relayers and Entities do not access the network directly, they speak with the DummyGameLoop + Removed two useless methods in DummyGameloop
parent cc3d6281
......@@ -232,7 +232,7 @@ public class EffectBuilder implements Serializable{
}
if(hit){
Event event = new AbilityEvent(casterCharacterID, 0);
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
DummyLocalGameLoop.getInstance().sendEvent(event);
}
}
};
......@@ -283,7 +283,7 @@ public class EffectBuilder implements Serializable{
}
if(hit){
Event event = new AbilityEvent(casterCharacterID, 0);
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
DummyLocalGameLoop.getInstance().sendEvent(event);
}
}
};
......
......@@ -37,7 +37,7 @@ public class MoveEvent implements ClientEvent,ServerEvent {
}
else if(target.getSpeciesName().equals("FrostBolt") || target.getSpeciesName().equals("FireBoltLP")){
Event event = new AbilityEvent(targetID, 0);
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
DummyLocalGameLoop.getInstance().sendEvent(event);
}
return true;
} catch (EntityNotFoundExeption entityNotFoundExeption) {
......
......@@ -149,20 +149,19 @@ public class Being extends Entity {
if (HP==0){
Event event = new ToServerDeathEvent(this.getID());
Logger LOGGER = Logging.getInstance().getLogger();
NetworkConnection network = DummyLocalGameLoop.getInstance().getNetworkConnection();
if (network==null){
if (DummyLocalGameLoop.getInstance().networkConnectionIsNull()){
LOGGER.severe("NetworkConnection null, Being"+this.getID()+" can't die");
} else {
LOGGER.info("Death of "+this.getID()+" is send");
network.sendEvent(event);
DummyLocalGameLoop.getInstance().sendEvent(event);
}
Event event2 = new SpawnGold(this.getID());
if (network==null){
if (DummyLocalGameLoop.getInstance().networkConnectionIsNull()){
LOGGER.severe("NetworkConnection null, GOld"+this.getID()+" can't be created");
} else {
LOGGER.info("Gold creation by " + this.getID() + " is send");
network.sendEvent(event2);
DummyLocalGameLoop.getInstance().sendEvent(event2);
}
}
......
......@@ -74,7 +74,7 @@ public class Relayer extends Thread{
return;
}
Event event = new MoveEvent(character.getID(), Translation.construct(direction,character.getSpeed()));
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
DummyLocalGameLoop.getInstance().sendEvent(event);
}
private void stopMove(){
......@@ -95,7 +95,7 @@ public class Relayer extends Thread{
*/
private void applyAbility() {
Event event = new AbilityEvent(character.getID(), currentAbility);
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
DummyLocalGameLoop.getInstance().sendEvent(event);
hasTriedToCastAbility = false;
}
......
......@@ -40,7 +40,7 @@ public class RelayerEntity extends Thread{
return;
}
Event event = new MoveEvent(character.getID(), Translation.construct(direction,character.getSpeed()));
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
DummyLocalGameLoop.getInstance().sendEvent(event);
}
private void stopMove(){
......
......@@ -10,6 +10,7 @@ import core.relayer.Relayers;
import graphics.graphical_abstraction.GraphicsMasterAbstraction;
import logging.Logging;
import network.helpers.EndEvent;
import network.helpers.NetworkObject;
import network.inner_shell.NetworkConnection;
......@@ -18,7 +19,7 @@ import java.util.logging.Logger;
/**
* Created by bogdanbear on 22/11/2015.
Singleton class for the DummyLocalGameLoop
Singleton class for the DummyLocalGameLoop
*/
public class DummyLocalGameLoop extends Thread{
private NetworkConnection networkConnection;
......@@ -108,7 +109,7 @@ public class DummyLocalGameLoop extends Thread{
}
//default visibility for the getter, should be inaccessible to entities etc.
public synchronized NetworkConnection getNetworkConnection()
synchronized NetworkConnection getNetworkConnection()
{
return networkConnection;
}
......@@ -141,10 +142,10 @@ public class DummyLocalGameLoop extends Thread{
while (!((eventToReceive = (Event) getNetworkConnection().receiveEvent(true)) instanceof EndEvent)) {
//System.out.println("DummyLocalGameLoop Thread: We have received an event!applying...+"+eventToReceive);
try {
eventToReceive.apply(gameContent);
} catch (InterruptedException e) {
e.printStackTrace();
}
eventToReceive.apply(gameContent);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (eventToReceive instanceof MapInit) {
GameContent gc = DummyLocalGameLoop.getInstance().getContent();
Relayer firstEntityRelayer = Relayers.addFollowedRelayer(((MapInit) eventToReceive).getFollowedDummyEntity());
......@@ -152,7 +153,7 @@ public class DummyLocalGameLoop extends Thread{
relayerSemaphore.release();
}
if(eventToReceive instanceof MapChangeEvent){
GraphicsMasterAbstraction.getInstance().changeGUIStateTo(GraphicsMasterAbstraction.GUIStates.GAME_WIN);
GraphicsMasterAbstraction.getInstance().changeGUIStateTo(GraphicsMasterAbstraction.GUIStates.GAME_WIN);
}
if (eventToReceive instanceof ToClientDeathEvent && ((ToClientDeathEvent) eventToReceive).getEntityID()==followedRelayer.getCharacter().getID()) {
......@@ -163,21 +164,33 @@ public class DummyLocalGameLoop extends Thread{
LOGGER.info("End Event Received!");
}
else {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public synchronized boolean isShouldRPC() {
return shouldRPC;
/**
* Entities, relayers and objects related to the GameContent should not have a direct reference to the network connection
* this is why I insert here a sendEvent method
*/
public void sendEvent(NetworkObject event)
{
getNetworkConnection().sendEvent(event);
}
public synchronized void setShouldRPC(boolean shouldRPC) {
this.shouldRPC = shouldRPC;
/**
* for compatibility reasons when changing visibility of getNetworkConnection(),
* we need to implement the following function
* **/
public boolean networkConnectionIsNull()
{
return(getNetworkConnection()==null);
}
}
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