public FollowPath(AgentUnit agent, Vector3[] path, FollowT type, Action <bool> callback) : base(agent, callback) { empty = new GameObject(); if (agent == null) { Debug.Log(agent); Debug.LogError("Follow path en unidad muerta "); } else { empty.transform.parent = agent.gameObject.transform; } if (type == FollowT.STAY) { pathF = empty.AddComponent <PathFollowingAdvanced>(); } else { pathF = empty.AddComponent <PathFollowing>(); pathF.type = type; } pathF.SetNPC(agent); pathF.SetPath(path); pathF.visibleRays = true; pathF.maxAccel = 700f; }
public void ComputeInfluenceDijkstra(AgentUnit unit, Vector2[,] influenceMap) { Node startNode = Map.NodeFromPosition(unit.position); startNode.gCost = 1; Heap <Node> openSet = new Heap <Node>(Map.GetMaxSize()); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); HashSet <Node> toReset = new HashSet <Node>(); while (openSet.Count > 0) { Node currentNode = openSet.Pop(); closedSet.Add(currentNode); toReset.Add(currentNode); currentNode.SetInfluence(unit.faction, unit.GetDropOff(currentNode.gCost), influenceMap, InfluenceT.ACCUMULATE); if (closedSet.Count > NNodesInfluenceMap) { break; } foreach (Node neighbour in Map.GetNeighbours(currentNode)) { if (!neighbour.isWalkable() || closedSet.Contains(neighbour)) { continue; } //This penaly for the terrain is based on the idea that if you move from road to forest is slower than from forest to road float newMovementCostToNeighbour = currentNode.gCost + PathUtil.realDist(currentNode, neighbour) * unit.Cost[neighbour.type]; if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { toReset.Add(neighbour); neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = 0; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } foreach (var node in toReset) { node.gCost = 0; node.hCost = 0; } }
public void RemoveUnitFromSchedulers(AgentUnit unit) { foreach (SchedulerStrategy sched in strategySchedulers.Values) { sched.RemoveUnit(unit); } }
public void RemoveUnit(AgentUnit unit) { base.RemoveUnit(unit); heal.Remove(unit); regr.Remove(unit); atking.Remove(unit); }
public void SetSprite(WorkerModel model) { AgentAnim anim = null; if (model is AgentModel) { AgentUnit unit = AgentLayer.currentLayer.GetAgent(model.instanceId); anim = unit.animTarget; this.Symbol.gameObject.SetActive(true); } else if (model is OfficerModel) { OfficerUnit unit = OfficerLayer.currentLayer.GetOfficer(model.instanceId); anim = unit.animTarget; this.Symbol.gameObject.SetActive(false); } this.Face.sprite = anim.face.sprite; this.Hair.sprite = anim.hair.sprite; this.Body.sprite = anim.body.sprite; this.LeftDownArm.sprite = anim.B_low_arm.sprite; this.LeftUpArm.sprite = anim.B_up_arm.sprite; this.RightDownArm.sprite = anim.F_low_arm.sprite; this.RightUpArm.sprite = anim.F_up_arm.sprite; this.LeftDownLeg.sprite = anim.B_low_leg.sprite; this.LeftUpLeg.sprite = anim.B_up_leg.sprite; this.RightDownLeg.sprite = anim.F_low_leg.sprite; this.RightUpLeg.sprite = anim.F_up_leg.sprite; }
public void Attack(AgentUnit unit) { float damage; if (Random.Range(0, 100) > 99f) { damage = attack * 5; } else { damage = attack * Random.Range(0.8f, 1.2f) * AgentUnit.atkTable[(int)agent.GetUnitType(), (int)unit.GetUnitType()]; } if (Map.projectile != null) { var newProjectile = GameObject.Instantiate(Map.projectile); newProjectile.transform.position = agent.position; newProjectile.GetComponent <Projectile>().SetTarget(unit); } else { Debug.Log("Falta el prefab del projectil para atacar"); } unit.militar.ReceiveAttack(agent, (int)Mathf.Round(damage)); }
public static List <HashSet <AgentUnit> > GetClusters(Faction unitsFaction, Faction baseFaction) { List <HashSet <AgentUnit> > clusters = new List <HashSet <AgentUnit> >(); var units = GetUnitsFactionArea(GetWaypoint("base", baseFaction), 45f, unitsFaction); while (units.Count > 0) { HashSet <AgentUnit> cluster = new HashSet <AgentUnit>(); Stack <AgentUnit> neighbours = new Stack <AgentUnit>(); clusters.Add(cluster); neighbours.Push(units.First()); cluster.Add(units.First()); units.Remove(units.First()); while (neighbours.Count > 0) { AgentUnit currentUnit = neighbours.Pop(); var nearUnits = GetUnitsFactionArea(currentUnit.position, 4f, unitsFaction) .Where(unit => !clusters.Any(c => c.Contains(unit))); foreach (AgentUnit nearUnit in nearUnits) { Debug.DrawLine(currentUnit.position, nearUnit.position); neighbours.Push(nearUnit); units.Remove(nearUnit); cluster.Add(nearUnit); } } } return(clusters); }
public void ComputeInfluenceBFS(AgentUnit unit, Vector2[,] influenceMap, UnitT targetType = UnitT.MELEE, bool considerUnit = false) { HashSet <Node> pending = new HashSet <Node>(); HashSet <Node> visited = new HashSet <Node>(); Node vert = Map.NodeFromPosition(unit.position); pending.Add(vert); // BFS for assigning influence for (int i = 1; i <= RadiusInfluenceMap; i++) { HashSet <Node> frontier = new HashSet <Node>(); foreach (Node p in pending) { if (visited.Contains(p)) { continue; } visited.Add(p); if (!considerUnit) { p.SetInfluence(unit.faction, unit.GetDropOff(1 + Util.HorizontalDist(p.worldPosition, unit.position)), influenceMap, InfluenceT.MAXIMUM); } else { float atkFactor = AgentUnit.atkTable[(int)unit.GetUnitType(), (int)targetType]; p.SetInfluence(unit.faction, atkFactor * unit.GetDropOff(1 + Util.HorizontalDist(p.worldPosition, unit.position)), influenceMap, InfluenceT.MAXIMUM); } frontier.UnionWith(Map.GetDirectNeighbours(p)); } pending = new HashSet <Node>(frontier); } }
public override Steering Apply() { if (IsFinished()) { callback(true); } Steering st = new Steering(); //Comprobar si se ha matado a la unidad if (attack == null || Util.HorizontalDist(targetEnemy.position, center) > rangeRadius + agent.militar.attackRange + followRangeExtra) { AgentUnit closerEnemy = Info.GetUnitsFactionArea(center, rangeRadius + agent.militar.attackRange, Util.OppositeFaction(agent.faction)) .OrderBy(enemy => Util.HorizontalDist(agent.position, enemy.position)) .FirstOrDefault(); AttackEnemy(closerEnemy); } SetCenter(agent.position); if (attack != null) { st = attack.Apply(); } else { st = followPath.Apply(); } return(st); }
private void OnTriggerEnter(Collider other) { AgentUnit unit = other.GetComponent <AgentUnit>(); if (unit != null) { units.Add(unit); } }
private void OnTriggerExit(Collider other) { AgentUnit unit = other.GetComponent <AgentUnit>(); if (unit != null) { units.Remove(unit); } }
public void SetTarget(AgentUnit target) { velocity = Mathf.Max(3f, Util.HorizontalDist(transform.position, target.position) / 0.7f); this.target = target; transform.LookAt(target.position); transform.Rotate(new Vector3(90, 180, 0)); endTime = Time.fixedTime + Util.HorizontalDist(transform.position, target.position) / velocity; }
public Patrol(AgentUnit agent, Vector3[] path, float rangeRadius, Action <bool> callback) : base(agent, callback) { Debug.Assert(followRangeExtra >= 1f); this.rangeRadius = rangeRadius; targetEnemy = null; attack = null; followPath = new FollowPath(agent, path, FollowT.LOOP, (_) => {}); SetCenter(agent.position); }
public GoToAggresive(AgentUnit agent, Vector3 target, float rangeRadius, Action <bool> callback) : base(agent, callback) { this.target = target; this.defendZone = new DefendZone(agent, agent.position, rangeRadius, (_) => { }); this.fighting = false; this.goTo = new GoTo(agent, target, (bool result) => { goTo.Terminate(); goTo = null; }); }
public RestoreHealth(AgentUnit agent, Action <bool> callback) : base(agent, callback) { healingPoint = Info.GetClosestHealingPoint(agent.position, 150f).position; this.goTo = new GoTo(agent, healingPoint, Mathf.Infinity, 0, true, (bool success) => { goTo.Terminate(); goTo = null; defendZone = new DefendZone(agent, agent.position, 2f, (_) => { }); }); }
public void ReceiveAttack(AgentUnit enemy) { //It would be uncommon that targetEnemy is null unless that the range of the enemy is greater than the range of defend zone bool inRange = Util.HorizontalDist(targetEnemy.position, center) /* + Attack range*/ > rangeRadius; //TODO Aqui hay un NullReference bool targetNear = Util.HorizontalDist(targetEnemy.position, agent.position) < 3f /*+ AttackRange*/; if (targetEnemy == null || (inRange && !targetNear)) { AttackEnemy(enemy); } }
public void GenerateUnit() { if (Map.GetAllies(faction).Count < Map.maxUnits && gold >= 50) { gold -= 50; GameObject created = GameObject.Instantiate(units[unitToGenerate], (Info.GetWaypoint("recruit", faction) + new Vector3(0, 0.75f, 0)), Quaternion.identity) as GameObject; AgentUnit newUnit = created.GetComponent <AgentUnit>(); Map.unitList.Add(newUnit); Debug.Log("Generada una unidad de " + unitToGenerate); } }
public void RemoveUnit(AgentUnit unit, bool updateText = true) { selectedUnits.Remove(unit); Destroy(unit.selectCircle); unit.selectCircle = null; if (updateText) { UpdateSelectionText(); } }
public static Dictionary <StrategyT, float> GetStrategyPriority(AgentUnit unit, Faction faction) { return(new Dictionary <StrategyT, float> { { StrategyT.ATK_BASE, Util.HorizontalDist(unit.position, GetWaypoint("base", Util.OppositeFaction(faction))) }, { StrategyT.ATK_HALF, Util.HorizontalDist(unit.position, GetWaypoint("front", Util.OppositeFaction(faction))) }, { StrategyT.DEF_HALF, Util.HorizontalDist(unit.position, GetWaypoint("front", faction)) }, { StrategyT.DEF_BASE, Util.HorizontalDist(unit.position, GetWaypoint("base", faction)) } }); }
void GenerateUnit(UnitT type) { GameObject created = GameObject.Instantiate(units[type], (Info.GetWaypoint("recruit", faction) + new Vector3(0, 0.75f, 0)), Quaternion.identity) as GameObject; // TODO Cambiarlo por un waypoint AgentUnit newUnit = created.GetComponent <AgentUnit>(); newUnit.transform.parent = transform.parent; newUnit.gameObject.name += "" + Time.frameCount; newUnit.Start(); Map.unitList.Add(newUnit); //Debug.Log ("Generada una unidad de " + type); stratManager.CycleLayer12(); }
public void AttackEnemy(AgentUnit enemy) { ResetTask(); SetTask(new Attack(this, enemy, (bool sucess) => { Debug.Log("Task finished"); ResetTask(); })); /* SetTask(new DefendZone(this, position, 6f, (bool sucess) => { * Debug.Log("Defend finished"); * RemoveTask(); * }));*/ }
public void AddUnit(AgentUnit unit, bool updateText = true) { selectedUnits.Add(unit); if (unit.selectCircle == null) { unit.selectCircle = Instantiate(prefabSelectCircle, unit.transform); unit.selectCircle.transform.position = new Vector3(unit.transform.position.x, 0.1f, unit.transform.position.z); } if (updateText) { UpdateSelectionText(); } }
public DefendZone(AgentUnit agent, Vector3 center, float rangeRadius, Action <bool> callback) : base(agent, callback) { Debug.Assert(followRangeExtra >= 1f); this.center = center; this.rangeRadius = rangeRadius; targetEnemy = null; attack = null; goTo = new GoTo(agent, center, Mathf.Infinity, rangeRadius / 3, false, (_) => { returning = false; goTo.SetVisiblePath(false); agent.RequestStopMoving(); }); //The offset should be smaller than the distance when it is consider far goTo.SetVisiblePath(false); returning = false; }
public GoTo(AgentUnit agent, Vector3 target, float reconsiderSeconds, float offset, bool defensive, Action <bool> callback) : base(agent, callback) { this.offset = offset; this.defensive = defensive; this.reconsiderSeconds = Mathf.Infinity;//reconsiderSeconds; followPath = new FollowPath(agent, null, (_) => { finished = true; }); SetNewTarget(target); n++; //Debug.Log("numero gotos " + n); }
public GoToLRTA(AgentUnit agent, Vector3 target, Action <bool> callback) : base(agent, callback) { this.target = target; empty = new GameObject(); empty.transform.parent = agent.gameObject.transform; pathF = empty.AddComponent <PathFollowing>(); pathF.SetNPC(agent); pathF.path = null; pathF.visibleRays = true; pathF.maxAccel = 50f; lrta = new LRTA(); lrta.StartPath(target, agent.Cost); RequestPath(); }
public Follow(AgentUnit agent, AgentUnit target, Action <bool> callback) : base(agent, callback) { this.target = target; //this.lastTargetPosition = target.position; Debug.Assert(Map.NodeFromPosition(target.position).isWalkable()); this.goTo = new GoTo(agent, target.position /*GetFutureTargetPosition()*/, (_) => { if (IsNearEnough()) { SetInRange(); } else { ReconsiderPath(); } }); inRange = IsNearEnough(); }
void AttackEnemy(AgentUnit newEnemy) { if (attack != null) { attack.Terminate(); attack = null; } if (newEnemy != null) { targetEnemy = newEnemy; attack = new Attack(agent, targetEnemy, (_) => { attack.Terminate(); attack = null; }); } }
public static SuppressWindow CreateWindow(AgentModel target) { if (currentWindow.gameObject.activeSelf) { if (currentWindow.target == target) { return(currentWindow); } } else { currentWindow.gameObject.SetActive(true); currentWindow.line.gameObject.SetActive(true); currentWindow.Activate(); } SuppressWindow inst = currentWindow; inst.target = target; inst.targetType = TargetType.AGENT; if (inst.currentSefira != SefiraManager.instance.GetSefira(target.currentSefira)) { inst.currentSefira = SefiraManager.instance.GetSefira(target.currentSefira); inst.agentList.Clear(); inst.SetSprites(inst.currentSefira); } inst.InitAgentList(); inst.ShowAgentList(); //inst.currentSefira = SefiraManager.instance.getSefira("1"); AgentUnit unit = AgentLayer.currentLayer.GetAgent(target.instanceId); inst.attachedPos = unit.transform; inst.ui.Init(target, inst.targetType); Canvas canvas = inst.transform.GetChild(0).GetComponent <Canvas>(); canvas.worldCamera = UIActivateManager.instance.GetCam(); currentWindow = inst; return(inst); }
public float ReceiveAttack(AgentUnit enemy, int amount) { int damage = Mathf.Max(0, amount - defense); Console.Log("Unit caused " + damage + " damage"); health = Mathf.Max(0, health - damage); if (IsDead()) { Console.Log("Unit died"); agent.gameObject.layer = 0; } else if (agent.HasTask <HostileTask>()) //To change the target if needed { agent.StartCoroutine(BlinkMesh()); ((HostileTask)agent.GetTask()).ReceiveAttack(enemy); } //Request to update selection text return(damage); }
void AttackEnemy(AgentUnit newEnemy) { if (attack != null) { attack.Terminate(); attack = null; } if (newEnemy != null) { Debug.Log("Found enemy " + newEnemy.name + " distance " + Util.HorizontalDist(newEnemy.position, agent.position) + " by " + agent.name); targetEnemy = newEnemy; returning = false; goTo.SetVisiblePath(false); attack = new Attack(agent, targetEnemy, (_) => { attack.Terminate(); attack = null; }); } }