public static AttackingEntity.AttackResult Fight(SynchronizedActor actor, SynchronizedActor target) { Debug.Log("Begin Attack: " + actor.Stats.Name + " vs. " + target.Stats.Name); Entity tentity = target.Entity; int x, y; tentity.GetPosition(out x, out y); AttackingEntity.AttackResult ares = actor.AttackingEntity.Attack(x, y); if (ares != null && (ares.Result == AttackingEntity.AttackResult.ResultValue.Hit)) { int dmg = ares.Weapon.GetDamage(); tentity.Stats.Health -= dmg; tentity.Actor.Moan(dmg); } return(ares); }
/// <summary> /// Checks if theres anyone nearby within melee range and attacks. Return true if attack was made. /// Ranged attack not supported /// </summary> /// <returns></returns> private bool doAttack() { World.Grid grid = World.Instance.GetGrid(); int x, y; m_actor.Entity.GetPosition(out x, out y); World.Cell myCell = grid.GetCell(x, y); World.Cell[] neighbours = grid.GetNeighbours4(myCell); List <SynchronizedActor> pTargets = new List <SynchronizedActor>(); foreach (World.Cell cell in neighbours) { if (cell.ContainsEntity) { Entity e = cell.GetEntity(); if (e.GetType() == typeof(Player)) { pTargets.Add(e.Actor); break; } else if (UnityEngine.Random.value < Constants.FRIENDLY_FIRE_CHANCE) { pTargets.Add(e.Actor); } } } if (pTargets.Count > 0) { SynchronizedActor target = pTargets[UnityEngine.Random.Range(0, pTargets.Count)]; AttackingEntity.AttackResult result = CombatSolver.Fight(m_actor, target); if (result.Result == AttackingEntity.AttackResult.ResultValue.Hit || result.Result == AttackingEntity.AttackResult.ResultValue.Miss) { Synchronizer.Continue(m_actor, result.Weapon.TimeCost); } return(true); } return(false); }
// Update is called once per frame void Update() { if (m_myTurn) { World.Cell mouseOverCell = GetMouseOverCell(); if (mouseOverCell != null && mouseOverCell.ContainsEntity) { ApplyLOS(); int x, y; Entity ent = mouseOverCell.GetEntity(); ent.GetPosition(out x, out y); if (!"Player".Equals(ent.GetType().ToString()) && ent.GetCell().IsVisible()) { SetCrosshair(x, y); } else { HideCrosshair(); } if (Input.GetMouseButtonDown(0)) { AttackingEntity.AttackResult res = CombatSolver.Fight(m_actor, mouseOverCell.GetEntity().Actor); if (res != null && res.Weapon != null) { Synchronizer.Continue(m_actor, res.Weapon.TimeCost); return; } else if (res.Result == AttackingEntity.AttackResult.ResultValue.NoEnergy) { Debug.Log(res.Weapon.Name + " Out of energy"); } } } else { HideCrosshair(); } Vector2 move = Vector2.zero; if (Input.GetButtonDown("Horizontal")) { float h = Input.GetAxis("Horizontal"); move.x = Mathf.Sign(h); } if (Input.GetButtonDown("Vertical")) { float h = Input.GetAxis("Vertical"); move.y = Mathf.Sign(h); } if (move != Vector2.zero) { //Debug.Log("Move: " + move); MovingEntity.MoveResult result = m_movingEntity.Move(move); switch (result.Result) { case MovingEntity.MoveResult.ResultValue.Ok: Synchronizer.Continue(m_actor, m_movingEntity.moveActionCost); if (m_levelBuilder.LevelType != LevelType.Ambush) { if (m_levelBuilder.EndPoint.X == result.Cell.X && m_levelBuilder.EndPoint.Y == result.Cell.Y) { if (OnPlayerMovedToEndEvent != null) { OnPlayerMovedToEndEvent(); } } if (m_levelBuilder.ObjectivePoint.X == result.Cell.X && m_levelBuilder.ObjectivePoint.Y == result.Cell.Y) { if (OnPlayerMovedToObjectiveEvent != null) { OnPlayerMovedToObjectiveEvent(); } } } break; case MovingEntity.MoveResult.ResultValue.TileBlocked: //Debug.Log("Tile blocked!"); break; case MovingEntity.MoveResult.ResultValue.TileOccupied: AttackingEntity.AttackResult res = CombatSolver.Fight(m_actor, result.Cell.GetEntity().Actor); Synchronizer.Continue(m_actor, res.Weapon.TimeCost); break; } } } }