示例#1
0
        /// <summary>
        /// returns a valid spawn location
        /// </summary>
        private Vector2 RandomSpawnLocation()
        {
            int     x        = rng.getInt(0, currentMap.width - 1);
            int     y        = rng.getInt(0, currentMap.height - 1);
            Vector2 spawnLoc = new Vector2(x, y);

            return(currentMap.GetNearestValidMove(spawnLoc));
        }
示例#2
0
        public bool Event_ThrowItem(ComponentEvent e)
        {
            AreaMap map        = Engine.instance.world.currentMap;
            var     throwEvent = ((EThrowItem)e);
            string  itemType   = throwEvent.itemName;

            // try to find the item on the entity to throw it
            var consumeItemEvent = (EConsumeItem)owner.FireEvent(new EConsumeItem()
            {
                itemName = itemType
            });

            if (!consumeItemEvent.hasItem)
            {
                return(true);
            }

            // This enttiy did have the item to throw
            Entity projectile = consumeItemEvent.consumedItem;

            // Get the thrwoer strength and item weight, in order to calc both damage and max throw distance
            var getStrength = (EGetAttributeLevel)owner.FireEvent(new EGetAttributeLevel()
            {
                target = "strength"
            });
            int   strength = getStrength.level;
            float strRatio = (strength / 100.0f);

            var getWeight    = (EGetWeight)owner.FireEvent(new EGetWeight());
            int thrownWeight = getWeight.weight;

            float maxDistance = CombatEngine.GetThrowRange(owner, projectile);
            int   throwDamage = (int)(strRatio * thrownWeight);

            // if the target position is farther than the max distance, select the nearest point in that direction
            Vector2 targetLoc = throwEvent.targetLocation;
            Vector2 toTarget  = targetLoc - owner.position;

            if (toTarget.Magnitude() > maxDistance)
            {
                Vector2 dir = toTarget.Normalized();
                targetLoc = owner.position + (dir * maxDistance);
            }

            // check where the item hits, if there is something in the way
            Vector2 hitNormal;
            Vector2 endPosition = map.CollisionPointFromTo(owner.position, targetLoc, out hitNormal);

            // find the target to hit, and apply some damage and knock them back
            Entity[] targets = map.GetAllObjectsAt(endPosition.X, endPosition.Y);
            foreach (Entity hit in targets)
            {
                CombatInstance combat = new CombatInstance(owner, hit)
                {
                    { "damage", throwDamage },
                    { "weapon", projectile }
                };

                CombatEngine.ProcessCombat(combat);
            }

            // the thrown item ends up next to the target location, or the nearest valid location
            toTarget    = Vector2.OrthoNormal(toTarget);
            endPosition = endPosition - toTarget;
            endPosition = map.GetNearestValidMove(endPosition);
            Engine.instance.world.SpawnExisting(projectile, endPosition);

            return(true);
        }