Example #1
0
        private void SpreadLight(int x, int y)
        {
            Map map = MapManager.CurrentMap();

            if (map == null)
            {
                return;
            }
            float angleDiff          = 2 * (float)Math.PI / lightRays;
            float singleRayIntensity = totalLightIntensity / lightRays;

            for (int i = 0; i < lightRays; ++i)
            {
                float xSpeed = lightSpeed * (float)Math.Cos(i * angleDiff);
                float ySpeed = lightSpeed * (float)Math.Sin(i * angleDiff);
                Ray   ray    = new Ray(x + .5f, y + .5f, xSpeed, ySpeed, singleRayIntensity);
                while (ray.IsWithinRadius())
                {
                    ray.Advance();
                    GameObject go = ray.Collides(gameObject, map);
                    if (go != null)
                    {
                        ray.Illuminate(go);
                        break;
                    }
                }
            }
        }
Example #2
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;
        }
Example #3
0
        /// <summary>
        /// Uses an inputted movement and uses the current position and the movement to check
        /// if there location being moved into is empty or not. If there is already a game object
        /// at that spot, it will also return a reference to that game object.
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="found"></param>
        /// <returns></returns>
        public CollisionTypes HandleCollision(int dx, int dy, out GameObject found)
        {
            found = null;

            // Finds the map that is stored in the global GameObject.
            Map map = MapManager.CurrentMap();

            if (map != null)
            {
                if (this.transform != null)
                {
                    // Checks if the map cell is open.
                    found = map.PeekObject(this.transform.position.x + dx, this.transform.position.y + dy);
                }
                else
                {
                    Debug.LogError("Game object is null.");
                }
            }
            else
            {
                Debug.LogError("Map game object wasn't found.");
            }
            // Return either no collision or some active object.
            if (found == null)
            {
                return(CollisionTypes.None);
            }
            if (found.GetComponent <Wall>() != null)
            {
                return(CollisionTypes.Wall);
            }
            return(CollisionTypes.ActiveObject);
        }
Example #4
0
        /// <summary>
        /// Takes a position and adds its neighbors to connect the now empty spot.
        /// </summary>
        /// <param name="from"></param>
        public void RemoveObject(Vec2i from)
        {
            Map map = MapManager.CurrentMap();

            if (graph == null || map == null || from == null)
            {
                return;
            }

            AddNeighbors(from, map);
        }
Example #5
0
        /// <summary>
        /// Checks if there exists a linear line between the target and this objects current position.
        /// </summary>
        /// <param name="targeterLocation"> The location of the targeter.</param>
        /// <param name="targetLocation"> The location of the target.</param>
        /// <returns></returns>
        private bool CheckLine(Vec2i targeterLocation, Vec2i targetLocation)
        {
            Map  map       = MapManager.CurrentMap();
            bool hitObject = false;

            if (map == null)
            {
                Debug.LogWarning("Map not found.");
                return(false);
            }

            if (targeterLocation == targetLocation)
            {
                return(true);
            }

            double slopeY   = 0;
            double slopeX   = 0;
            double currentX = 0;
            double currentY = 0;

            /*If the distance from the target on the X-axis is larger then the Y-axis, is uses
             * //the X-axis as the indepent variable and generates the slope of Y in terms of the
             * //change in X. It then increments a block at a time towards the target checking at each increment
             * //if there is an object blocking the line of sight.*/
            if (Math.Abs(targetLocation.x - targeterLocation.x) > Math.Abs(targetLocation.y - targeterLocation.y))
            {
                slopeX = (targeterLocation.x < targetLocation.x) ? 1 : -1;
                slopeY = (targetLocation.y - targeterLocation.y + 0.0) / (targetLocation.x - targeterLocation.x) * slopeX;
            }
            //Does the same thing as above but the Y-Axis is the indepent variable.
            else
            {
                slopeY = (targeterLocation.y < targetLocation.y) ? 1 : -1;
                slopeX = (targetLocation.x - targeterLocation.x + 0.0) / (targetLocation.y - targeterLocation.y) * slopeY;
            }
            currentX = slopeX;
            currentY = slopeY;

            while (((targeterLocation.x + (int)Math.Round(currentX, 0) != targetLocation.x) ||
                    (targeterLocation.y + (int)Math.Round(currentY) != targetLocation.y)) && !hitObject)
            {
                if (map.PeekObject(targeterLocation.x + (int)Math.Round(currentX, 0), targeterLocation.y + (int)Math.Round(currentY, 0)) != null)
                {
                    hitObject = true;
                }
                currentY += slopeY;
                currentX += slopeX;
            }
            return(!hitObject);
        }
