示例#1
0
        public override void Update(GameTime gameTime, TeeEngine engine)
        {
            Hero          player        = (Hero)engine.GetEntity("Player");
            KeyboardState keyboardState = Keyboard.GetState();

            if (KeyboardExtensions.GetKeyDownState(keyboardState, Keys.S, this, true) &&
                Entity.IntersectsWith(this, null, player, "Shadow", gameTime))
            {
                if (CurrentDrawableState != "Open")
                {
                    Random random = new Random();

                    CurrentDrawableState = "Open";
                    Drawables.ResetState("Open", gameTime);

                    for (int i = 0; i < 10; i++)
                    {
                        Coin coin = new Coin(this.Pos.X, this.Pos.Y, 100, (CoinType)random.Next(3));
                        coin.Pos.X += (float)((random.NextDouble() - 0.5) * 100);
                        coin.Pos.Y += (float)((random.NextDouble() - 0.5) * 100);

                        engine.AddEntity(coin);
                    }
                }
            }
        }
        public override bool PreDestroy(GameTime gameTime, TeeEngine engine)
        {
            if (PreDestroyEvent != null)
                PreDestroyEvent(this, gameTime, engine);

            return true;
        }
示例#3
0
 void OnMapZoneHit(Entity entity, TeeEngine engine, GameTime gameTime)
 {
     if (MapZoneHit != null)
     {
         MapZoneHit(this, entity, engine, gameTime);
     }
 }
示例#4
0
        public override void MapLoaded(TeeEngine engine, TiledMap map, MapEventArgs mapEventArgs)
        {
            LightShader lightShader = (LightShader)engine.GetPostGameShader("LightShader");

            lightShader.Enabled = false;

            Random random = new Random();

            for (int i = 0; i < 50; i++)
            {
                int px = (int)Math.Ceiling(random.NextDouble() * engine.Map.pxWidth);
                int py = (int)Math.Ceiling(random.NextDouble() * engine.Map.pxHeight);

                Bat  bat  = new Bat(px, py);
                Coin coin = new Coin(px, py, 100, (CoinType)random.Next(3));

                // Switch between adding bats and coins to the map.
                if (i % 2 == 0)
                {
                    engine.AddEntity(bat);
                }
                else
                {
                    engine.AddEntity(coin);
                }
            }

            base.MapLoaded(engine, map, mapEventArgs);
        }
示例#5
0
        public override void MapLoaded(TeeEngine engine, TiledMap map, MapEventArgs mapEventArgs)
        {
            LightShader lightShader = (LightShader)engine.GetPostGameShader("LightShader");

            lightShader.Enabled = true;

            base.MapLoaded(engine, map, mapEventArgs);
        }
示例#6
0
        public override void Update(GameTime gameTime, TeeEngine engine)
        {
            List<Entity> entitiesHit = engine.Collider.GetIntersectingEntities<Entity>(this.CurrentBoundingBox);

            foreach (Entity entity in entitiesHit)
            {
                if (entity != this && Entity.IntersectsWith(this, null, entity, "Shadow", gameTime))
                    OnMapZoneHit(entity, engine, gameTime);
            }
        }
        public virtual void MapLoaded(TeeEngine engine, TiledMap map, MapEventArgs mapEventArgs)
        {
            if (mapEventArgs.HasProperty("Target"))
            {
                Hero        player         = new Hero();
                MapEntrance targetEntrance = (MapEntrance)engine.GetEntity(mapEventArgs.GetProperty("Target"));

                player.Pos = targetEntrance.Pos + new Vector2(targetEntrance.Width, targetEntrance.Height) / 2;

                engine.AddEntity("Player", player);
            }
        }
示例#8
0
        public override void Update(GameTime gameTime, TeeEngine engine)
        {
            List <Entity> entitiesHit = engine.Collider.GetIntersectingEntites(this.CurrentBoundingBox);

            foreach (Entity entity in entitiesHit)
            {
                if (entity != this && Entity.IntersectsWith(this, null, entity, "Shadow", gameTime))
                {
                    OnMapZoneHit(entity, engine, gameTime);
                }
            }
        }
