2018-08-28 06:13:14 +00:00
|
|
|
package com.seodisparate.TurnBasedMinecraft.common;
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.Instant;
|
2018-08-29 06:09:44 +00:00
|
|
|
import java.util.ArrayList;
|
2018-08-28 06:13:14 +00:00
|
|
|
import java.util.Collection;
|
2018-08-29 06:09:44 +00:00
|
|
|
import java.util.HashMap;
|
2018-08-28 06:13:14 +00:00
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2018-08-29 06:09:44 +00:00
|
|
|
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
|
|
|
|
|
2018-08-28 06:13:14 +00:00
|
|
|
import net.minecraft.entity.Entity;
|
2018-08-29 06:09:44 +00:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2018-08-28 06:13:14 +00:00
|
|
|
|
|
|
|
public class Battle
|
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
private final int id;
|
|
|
|
private Map<Integer, Combatant> sideA;
|
|
|
|
private Map<Integer, Combatant> sideB;
|
2018-08-28 06:13:14 +00:00
|
|
|
|
|
|
|
private Instant lastUpdated;
|
2018-08-29 06:09:44 +00:00
|
|
|
private State state;
|
|
|
|
private int playerCount;
|
|
|
|
private int undecidedCount;
|
|
|
|
private Duration timer;
|
|
|
|
|
|
|
|
public enum State
|
|
|
|
{
|
|
|
|
DECISION,
|
|
|
|
ATTACK,
|
|
|
|
HEALTH_CHECK
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum Decision
|
|
|
|
{
|
|
|
|
UNDECIDED(0),
|
|
|
|
ATTACK(1),
|
|
|
|
DEFEND(2),
|
|
|
|
FLEE(3),
|
|
|
|
USE_ITEM(4);
|
|
|
|
|
|
|
|
private int value;
|
|
|
|
private static Map<Integer, Decision> map = new HashMap<Integer, Decision>();
|
|
|
|
|
|
|
|
private Decision(int value)
|
|
|
|
{
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getValue()
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
|
|
|
for(Decision decision : Decision.values())
|
|
|
|
{
|
|
|
|
map.put(decision.value, decision);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Decision valueOf(int decisionType)
|
|
|
|
{
|
|
|
|
return map.get(decisionType);
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
|
|
|
|
public Battle(int id, Collection<Entity> sideA, Collection<Entity> sideB)
|
|
|
|
{
|
|
|
|
this.id = id;
|
2018-08-29 06:09:44 +00:00
|
|
|
this.sideA = new Hashtable<Integer, Combatant>();
|
|
|
|
this.sideB = new Hashtable<Integer, Combatant>();
|
|
|
|
playerCount = 0;
|
|
|
|
if(sideA != null)
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
for(Entity e : sideA)
|
|
|
|
{
|
|
|
|
this.sideA.put(e.getEntityId(), new Combatant(e));
|
|
|
|
if(e instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
++playerCount;
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
2018-08-29 06:09:44 +00:00
|
|
|
if(sideB != null)
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
for(Entity e : sideB)
|
|
|
|
{
|
|
|
|
this.sideB.put(e.getEntityId(), new Combatant(e));
|
|
|
|
if(e instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
++playerCount;
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastUpdated = null;
|
2018-08-29 06:09:44 +00:00
|
|
|
state = State.DECISION;
|
|
|
|
undecidedCount = playerCount;
|
|
|
|
timer = TurnBasedMinecraftMod.BattleDecisionTime;
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getId()
|
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasCombatant(int entityID)
|
|
|
|
{
|
|
|
|
return sideA.containsKey(entityID) || sideB.containsKey(entityID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasCombatantInSideA(int entityID)
|
|
|
|
{
|
|
|
|
return sideA.containsKey(entityID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addCombatantToSideA(Entity e)
|
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
sideA.put(e.getEntityId(), new Combatant(e));
|
|
|
|
if(e instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
++playerCount;
|
|
|
|
if(state == State.DECISION)
|
|
|
|
{
|
|
|
|
++undecidedCount;
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addCombatantToSideB(Entity e)
|
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
sideB.put(e.getEntityId(), new Combatant(e));
|
|
|
|
if(e instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
++playerCount;
|
|
|
|
if(state == State.DECISION)
|
|
|
|
{
|
|
|
|
++undecidedCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearCombatants()
|
|
|
|
{
|
|
|
|
sideA.clear();
|
|
|
|
sideB.clear();
|
|
|
|
playerCount = 0;
|
|
|
|
undecidedCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Combatant> getSideA()
|
|
|
|
{
|
|
|
|
return sideA.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Combatant> getSideB()
|
|
|
|
{
|
|
|
|
return sideB.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Integer> getSideAIDs()
|
|
|
|
{
|
|
|
|
Collection<Integer> sideAIDs = new ArrayList<Integer>(sideA.size());
|
|
|
|
for(Combatant combatant : sideA.values())
|
|
|
|
{
|
|
|
|
sideAIDs.add(combatant.entity.getEntityId());
|
|
|
|
}
|
|
|
|
return sideAIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Integer> getSideBIDs()
|
|
|
|
{
|
|
|
|
Collection<Integer> sideBIDs = new ArrayList<Integer>(sideB.size());
|
|
|
|
for(Combatant combatant : sideB.values())
|
|
|
|
{
|
|
|
|
sideBIDs.add(combatant.entity.getEntityId());
|
|
|
|
}
|
|
|
|
return sideBIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Combatant getCombatantByID(int entityID)
|
|
|
|
{
|
|
|
|
Combatant combatant = sideA.get(entityID);
|
|
|
|
if(combatant == null)
|
|
|
|
{
|
|
|
|
combatant = sideB.get(entityID);
|
|
|
|
}
|
|
|
|
return combatant;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDecision(int entityID, Decision decision)
|
|
|
|
{
|
|
|
|
if(state != State.DECISION)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Combatant combatant = sideA.get(entityID);
|
|
|
|
if(combatant == null)
|
|
|
|
{
|
|
|
|
combatant = sideB.get(entityID);
|
|
|
|
if(combatant == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(combatant.entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
combatant.decision = decision;
|
|
|
|
--undecidedCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public State getState()
|
|
|
|
{
|
|
|
|
return state;
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void update()
|
|
|
|
{
|
|
|
|
if(lastUpdated == null)
|
|
|
|
{
|
|
|
|
lastUpdated = Instant.now();
|
|
|
|
update(Duration.ZERO);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Instant now = Instant.now();
|
|
|
|
update(Duration.between(lastUpdated, now));
|
|
|
|
lastUpdated = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 06:09:44 +00:00
|
|
|
private void update(final Duration dt)
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case DECISION:
|
|
|
|
timer = timer.minus(dt);
|
|
|
|
if(timer.isNegative() || timer.isZero())
|
|
|
|
{
|
|
|
|
state = State.ATTACK;
|
|
|
|
timer = TurnBasedMinecraftMod.BattleDecisionTime;
|
|
|
|
update(Duration.ZERO);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATTACK:
|
|
|
|
// TODO
|
|
|
|
break;
|
|
|
|
case HEALTH_CHECK:
|
|
|
|
// TODO
|
|
|
|
break;
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
}
|