Exemplo n.º 1
0
        public void OnDeath(GameObject source)
        {
            // Killer gains exp n stuff for killing, right?
            Actor killer = (Actor)source.GetComponent <Actor>();

            if (killer != null)
            {
                killer.GiveXp(Xp);
                HUD.Append(source.Name + " killed " + Name + ".");
            }

            // Update HUD
            HUD.CacheInstance().Target(null);

            // We need to remove this enemy for the map too, right?
            MapManager.CurrentMap().PopObject(transform.position.x, transform.position.y);
            MapManager.CurrentNavigationMap().RemoveObject(transform.position);

            // Transfer inventory items from killed actor
            Inventory killedInventory = (Inventory)gameObject.GetComponent <Inventory>();

            killedInventory.MergeWith((Inventory)source.GetComponent <Inventory>());

            GameObject.Destroy(this.gameObject);

            // Play death sound
            //Console.Beep(200, 100);

            return;
        }
Exemplo n.º 2
0
        public bool TryMove(int dx, int dy)
        {
            bool moved = false;
            Map  map   = MapManager.CurrentMap();

            if (map == null)
            {
                return(false);
            }
            Collider.CollisionTypes type = collider.HandleCollision(dx, dy, out GameObject found);
            // It checks the map to see if there is any collisions if the enemy moves to that square.
            if (type == Collider.CollisionTypes.None)
            {
                //If there is none, it moves the actor into the new square and updates the map.

                map.PopObject(transform.position.x, transform.position.y);
                moved = map.AddObject(transform.position.x + dx, transform.position.y + dy, gameObject);
                MapManager.CurrentNavigationMap().UpdatePositions(transform.position, new Vec2i(transform.position.x + dx, transform.position.y + dy));

                if (moved)
                {
                    transform.Translate(dx, dy);
                }
            }
            else
            {
                // If there is a collision, the actor doesn't move. If this collision is with a damageable, a
                // damage calculation is performed to calculate the amount of damage done to the player.
                if (found != null)
                {
                    // It's possible that we collided with something interactable.
                    List <IInteractable> interactables = found.GetComponents <IInteractable>();
                    foreach (IInteractable interactable in interactables)
                    {
                        if (interactable.IsInteractable)
                        {
                            interactable.OnInteract(this.gameObject, this);
                        }
                    }

                    // It's also possible that we collided with something damageable.
                    List <IDamageable> damageables = found.GetComponents <IDamageable>();
                    foreach (IDamageable damageable in damageables)
                    {
                        damageable.ApplyDamage(this.gameObject, CalculateDamage());
                    }
                }
            }
            return(moved);
        }
Exemplo n.º 3
0
        public void OnInteract(GameObject objectInteracting, object interactorType)
        {
            // This line makes only actors with the DoorOpener component can open doors.
            if (objectInteracting != null && !(interactorType is IDoorOpener) &&
                !((interactorType is IRage) && ((IRage)interactorType).isRaging))
            {
                return;
            }

            bool destroyDoor = false;

            if (locked)
            {
                if (objectInteracting.GetComponent <Player>() != null)
                {
                    Inventory inv = (Inventory)Player.MainPlayer().GetComponent <Inventory>();
                    if (inv.Find("Key") != null)
                    {
                        inv.Remove("Key");
                        MapManager.CurrentMap().PopObject(transform.position.x, transform.position.y);
                        MapManager.CurrentNavigationMap().RemoveObject(transform.position);

                        HUD.Append(objectInteracting.Name + " unlocked and opened a door.");
                        destroyDoor = true;
                    }
                    else
                    {
                        HUD.Append(objectInteracting.Name + " tried to open door, but is was locked.");
                        //Console.Beep(80, 100);
                    }
                }
            }
            else
            {
                MapManager.CurrentMap().PopObject(transform.position.x, transform.position.y);
                MapManager.CurrentNavigationMap().RemoveObject(transform.position);
                if (objectInteracting.GetComponent <Player>() != null)
                {
                    HUD.Append(objectInteracting.Name + " opened a door.");
                    destroyDoor = true;
                }
            }

            if (destroyDoor)
            {
                GameObject.Destroy(gameObject);
                //Console.Beep(100, 100);
            }
        }