示例#9
0
        void MapEntrance_MapZoneHit(MapZone sender, Entity entity, TeeEngine engine, GameTime gameTime)
        {
            if (KeyboardExtensions.GetKeyDownState(Keyboard.GetState(), ACTIVATE_KEY, engine, true) &&
                entity == engine.GetEntity("Player"))
            {
                MapEventArgs mapArgs = new MapEventArgs();
                mapArgs.SetProperty("Target", Target);

                engine.ClearEntities();
                engine.LoadMap(Destination, mapArgs);
            }
        }
示例#10
0
        public override bool PreInitialize(GameTime gameTime, TeeEngine engine)
        {
            int coinx = (Width / CoinPadding);
            int coiny = (Height / CoinPadding);

            for (int i = 0; i < coinx; i++)
            {
                for (int j = 0; j < coiny; j++)
                {
                    Coin coin = new Coin();
                    coin.CoinType  = CoinType;
                    coin.CoinValue = CoinValue;
                    coin.Pos       = new Vector2(Pos.X + i * CoinPadding, Pos.Y + j * CoinPadding);

                    engine.AddEntity(coin);
                }
            }

            return(false);   // Destory self when ready.
        }
示例#11
0
        public override void Update(GameTime gameTime, TeeEngine engine)
        {
            if (IsOnScreen)
            {
                float COIN_MOVE_SPEED   = 5000;
                float TERMINAL_VELOCITY = 5;

                Hero player = (Hero)engine.GetEntity("Player");

                // Find the distance between the player and this coin.
                float distanceSquared = Vector2.DistanceSquared(Pos, player.Pos);

                float speed = COIN_MOVE_SPEED / distanceSquared;  // Mangitude of velocity.
                speed = Math.Min(speed, TERMINAL_VELOCITY);

                if (speed > 0.5)
                {
                    // Calculate the angle between the player and the coin.
                    double angle = Math.Atan2(
                        player.Pos.Y - this.Pos.Y,
                        player.Pos.X - this.Pos.X
                        );

                    this.Pos.X += (float)(Math.Cos(angle) * speed);        // x component.
                    this.Pos.Y += (float)(Math.Sin(angle) * speed);        // y component.

                    // Check to see if coin can be considered collected.
                    if (Entity.IntersectsWith(this, "Shadow", player, "Shadow", gameTime))
                    {
                        // CoinSound.Play(0.05f, 0.0f, 0.0f);
                        player.Coins += this.CoinValue;
                        engine.RemoveEntity(this);
                    }
                }
            }

            base.Update(gameTime, engine);
        }
示例#12
0
 /// <summary>
 /// Called AFTER the entity is added to the engines entity list.
 /// The entity will be garuanteed to have a valid CurrentBoundingBox value and have a place in the QuadTree.
 /// </summary>
 public virtual void PostInitialize(GameTime gameTime, TeeEngine engine)
 {
 }
示例#13
0
 /// <summary>
 /// Called BEFORE the entity is added to the engines entity list.
 /// The result of the PreInitialize method determines if the entity will be added or not.
 /// </summary>
 /// <returns>bool value determining if the engine instance should add this Entity or not.</returns>
 public virtual bool PreInitialize(GameTime gameTime, TeeEngine engine)
 {
     return(true);
 }
 public virtual void Update(TeeEngine engine, GameTime gameTime)
 {
 }
示例#15
0
 /// <summary>
 /// Called AFTER the entity is added to the engines entity list.
 /// The entity will be garuanteed to have a valid CurrentBoundingBox value and have a place in the QuadTree.
 /// </summary>
 public virtual void PostCreate(GameTime gameTime, TeeEngine engine)
 {
 }
 public virtual void MapUnloaded(TeeEngine engine, TiledMap map)
 {
 }
示例#17
0
 public virtual void Update(GameTime gameTime, TeeEngine engine)
 {
 }
示例#18
0
 /// <summary>
 /// Called AFTER the entity has been removed from the engines entity list.
 /// </summary>
 public virtual void PostDestroy(GameTime gameTime, TeeEngine engine)
 {
 }
