Exemple #1
0
    public void NotifyEntityMovementEnd(MoveableEntity ent)
    {
        // Get the visual for the old tile it was on and unlink the entity from it.
        TileVisual oldTV = mapTerrainVisuals.GetAt(ent.t.x, ent.t.y);
        GameObject entGO;

        if (oldTV != null)
        {
            entGO = oldTV.entityGO;
            mapTerrainVisuals.GetAt(ent.t.x, ent.t.y).entityGO = null;
        }
        else
        {
            // There was no old tile visual. We will have to create a new one.
            entGO = TileVisual.CreateEntityVisual(map.GetAt(ent.movingTo));
        }

        // Set as the entity visual for another tile visual, but only if that tile is on screen...
        TileVisual newTV = mapTerrainVisuals.GetAt(ent.movingTo.x, ent.movingTo.y);

        if (newTV != null)
        {
            if (newTV.entityGO != null)
            {
                Destroy(newTV.entityGO);
            }
            newTV.entityGO = entGO;
        }
        else
        {
            // This enttity game object isn't needed.
            Destroy(entGO);
        }
    }
Exemple #2
0
    public void NotifyAttack(MoveableEntity source, Entity target)
    {
        // For now, always display the attack even if off screen. This is because for example there might be an attack from off the top of the screen to off the left of the screen,
        //   where even though you can't see either entity you can still see the attack orb.

        // Set the default sprite and colour.
        Sprite s = attackOrbSprite;
        Color  c = source.GetSpriteColor();

        // If the player is attacking however...
        if (source is Player)
        {
            Player p = (Player)source;
            // See if the player has an item selected...
            ItemType it = p.inv.GetSelectedItemType();
            if (it != null)
            {
                // Use the sprite for this item type instead.
                s = it.sprite;
                c = it.color;
            }
        }

        animations.Add(new Transition(s, c, new Vector3(source.t.x, source.t.y, 3f), new Vector3(target.t.x, target.t.y, 3f), 1));
    }
Exemple #3
0
    public void NotifyEntityMovementStart(MoveableEntity ent)
    {
        TileVisual tv = mapTerrainVisuals.GetAt(ent.t.x, ent.t.y);

        if (tv == null || tv.entityGO == null)
        {
            return;
        }
        else
        {
            animations.Add(new Transition(tv.entityGO, new Vector3(ent.movingTo.x, ent.movingTo.y), ent.actionTimer));
        }
    }
    public bool TrySetTarget(int x, int y)
    {
        // First check there is a target there.
        Entity e = GC.inst.map.GetAt(x, y).entity;

        if (e == null)
        {
            return(false);
        }

        // Now make sure the target isn't dead.
        if (e.dead)
        {
            return(false);
        }

        // Check the the target is moveable. We don't want to go around randomly chopping down trees, or trying to attack dummy entities.
        if (!(e is MoveableEntity))
        {
            return(false);
        }

        // Check that we are hostile or evasive to the entity.
        if (e is Player)
        {
            if (creatureType.playerStance == CreatureType.Stance.SemiEvasive || creatureType.playerStance == CreatureType.Stance.SemiHostile)
            {
                return(false);
            }
        }
        else if (e is Creature)
        {
            CreatureType.Stance stance;
            creatureType.creatureStances.TryGetValue(((Creature)e).creatureType, out stance);
            if (stance == CreatureType.Stance.SemiEvasive || stance == CreatureType.Stance.SemiHostile)
            {
                return(false);
            }
        }

        // Finally double check we can see the creature.
        if (SquareDistance(e) >= creatureType.sightRange)
        {
            return(false);
        }

        target = (MoveableEntity)e;

        return(true);
    }
