Esempio n. 1
0
        public BasicEnemy(Vector2 pos, TextureInfo tex)
        {
            Position = pos;

            sprite             = new SpriteTile();
            sprite.TextureInfo = tex;
            sprite.Position    = pos;
            sprite.TileIndex2D = new Vector2i(0, 0);
            sprite.CenterSprite(new Vector2(0.4f, 0.5f));
            sprite.Scale = new Vector2(2.0f, 2.0f);

            //set up the random vector
            randomMovement = Support.rand.NextVector2(-0.01f, 0.01f);

            //update the animation frames
            sprite.Schedule((dt) => {
                if (FrameCount % 2 == 0)
                {
                    //if attacking,use all animation frames
                    if (attacking)
                    {
                        //deal damage to the player
                        if (FrameCount % damageDelay == 0)
                        {
                            Player.Instance.Health -= damage;
                        }

                        animationFrame = (animationFrame + 1) % 25;
                    }
                    else
                    {
                        //if not,then use first three animation frames
                        animationFrame = (animationFrame + 1) % 4;
                    }
                    //assign the correct tileindex
                    sprite.TileIndex1D = animationFrame;

                    //if close to the player,then follow,otherwise move randomly
                    if (Player.Instance.Position.Distance(sprite.Position) < 6.0f &&
                        Collisions.checkLineOfSight(this, Player.Instance))
                    {
                        isMovingRandomly = false;
                        step             = (Player.Instance.Position - sprite.Position).Normalize() / 15.0f;

                        //check if should be attacking
                        if (Collisions.checkCollisionBetweenEntities(this, Player.Instance))
                        {
                            //check if is not attacking already
                            if (attacking == false)
                            {
                                attacking = true;
                                //manualy skip the frames
                                animationFrame = 4;
                            }
                        }
                        else
                        {
                            //make sure that attacking is set to false
                            attacking = false;
                        }
                    }
                    else
                    {
                        //player is not within range, move randomly
                        isMovingRandomly = true;
                        step             = randomMovement;
                    }
                }

                //advance the position by the given Vector2 step
                sprite.Position += step;

                //only move when not attacking
                if (!attacking)
                {
                    //collision detection on the X axis

                    //create a vector 2 containing a proposed change to the position
                    Vector2 proposedChange = new Vector2(step.X, 0.0f) * speedModifier;
                    //temporary game entity to contain the entity the enemy might have collided with
                    GameEntity tempEntity;

                    //check wall collisions and then collisions with other enemies
                    if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange) && !Collisions.efficientCollisionsCheck(this, proposedChange, out tempEntity))
                    {
                        //no collision, so we can change the position
                        Position += proposedChange / speedModifier;
                    }
                    else if (isMovingRandomly)
                    {
                        //collided with something,but if the enemy should be moving randomly, then let it move
                        randomMovement.X = -randomMovement.X;
                    }


                    //collision detection on the Y axis

                    proposedChange = new Vector2(0.0f, step.Y) * speedModifier;
                    if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange) && !Collisions.efficientCollisionsCheck(this, proposedChange, out tempEntity))
                    {
                        Position += proposedChange / speedModifier;
                    }
                    else if (isMovingRandomly)
                    {
                        randomMovement.Y = -randomMovement.Y;
                    }


                    //position changed, so we need to remove the entity from the QuadTree and add again
                    //this is because the entity might be in the wrong place in the QuadTree and removing and re-adding is the only way to fix that
                    Game.Instance.quadTree.removeEntity(this);
                    Game.Instance.quadTree.insert(this);
                }



                //rotate the sprite to face the direction of walking
                var angleInRadians = -FMath.Atan2(step.X, step.Y);
                sprite.Rotation    = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians));

                //correct for the fact that the sprite is rotated in the texture file
                sprite.Rotation = sprite.Rotation.Rotate(90.0f);
            }, -1);

            //create a shadow texture - temporarily disabled
            //SpriteUV shadow = new SpriteUV(new TextureInfo(Bullet.fireTexture));
            //shadow.CenterSprite(new Vector2(0.5f,0.5f));
            //shadow.Color = Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.SetAlpha(Colors.Black,0.5f);


            //calculate the bounds for the entire sprite
            bounds = new Bounds2();
            sprite.GetlContentLocalBounds(ref bounds);

            bounds = new Bounds2(bounds.Min * 0.5f, bounds.Max * 0.5f);
        }