示例#19
0
        /// <summary>
        /// Uses the AStar algorithm to generate a Path from pxStart to pxEnd of valid ANodes to pass through.
        /// What is considered to be a valid ANode is determined by the current delegate method assigned to
        /// the instance's Validator property. The method will return an empty path if the location is impossible
        /// to reach from the specified start location.
        /// </summary>
        /// <returns>Path object containing a path to the specified end location.</returns>
        public Path GeneratePath(Vector2 pxStart, Vector2 pxEnd, TeeEngine engine, GameTime gameTime, NodeValidationHandler validator)
        {
            // Prevent from creating a dictionary each time this method is called.
            _openList.Clear();
            _closedList.Clear();

            Vector2 TXEND = engine.Map.PxToTx(pxEnd);
            Vector2 TXSTART = engine.Map.PxToTx(pxStart);

            if(validator == null || !validator(null, new ANode(TXEND), engine, gameTime)) return new Path();
            if(validator == null || !validator(null, new ANode(TXSTART), engine, gameTime)) return new Path();

            // Working backwards allows us to follow the parent path.
            _openList.Add(TXEND, new ANode(TXEND, null));

            while (_openList.Count > 0)
            {
                int min = Int32.MaxValue;
                ANode selectedNode = null;

                // Select the most promising node from the open list.
                foreach (ANode node in _openList.Values)
                {
                    int G = node.Length;
                    int H = (int) Math.Ceiling(Vector2.Distance(node.TxPos, TXSTART)) * 10;
                    int length = G + H;
                    if (length < min)
                    {
                        min = length;
                        selectedNode = node;
                    }
                }

                _openList.Remove(selectedNode.TxPos);
                _closedList.Add(selectedNode.TxPos, selectedNode);

                if (selectedNode.TxPos == TXSTART)
                    return new Path(selectedNode, pxStart, pxEnd);

                // Iterate through the node's neighbors.
                for (int i = -1; i < 2; i++)
                {
                    for (int j = -1; j < 2; j++)
                    {
                        if (i == 0 && j == 0) continue;

                        ANode node = new ANode(selectedNode.TxPos + new Vector2(i, j), selectedNode);

                        // If a validator has been supplied, use it to check if the current node is valid.
                        if ((validator == null || validator(selectedNode, node, engine, gameTime)) && !_closedList.ContainsKey(node.TxPos))
                        {
                            if (_openList.ContainsKey(node.TxPos))
                            {
                                if (_openList[node.TxPos].Length > node.Length)
                                    _openList[node.TxPos].SetParent(node.Parent);
                            }
                            else
                                _openList.Add(node.TxPos, node);
                        }
                    }
                }
            }

            return new Path();
        }
示例#20
0
 /// <summary>
 /// Called BEFORE the entity is added to the engines entity list.
 /// The result of the PreCreate method determines if the entity will be added or not.
 /// </summary>
 /// <returns>bool value determining if the engine instance should add this Entity or not.</returns>
 public virtual bool PreCreate(GameTime gameTime, TeeEngine engine)
 {
     return true;
 }
示例#21
0
 public override void PostInitialize(GameTime gameTime, TeeEngine engine)
 {
     engine.AddEntity(LightSource);
 }
示例#22
0
 /// <summary>
 /// Called BEFORE the entity has been removed from the engines entity list.
 /// The result of the PreDestroy method determines if the entity will be removed or not.
 /// </summary>
 /// <returns>bool value determining if the engine instance should destory this entity or not.</returns>
 public virtual bool PreDestroy(GameTime gameTime, TeeEngine engine)
 {
     return(true);
 }
示例#23
0
 /// <summary>
 /// Called AFTER the entity has been removed from the engines entity list.
 /// </summary>
 public virtual void PostDestroy(GameTime gameTime, TeeEngine engine)
 {
 }
示例#24
0
 /// <summary>
 /// Called BEFORE the entity has been removed from the engines entity list.
 /// The result of the PreDestroy method determines if the entity will be removed or not.
 /// </summary>
 /// <returns>bool value determining if the engine instance should destory this entity or not.</returns>       
 public virtual bool PreDestroy(GameTime gameTime, TeeEngine engine)
 {
     return true;
 }
示例#25
0
 public virtual void Update(GameTime gameTime, TeeEngine engine)
 {
 }
示例#26
0
 public override void PostCreate(GameTime gameTime, TeeEngine engine)
 {
     if (PostCreateEvent != null)
         PostCreateEvent(this, gameTime, engine);
 }