Exemple #5
0
 public ActionArgs(MoveableEntity interactingEntity, ActionType type)
 {
     this.sender = interactingEntity;
     this.type   = type;
 }
    public override void SelectNewAction()
    {
        // Firstly, do we have a target?
        if (target != null)
        {
            // Can we still see the target?
            if (target.dead || SquareDistance(target) >= creatureType.sightRange * creatureType.sightRange)
            {
                // Not any more.
                target = null;
            }
        }

        // We still may or may not have a target. If we don't, try to find one.
        if (target == null)
        {
            // For now just do a box search starting with a box of radius 1, with the radius increasing.
            for (int r = 0; r < creatureType.sightRange; r++)
            {
                // Left and right column.
                for (int y = -r; y <= r; y++)
                {
                    if (TrySetTarget(-r, y) || TrySetTarget(r, y))
                    {
                        break;
                    }
                }

                if (target != null)
                {
                    break;
                }

                // Top and bottom row.
                for (int x = t.x - r + 1; x <= t.x + r - 1; x++)
                {
                    if (TrySetTarget(x, -r) || TrySetTarget(x, r))
                    {
                        break;
                    }
                }

                if (target != null)
                {
                    break;
                }
            }
        }

        // Finally act based on whether or not there is a target.
        if (target != null)
        {
            // We have a target.
            CreatureType.Stance stance;
            if (target is Player)
            {
                stance = creatureType.playerStance;
            }
            else if (target is Creature)
            {
                creatureType.creatureStances.TryGetValue(((Creature)target).creatureType, out stance);
            }
            else
            {
                // This shouldn't happen, and is not coded for yet.
                Debug.LogError("MoveableEntity is neither creature nor player.");
                stance = CreatureType.Stance.Hostile;
            }

            // If we can attack it we should, but only if hostile or semi hostile.
            if ((stance == CreatureType.Stance.Hostile || stance == CreatureType.Stance.SemiHostile) && attackCooldown <= 0 && SquareDistance(target) < GetAttackRange() * GetAttackRange())
            {
                TryAttack(target);
            }
            else
            {
                // We can't attack it so move closer. First find out our relative position.
                int relx = t.x - target.t.x;
                int rely = t.y - target.t.y;

                // Also calculate their norm.
                int nx = Mathf.Abs(relx);
                int ny = Mathf.Abs(rely);

                // Normalise the relative ones so that they are only 1 or -1.
                if (nx != 0)
                {
                    relx /= nx;
                }
                if (ny != 0)
                {
                    rely /= ny;
                }

                // Now randomly choose to either move in the x direction or the y direction.
                int r = Random.Range(0, nx + ny);

                bool anyEvasive = stance == CreatureType.Stance.Evasive || stance == CreatureType.Stance.SemiEvasive;

                if (r < relx)
                {
                    if (anyEvasive)
                    {
                        // Try to move in the y direction first.
                        TryMoveTo(t.x, t.y + rely);
                    }
                    else
                    {
                        // Try to move in the x direction first.
                        TryMoveTo(t.x - relx, t.y);
                    }
                }
                else
                {
                    if (anyEvasive)
                    {
                        // Try to move in the x direction first.
                        TryMoveTo(t.x + relx, t.y);
                    }
                    else
                    {
                        // Try to move in the y direction first.
                        TryMoveTo(t.x, t.y - rely);
                    }
                }

                // If we didn't move, then try reversing things.
                if (actionTimer <= 0)
                {
                    if (r >= relx)
                    {
                        if (anyEvasive)
                        {
                            // Try to move in the y direction now.
                            TryMoveTo(t.x, t.y + rely);
                        }
                        else
                        {
                            // Try to move in the x direction now.
                            TryMoveTo(t.x - relx, t.y);
                        }
                    }
                    else
                    {
                        if (anyEvasive)
                        {
                            // Try to move in the x direction now.
                            TryMoveTo(t.x + relx, t.y);
                        }
                        else
                        {
                            // Try to move in the y direction now.
                            TryMoveTo(t.x, t.y - rely);
                        }
                    }
                }

                // If that didn't work either then we will just stay here for now and pass.
            }
        }
        else
        {
            // There is no target. Walk in a random direction or else stay still.
            if (GC.Chance(P_NO_TARGET_IDLE))
            {
                // Don't do anything.
                return;
            }
            else
            {
                // Move randomly.
                int r = Random.Range(0, 2);
                if (r == 0)
                {
                    r = -1;
                }
                if (Random.Range(0, 2) == 0)
                {
                    TryMoveTo(t.x + r, t.y);
                }
                else
                {
                    TryMoveTo(t.x, t.y + r);
                }
            }
        }
    }
Exemple #7
0
 public PlayerController(MoveableEntity assignedEntity)
 {
     _assignedEntity = assignedEntity;
 }
Exemple #8
0
 protected InteractEntityActionArgs(MoveableEntity interactingEntity, IInteractable interactedEntity, ActionType type)
     : base(interactingEntity, type)
 {
     this.interacted = interactedEntity;
 }
Exemple #9
0
 public InteractEntityActionArgs(MoveableEntity interactingEntity, IInteractable interactedEntity)
     : this(interactingEntity, interactedEntity, ActionType.INTERACT_ENTITY)
 {
 }
Exemple #10
0
 public ActionArgsCreator(World world, MoveableEntity interactingEntity)
 {
     this.world  = world;
     this.action = ActionType.NOTHING;
     this.sender = interactingEntity;
 }