public static void Spawn(int x, int y, GameObject prefab) { World.Grid grid = World.Instance.GetGrid(); World.Cell cell = grid.GetCell(x, y); if (!cell.IsBlocked) { //Vector3 position = new Vector3(x, heightOffset, y); GameObject spawn = Instantiate <GameObject>(prefab); spawn.transform.position = spawn.transform.position; SynchronizedActor actor = spawn.GetComponent <SynchronizedActor>(); if (actor != null) { Entity entity = null; if (actor.tag == "Player") { entity = new Player(x, y, EntityStatistics.GetRandomPlayerStats()); } else if (actor.tag == "Enemy") { MonsterConfig config = spawn.GetComponent <MonsterConfig>(); entity = new Monster(x, y, config.GetStats()); } if (entity != null) { actor.InitActor(entity); } } } }
/// <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); }