Example #6
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);
        }
Example #7
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);
            }
        }
Example #8
0
        /// <summary>
        /// Takes in an array of positions that needs to be updated and updates them.
        /// </summary>
        /// <param name="positionList"></param>
        public void UpdatePositions(params Vec2i[] positionList)
        {
            Map map = MapManager.CurrentMap();

            if (graph == null || map == null)
            {
                return;
            }

            //To each update each position, all the edges related to that position are removed
            //and then readded back into the graph.
            foreach (Vec2i current in positionList)
            {
                RemoveNeighbors(current);
                AddNeighbors(current, map);
            }
        }
Example #9
0
        public override void Start()
        {
            player      = (Player)GameObject.Instantiate("MainPlayer").AddComponent(new Player("Sneaky McDevious", "Thiefy rogue", 1, 10, 1, 2));
            player.Name = "MainPlayer";

            GameObject mapObject = GameObject.Instantiate("MapManager");

            mapObject.Name = "MapManager";

            mapManager = (MapManager)mapObject.AddComponent(new MapManager(80, 40, 1));

            mapObject.Transform.position = new Vec2i(mapObject.Transform.position.x, gameHeight - 1);

            Map map = MapManager.CurrentMap();

            currentMap = map;


            player.AddComponent(new PlayerController());
            player.AddComponent(new Model());
            player.AddComponent(new LightSource(10.0f));
            player.AddComponent(new Camera(gameWidth - hudWidth, gameHeight));
            player.AddComponent(new MapTile('$', new Color(255, 255, 255), true));
            player.AddComponent(new Inventory());
            player.AddComponent(new Sound());

            player.transform.position = new Vec2i(map.startingX, map.startingY);
            player.transform.SetParent(this.transform);

            map.AddObject(map.startingX, map.startingY, player.gameObject);


            // Setup HUD for stats and info
            hud      = (HUD)GameObject.Instantiate("HUD").AddComponent(new HUD(hudWidth, gameHeight));
            hud.Name = "HUD";

            hud.transform.SetParent(this.transform);

            Model hudModel = (Model)hud.AddComponent(new Model());

            hudModel.color.Set(180, 180, 180);
            hud.transform.position = new Vec2i(gameWidth - hudWidth, gameHeight - 1);

            Debug.Log("GameManager added all components on start.");
            return;
        }
Example #10
0
        /// <summary>
        /// When this component starts, it uses the map of the level to fill in all the
        /// initial edges between the empty map squares.
        /// </summary>
        public override void Start()
        {
            graph = new Graph <Vec2i>();
            Map map = MapManager.CurrentMap();

            if (map != null)
            {
                // Building the graph from the map.
                for (int x = 0; x < map.width; ++x)
                {
                    for (int y = 0; y < map.height; ++y)
                    {
                        Vec2i from = new Vec2i(x, y);
                        AddNeighbors(from, map);
                    }
                }
            }
            return;
        }
Example #11
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);
            }
        }
Example #12
0
        public override void Render()
        {
            map = MapManager.CurrentMap();
            if (map == null)
            {
                return;
            }

            int playerX = gameObject.Transform.position.x;
            int playerY = gameObject.Transform.position.y;

            int halfWidth  = width / 2;
            int halfHeight = height / 2;

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    int        xToCheck = playerX - halfWidth + x;
                    int        yToCheck = playerY - halfHeight + y;
                    GameObject go       = map.PeekObject(xToCheck, yToCheck);
                    if (go != null)
                    {
                        MapTile tile = (MapTile)go.GetComponent(typeof(MapTile));
                        if (tile.disableLighting)
                        {
                            ConsoleUI.Write(x, y, tile.character, tile.color);
                        }
                        else if (tile.lightLevel > 0f)
                        {
                            Color illuminated = tile.color.Apply(tile.lightLevel);
                            ConsoleUI.Write(x, y, tile.character, illuminated);
                        }
                    }
                }
            }
            return;
        }
Example #13
0
 public override void Start()
 {
     map = MapManager.CurrentMap();
     return;
 }