Exemplo n.º 4
0
        public void OnInteract(GameObject objectInteracting, object interactorType)
        {
            if (objectInteracting == null)
            {
                return;
            }
            if (!(interactorType is IRage))
            {
                return;
            }

            IRage ragingEnemy = (IRage)interactorType;

            if (ragingEnemy.isRaging)
            {
                MapManager.CurrentMap().PopObject(transform.position.x, transform.position.y);
                MapManager.CurrentNavigationMap().RemoveObject(transform.position);
                GameObject.Destroy(gameObject);
            }
        }
Exemplo n.º 5
0
        public override void LateUpdate()
        {
            targetPath.Clear();
            if (Target == null)
            {
                return;
            }

            Graph <Vec2i> graph;

            if ((graph = MapManager.CurrentNavigationMap()?.CacheInstance()) == null)
            {
                return;
            }

            Vec2i goal = Target.position;

            if (Vec2i.Heuristic(goal, transform.position) == 1)
            {
                targetPath.Add(goal);
                return;
            }

            Vec2i current = null;

            Vec2i start = this.transform.position;

            PriorityQueue <Vec2i> frontier = new PriorityQueue <Vec2i>();

            frontier.Enqueue(start, 0.0);

            Dictionary <Vec2i, Vec2i>  cameFrom  = new Dictionary <Vec2i, Vec2i>();
            Dictionary <Vec2i, double> costSoFar = new Dictionary <Vec2i, double>(); // Movement Costs.

            cameFrom[start]  = null;
            costSoFar[start] = 0; // Movement Costs.

            while (frontier.Count() != 0)
            {
                current = frontier.Dequeue();

                if (current == goal)
                {
                    break;
                }

                if (graph.GetEdges(current) != null)
                {
                    foreach (Vec2i next in graph.GetEdges(current))
                    {
                        if (next == null)
                        {
                            continue;
                        }
                        double newCost = costSoFar[current] + 1; // Movement Costs.

                        //If the cost is greater then 50, then the path is too long already and it continues.
                        if (newCost > 50)
                        {
                            continue;
                        }

                        GameObject nextGo = map.PeekObject(next);
                        if (nextGo == null || next == goal || nextGo.GetComponent <Door>() != null)
                        {
                            if (!cameFrom.ContainsValue(next) || newCost < costSoFar[next])
                            {
                                costSoFar[next] = newCost;    // Movement Costs.

                                double priority = newCost + Vec2i.Heuristic(goal, next);

                                frontier.Enqueue(next, priority);
                                cameFrom[next] = current;
                            }
                        }
                    }
                }
            }


            bool isPathGood = true;


            if (goal != null)
            {
                current = goal;
                //List<Vec2i> path = new List<Vec2i>();
                while (current != start)
                {
                    targetPath.Add(current);

                    if (!cameFrom.ContainsKey(current))
                    {
                        isPathGood = false;
                        break;
                    }
                    else
                    {
                        current = cameFrom[current];
                    }
                    if (current != start && current != goal && (map.PeekObject(current) != null && map.PeekObject(current).GetComponent <Door>() == null))
                    {
                        isPathGood = false;
                        break;
                    }
                }

                if (!isPathGood)
                {
                    targetPath.Clear();
                }

                /*if (isPathGood)
                 * {
                 *  Camera camera = Camera.CacheInstance();
                 *  int halfWidth = camera.width / 2;
                 *  int halfHeight = camera.height / 2;
                 *
                 *  Player player = Player.MainPlayer();
                 *  int playerX = player.transform.position.x;
                 *  int playerY = player.transform.position.y;
                 *
                 *  foreach (Vec2i v in path)
                 *  {
                 *      int x = v.x - playerX + halfWidth;
                 *      int y = v.y - playerY + halfHeight;
                 *      if (x < camera.width && y < camera.height)
                 *        ConsoleUI.Write(x, y, ".", Color.Teal);
                 *  }
                 * }*/
            }
            return;
        }