예제 #1
0
    public Pos[] GetBorderPositions(CommanderID comID)
    {
        List <Tile> buildings = new List <Tile>();

        foreach (Tile tile in tiles)
        {
            if (tile.Entity != null && tile.Entity.comID == comID)
            {
                buildings.Add(tile);
            }
        }

        HashSet <Pos> emptyBorderPositions = new HashSet <Pos>();

        foreach (Tile commanderTiles in buildings)
        {
            Pos[] neighPositions = GetNeighbours(commanderTiles.pos);
            foreach (Pos neighPos in neighPositions)
            {
                Tile neighTile = GetTile(neighPos);
                if (neighTile.Entity == null)
                {
                    emptyBorderPositions.Add(neighPos);
                }
            }
        }

        return(emptyBorderPositions.ToArray());
    }
예제 #2
0
 public Commander(int lumber, int wheat, Color color, CommanderID comID)
 {
     this.lumber = lumber;
     this.wheat  = wheat;
     this.color  = color;
     this.comID  = comID;
 }
예제 #3
0
    public static Peasant SpawnPeasant(int x, int y, CommanderID comID)
    {
        Peasant peasant = Object.Instantiate(Assets.Peasant);

        peasant.Init(x, y, comID);
        peasant.transform.SetParent(Controller.Instance.unitsRoot, false);
        peasant.transform.position = Controller.Instance.grid.GetTile(x, y).transform.position;
        SpawnEffect(x, y, Config.colors.white);
        return(peasant);
    }
예제 #4
0
    public void Init(int x, int y, CommanderID comID)
    {
        pos.x = x;
        pos.y = y;

        this.comID = comID;

        body.color = Controller.Instance.commanders[comID].color;

        resourceImage.enabled = false;

        List <Pos> path = new List <Pos>();

        updateTask = Task.Run().Name("Peasant(tick)").Loop(-1).OnRepeat(t =>
        {
            if (movementTask != null)
            {
                return;
            }

            if (path.Count > 0)
            {
                MoveTo(path[0]);
                path.RemoveAt(0);
                return;
            }

            path = PickNextPath();

            if (path.Count > 0)
            {
                MoveTo(path[0]);
                path.RemoveAt(0);
                return;
            }

            MoveToRandomNeighbour();

            if (!isActive)
            {
                Core.Juggler.Remove(updateTask);
                if (movementTask != null)
                {
                    Core.Juggler.Remove(movementTask);
                }
                Object.Destroy(this.gameObject);

                Hatchery.SpawnEffect(pos.x, pos.y, Config.colors.pink);
            }
        });
    }
예제 #5
0
    public AI(CommanderID comID)
    {
        this.comID = comID;

        Commander com = Controller.Instance.commanders[CommanderID.Opponent];

        commands.Add(Building.Windmill);
        commands.Add(Building.Lumberyard);
        commands.Add(Building.House);
        commands.Add(Building.Windmill);
        commands.Add(Building.Windmill);
        commands.Add(Building.House);
        commands.Add(Building.House);
        commands.Add(Building.Lumberyard);
        commands.Add(Building.Tower);
        commands.Add(Building.Tower);
        commands.Add(Building.House);
        commands.Add(Building.House);
        commands.Add(Building.Tower);
        commands.Add(Building.Castle);
        commands.Add(Building.House);
        commands.Add(Building.Castle);
        commands.Add(Building.House);
        commands.Add(Building.Windmill);
        commands.Add(Building.Windmill);
        commands.Add(Building.House);
        commands.Add(Building.House);
        commands.Add(Building.Lumberyard);
        commands.Add(Building.Tower);
        commands.Add(Building.Tower);
        commands.Add(Building.House);
        commands.Add(Building.House);

        Task.Run().Name("AI Brain").Time(5f).Random(4f).Loop(-1).OnRepeat(_ =>
        {
            Debug.Log("Thinking...");

            if (commands.Count > 0)
            {
                Pos[] borderPositions = Controller.Instance.grid.GetBorderPositions(CommanderID.Opponent);
                Pos randomPos         = borderPositions[Random.Range(0, borderPositions.Length)];

                if (com.TryBuild(Builder.BuildingToEntity(commands[0]), randomPos.x, randomPos.y))
                {
                    commands.RemoveAt(0);
                }
                return;
            }
        });
    }
예제 #6
0
    public Tile FindClosestTileWithEntity <T>(Pos pos, CommanderID comID)
    {
        List <Tile> potentials = new List <Tile>();

        foreach (Tile tile in tiles)
        {
            if (tile.Entity == null)
            {
                continue;
            }
            if (tile.Entity.comID != comID)
            {
                continue;
            }

            if (tile.Entity is T)
            {
                potentials.Add(tile);
            }
        }

        if (potentials.Count > 0)
        {
            Tile  closestTile     = null;
            float closestDistance = 9999f;

            foreach (Tile tile in potentials)
            {
                float d = Vector2.Distance(new Vector2(tile.pos.x, tile.pos.y), new Vector2(pos.x, pos.y));
                if (d < closestDistance)
                {
                    closestDistance = d;
                    closestTile     = tile;
                }
            }

            return(closestTile);
        }

        return(null);
    }