示例#27
0
 void OnMapZoneHit(Entity entity, TeeEngine engine, GameTime gameTime)
 {
     if (MapZoneHit != null)
         MapZoneHit(this, entity, engine, gameTime);
 }
示例#28
0
        public override void Update(GameTime gameTime, TeeEngine engine)
        {
            KeyboardState keyboardState = Keyboard.GetState();

            Vector2 movement = Vector2.Zero;
            float   prevX    = Pos.X;
            float   prevY    = Pos.Y;

            Tile  prevTile          = engine.Map.GetPxTopMostTile(Pos.X, Pos.Y);
            float moveSpeedModifier = prevTile.GetProperty <float>("MoveSpeed", 1.0f);

            // ATTACK KEY.
            if (keyboardState.IsKeyDown(Keys.A))
            {
                bool reset = !CurrentDrawableState.StartsWith("Slash");
                CurrentDrawableState = "Slash_" + Direction;

                if (reset)
                {
                    Drawables.ResetState(CurrentDrawableState, gameTime);
                }
            }
            else
            {
                // MOVEMENT BASED KEYBOARD EVENTS.
                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    CurrentDrawableState = "Walk_Up";
                    Direction            = Direction.Up;

                    movement.Y--;
                }
                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    CurrentDrawableState = "Walk_Down";
                    Direction            = Direction.Down;

                    movement.Y++;
                }
                if (keyboardState.IsKeyDown(Keys.Left))
                {
                    CurrentDrawableState = "Walk_Left";
                    Direction            = Direction.Left;

                    movement.X--;
                }
                if (keyboardState.IsKeyDown(Keys.Right))
                {
                    CurrentDrawableState = "Walk_Right";
                    Direction            = Direction.Right;

                    movement.X++;
                }

                if (KeyboardExtensions.GetKeyDownState(keyboardState, Keys.T, this, true))
                {
                    if (IsEquiped(ItemRepository.GameItems["RobeShirt"]))
                    {
                        Equip(ItemRepository.GameItems["PlateChest"]);
                    }
                    else
                    {
                        Equip(ItemRepository.GameItems["RobeShirt"]);
                    }
                }

                // Set animation to idle of no movements where made.
                if (movement.Length() == 0)
                {
                    CurrentDrawableState = "Idle_" + Direction;
                }
                else
                {
                    movement.Normalize();
                    Pos += movement * MOVEMENT_SPEED * moveSpeedModifier;
                }

                LightSource.Pos = this.Pos;
            }

            base.Update(gameTime, engine);
        }
示例#29
0
 /// <summary>
 /// Called AFTER the entity is added to the engines entity list.
 /// The entity will be garuanteed to have a valid CurrentBoundingBox value and have a place in the QuadTree.
 /// </summary>
 public virtual void PostInitialize(GameTime gameTime, TeeEngine engine)
 {
 }
示例#30
0
 public override void Update(TeeEngine engine, GameTime gameTime)
 {
     base.Update(engine, gameTime);
 }
示例#31
0
 /// <summary>
 /// Called BEFORE the entity is added to the engines entity list.
 /// The result of the PreInitialize method determines if the entity will be added or not.
 /// </summary>
 /// <returns>bool value determining if the engine instance should add this Entity or not.</returns>
 public virtual bool PreInitialize(GameTime gameTime, TeeEngine engine)
 {
     return true;
 }
示例#32
0
 public override void MapUnloaded(TeeEngine engine, TiledMap map)
 {
     base.MapUnloaded(engine, map);
 }
