Example #1
0
        public Piglets()
        {
            _graphics = new GraphicsDeviceManager(this)
            {
                PreferredBackBufferHeight = 650,
                PreferredBackBufferWidth = 800
            };

            Content.RootDirectory = "Content";
            _pigs = new List<PigEntity>();
            foreach (var i in Enumerable.Range(0, 10))
            {
                _pigs.Add(new PigEntity());
            }
            _terrain = new TerrainEntity();
        }
Example #2
0
        public void Update(GameTime gameTime, TerrainEntity currentTerrain)
        {
            if (_elapsedMilliseconds > _nextUpdateTicks)
            {
                Direction = _random.Next(0, 4);

                var newModifier = _random.Next(5, 35);

                TerrainInfo terrainInfo;

                var newX = 0;
                var newY = 0;

                switch (Direction)
                {
                    case 0: // right
                        newX = (int) (_position.X + newModifier);
                        terrainInfo = currentTerrain.GetTerrainInfo(newX, (int) _position.Y);

                        if (terrainInfo.IsValidForPig)
                        {
                            _position.X = newX - terrainInfo.Height;
                        }

                        break;
                    case 1: // left

                        newX = (int) (_position.X - newModifier);
                        terrainInfo = currentTerrain.GetTerrainInfo(newX, (int)_position.Y);
                        if (terrainInfo.IsValidForPig)
                        {
                            _position.X -= newModifier - terrainInfo.Height;
                        }
                        break;
                    case 2: // up
                        newY = (int) (_position.Y - newModifier);

                        terrainInfo = currentTerrain.GetTerrainInfo((int) Position.X, newY);
                        if (terrainInfo.IsValidForPig)
                        {
                            _position.Y -= newModifier - terrainInfo.Height;
                        }

                        break;
                    case 3: // down

                        newY = (int)(_position.Y + newModifier);
                        terrainInfo = currentTerrain.GetTerrainInfo((int)Position.X, newY);
                        if (terrainInfo.IsValidForPig)
                        {
                            _position.Y += newModifier - terrainInfo.Height;
                        }
                        break;
                }
                _elapsedMilliseconds = 0;
            }
            else
            {
                _elapsedMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
            }
        }