Example #1
0
 public Player AddPlayer(string name, int teamIndex=0)
 {
     Player newPlayer = new Player(GetNextId(), name, teamIndex);
     players.Add(newPlayer);
     unitQuadtrees[newPlayer] = new Quadtree(map.width, map.height);
     return newPlayer;
 }
Example #2
0
        public Unit AddUnit(UnitType unitType, Position position, Player owner)
        {
            if (!map.Inside(position)) return null;

            var newUnit = new Unit(GetNextId(), this, unitType, position, owner);
            units.Add(newUnit);
            unitQuadtrees[owner].AddUnit(newUnit);
            CacheSetUnitAt(newUnit);
            return newUnit;
        }
Example #3
0
        public AI( Engine e, Player player, List<Position> g, List<Position> i, List<Position> m )
        {
            engine = e;

            me = player;

            goldTiles = g;
            ironTiles = i;
            manaTiles = m;
        }
Example #4
0
        // handles context displays, selected units, resource counters,
        public Hud(ref Engine engine, ref Map map, Texture2D boxSelect, Texture2D HPbar)
        {
            this.engine = engine;
            this.map = map;
            this.boxSelect = boxSelect;
            this.HPbar = HPbar;
            this.currentPlayer = engine.players[0];

            selectedUnits = new List<Unit>();
            startRect = new Vector2();
            endRect = new Vector2();

            // initialize
            m = Mouse.GetState();
            m2 = Mouse.GetState();
            lastButtonPressed = "None";
        }
Example #5
0
        public Unit(int id, Engine engine, UnitType unitType, Position position, Player owner)
        {
            this.id = id;
            this.engine = engine;
            this.type = unitType;
            this.position = position;
            this.previousPosition = position;
            this.nextMove = -1;
            this.owner = owner;

            health = type.maxHealth;
            status = Status.Idle;
            orders = new List<Order>();
            modifiers = new List<UnitModifier>();

            animationStartTick = 0;
            direction = 0;

            currentPath = null;
            currentTargetPosition = null;
        }
Example #6
0
        public override void Update(GameTime gt)
        {
            // update states
            m = Mouse.GetState();
            k = Keyboard.GetState();

            if (k.IsKeyDown(Keys.NumPad1))
            {
                currentPlayer = engine.players[0];
            }
            else if (k.IsKeyDown(Keys.NumPad2))
            {
                currentPlayer = engine.players[1];
            }
            if (m.RightButton == ButtonState.Pressed && m2.RightButton == ButtonState.Released)
            {

            }
            else if (m.RightButton == ButtonState.Pressed && m2.RightButton == ButtonState.Pressed)
            {

            }
            else if (m.RightButton == ButtonState.Released && m2.RightButton == ButtonState.Pressed)
            {
                UnitGroupCommand();
            }
            else
            {
                // reset right button variables
            }

            // clicker first pressed
            if (m.LeftButton == ButtonState.Pressed && m2.LeftButton == ButtonState.Released)
            {
                // distribute orders
                //map.unitGroupMove(selectedUnits);
                // save the current mouse position
                startRect = new Vector2(m.X, m.Y);

                timer = 0;
            }
            // clicker held
            else if (m.LeftButton == ButtonState.Pressed && m2.LeftButton == ButtonState.Pressed)
            {
                timer += gt.ElapsedGameTime.TotalSeconds;
                if (timer >= HOLD_THRESH)
                {
                    if (!rectangleSelect)
                    {
                        // if held longer than half a second
                        rectangleSelect = true;
                        endRect = new Vector2(m.X, m.Y);
                    }
                    else
                    {
                        endRect = new Vector2(m.X, m.Y);
                    }
                }

            }
            // clicker first released
            else if (m.LeftButton == ButtonState.Released && m2.LeftButton == ButtonState.Pressed)
            {
                if (rectangleSelect)
                {
                    ClearSelectedUnits();
                    // save the end position for the rectangle
                    endRect = new Vector2(m.X, m.Y);

                    // calculate new selected units
                    selectedUnits = FindUnitsIn(startRect, endRect);
                }
                else // not rectangle select, single tile select
                {
                    if ( lastButtonPressed.Equals("None") )
                    {
                        ClearSelectedUnits();
                        // same algorithm to select units from a 1x1 tile square
                        selectedUnits = FindUnitsIn(startRect, new Vector2(startRect.X + 1, startRect.Y + 1));
                    }
                }
            }
            else // reset, no mouse buttons are pressed
            {
                rectangleSelect = false;
                timer = 0;
                startRect = Vector2.Zero;
                endRect = Vector2.Zero;
            }

            // update old state
            m2 = m;

            if (debugMode)
            {
                DebugUpdate(gt);
            }
        }
Example #7
0
        // returns a list of units in the bounds provided.
        public List<Unit> Select(Rectangle bounds, Player owner)
        {
            List<Unit> selectedUnits = new List<Unit>();

            var startPosition = ScreenToGame(new Vector2(bounds.Left, bounds.Top));
            var endPosition = ScreenToGame(new Vector2(bounds.Right, bounds.Bottom));
            Position startTile = new Position((int)Math.Floor(startPosition.X), (int)Math.Floor(startPosition.Y));
            Position endTile = new Position((int)Math.Ceiling(endPosition.X), (int)Math.Ceiling(endPosition.Y));

            // search only the grid. hopefully small n^2
            for (var y = startTile.y; y < endTile.y; ++y)
            {
                for (var x = startTile.x; x < endTile.x; ++x)
                {
                    Unit unit = engine.GetUnitAt(x, y);
                    if (unit != null && unit.owner.Equals(owner))
                    {
                        unit.selected = true;
                        selectedUnits.Add(unit);
                    }
                }
            }
            return selectedUnits;
        }