示例#33
0
        public override void Update(GameTime gameTime, TeeEngine engine)
        {
            // Get the Hero player for interaction purposes.
            Hero    player  = (Hero)engine.GetEntity("Player");
            Vector2 prevPos = Pos;

            // Check if this Bat has died.
            if (HP <= 0)
            {
                this.Opacity -= 0.02f;
                this.Drawables.ResetState(CurrentDrawableState, gameTime);
                if (this.Opacity < 0)
                {
                    engine.RemoveEntity(this);
                }
            }
            else
            {
                // ATTACKING LOGIC.
                if (_attackStance == AttackStance.Attacking)
                {
                    this.Pos.X           -= (float)(Math.Cos(_attackAngle) * _attackSpeed);
                    this.Pos.Y           -= (float)(Math.Sin(_attackAngle) * _attackSpeed);
                    this._attackHeight.Y += 30.0f / ATTACK_COUNTER_LIMIT;
                    this.Drawables.SetGroupProperty("Body", "Offset", _attackHeight);

                    if (Entity.IntersectsWith(this, "Shadow", player, "Shadow", gameTime))
                    {
                        player.HP -= 3;
                    }

                    if (_attackCounter++ == ATTACK_COUNTER_LIMIT)
                    {
                        _attackStance = AttackStance.NotAttacking;
                    }
                }
                // ATTACK PREPERATION LOGIC.
                else if (_attackStance == AttackStance.Preparing)
                {
                    _attackHeight.Y -= 2;

                    if (_attackHeight.Y < -40)
                    {
                        _attackHeight.Y = -40;
                        _attackAngle    = Math.Atan2(
                            this.Pos.Y - player.Pos.Y,
                            this.Pos.X - player.Pos.X
                            );
                        _attackStance  = AttackStance.Attacking;
                        _attackCounter = 0;
                    }

                    Drawables.SetGroupProperty("Body", "Offset", _attackHeight);
                }
                // NON-ATTACKING LOGIC. PATROL AND APPROACH.
                else if (_attackStance == AttackStance.NotAttacking)
                {
                    double distance = Vector2.Distance(player.Pos, this.Pos);

                    if (distance < AGRO_DISTANCE)
                    {
                        // Move towards the player for an attack move.
                        double angle = Math.Atan2(
                            player.Pos.Y - this.Pos.Y,
                            player.Pos.X - this.Pos.X
                            );

                        // Approach Function.
                        double moveValue;
                        if (distance < ATTACK_DISTANCE)
                        {
                            _attackStance = AttackStance.Preparing;
                            moveValue     = 0;
                        }
                        else
                        {
                            moveValue = _moveSpeed;
                        }

                        Pos.X += (float)(Math.Cos(angle) * moveValue);
                        Pos.Y += (float)(Math.Sin(angle) * moveValue);
                    }
                    else
                    {
                        // Perform a standard patrol action.
                        Pos.X += (float)(Math.Cos(gameTime.TotalGameTime.TotalSeconds - _randomModifier * 90) * 2);
                    }
                }

                // Determine the animation based on the change in position.
                if (Math.Abs(prevPos.X - Pos.X) > Math.Abs(prevPos.Y - Pos.Y))
                {
                    if (prevPos.X < Pos.X)
                    {
                        this.CurrentDrawableState = "Right";
                    }
                    if (prevPos.X > Pos.X)
                    {
                        this.CurrentDrawableState = "Left";
                    }
                }
                else
                {
                    if (prevPos.Y < Pos.Y)
                    {
                        this.CurrentDrawableState = "Down";
                    }
                    if (prevPos.Y > Pos.Y)
                    {
                        this.CurrentDrawableState = "Up";
                    }
                }
            }
        }
示例#34
0
 public void LargeMapZone_MapZoneHit(MapZone sender, Entity entity, TeeEngine engine, GameTime gameTime)
 {
 }
示例#35
0
 public override void PostDestroy(GameTime gameTime, TeeEngine engine)
 {
     if (PostDestroyEvent != null)
         PostDestroyEvent(this, gameTime, engine);
 }
示例#36
0
        protected override void Initialize()
        {
            Engine = new TeeEngine(this, WINDOW_WIDTH, WINDOW_HEIGHT);

            base.Initialize();
        }
示例#37
0
 public override void Update(GameTime gameTime, TeeEngine engine)
 {
     if (UpdateEvent != null)
         UpdateEvent(this, gameTime, engine);
 }
        public override void Update(GameTime gameTime, TeeEngine engine)
        {
            // Prevent from going out of range.
            if (Pos.X < 0)
            {
                Pos.X = 0;
            }
            if (Pos.Y < 0)
            {
                Pos.Y = 0;
            }
            if (Pos.X >= engine.Map.pxWidth - 1)
            {
                Pos.X = engine.Map.pxWidth - 1;
            }
            if (Pos.Y >= engine.Map.pxHeight - 1)
            {
                Pos.Y = engine.Map.pxHeight - 1;
            }

            if (TerrainCollisionEnabled && _prevTile != null)
            {
                // Iterate through each layer and determine if the tile is passable.
                int tileX = (int)Pos.X / engine.Map.TileWidth;
                int tileY = (int)Pos.Y / engine.Map.TileHeight;

                int pxTileX      = tileX * engine.Map.TileWidth;
                int pxTileY      = tileY * engine.Map.TileHeight;
                int pxTileWidth  = engine.Map.TileWidth;
                int pxTileHeight = engine.Map.TileHeight;

                Tile currentTile = engine.Map.GetPxTopMostTile(Pos.X, Pos.Y);
                bool impassable  = currentTile.HasProperty(ImpassableTerrainProperty);

                // CORRECT ENTRY AND EXIT MOVEMENT BASED ON TILE PROPERTIES
                // TODO
                // to improve structure
                // Current very very ineffecient way of checking Entry
                string[] entryPoints = currentTile.GetProperty("Entry", "Top Bottom Left Right").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                string[] exitPoints  = _prevTile.GetProperty("Entry", "Top Bottom Left Right").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                bool top    = _prevPos.Y < pxTileY;
                bool bottom = _prevPos.Y > pxTileY + pxTileHeight;
                bool left   = _prevPos.X < pxTileX;
                bool right  = _prevPos.X > pxTileX + pxTileWidth;

                // Ensure entry points.
                impassable |= top && !ContainsItem(entryPoints, "Top");
                impassable |= bottom && !ContainsItem(entryPoints, "Bottom");
                impassable |= left && !ContainsItem(entryPoints, "Left");
                impassable |= right && !ContainsItem(entryPoints, "Right");

                // Ensure exit points.
                impassable |= top && !ContainsItem(exitPoints, "Bottom");
                impassable |= bottom && !ContainsItem(exitPoints, "Top");
                impassable |= left && !ContainsItem(exitPoints, "Right");
                impassable |= right && !ContainsItem(exitPoints, "Left");

                // IF THE MOVEMENT WAS DEEMED IMPASSABLE, CORRECT IT.
                // if impassable, adjust X and Y accordingly.
                float padding = 0.001f;
                if (impassable)
                {
                    if (_prevPos.Y <= pxTileY && Pos.Y > pxTileY)
                    {
                        Pos.Y = pxTileY - padding;
                    }
                    else
                    if (_prevPos.Y >= pxTileY + pxTileHeight && Pos.Y < pxTileY + pxTileHeight)
                    {
                        Pos.Y = pxTileY + pxTileHeight + padding;
                    }

                    if (_prevPos.X <= pxTileX && Pos.X > pxTileX)
                    {
                        Pos.X = pxTileX - padding;
                    }
                    else
                    if (_prevPos.X >= pxTileX + pxTileWidth && Pos.X < pxTileX + pxTileWidth)
                    {
                        Pos.X = pxTileX + pxTileWidth + padding;
                    }
                }
            }

            _prevPos  = Pos;
            _prevTile = engine.Map.GetPxTopMostTile(Pos.X, Pos.Y);

            if (EntityCollisionEnabled)
            {
                List <Entity> intersectingEntities = engine.GetIntersectingEntities(CurrentBoundingBox);
                foreach (Entity entity in intersectingEntities)
                {
                    if (entity != this && entity is CollidableEntity)
                    {
                        CollidableEntity collidableEntity = (CollidableEntity)entity;

                        if (collidableEntity.EntityCollisionEnabled &&
                            Entity.IntersectsWith(
                                this, this.CollisionGroup,
                                collidableEntity, collidableEntity.CollisionGroup,
                                gameTime)
                            )
                        {
                            // Naive Collision Response.
                            Vector2 difference = entity.Pos - this.Pos;
                            if (difference.Length() > 0)
                            {
                                difference.Normalize();

                                if (!collidableEntity.Immovable)
                                {
                                    collidableEntity.Pos += difference;
                                }

                                if (!this.Immovable)
                                {
                                    this.Pos -= difference;
                                }
                            }
                        }
                    }
                }
            }
        }