Ejemplo n.º 1
0
        public override void Initialize(World world)
        {
            base.Initialize(world);

            //Make it so we can be possesed
            Possesable = true;

            //Create the input controller
            inputController = new InputController();

            //Attach us to the input controller
            bool attached = inputController.Posses(this);

            if (!attached)
            {
                throw new System.InvalidOperationException("Failed to attach input controller to player entity!");
            }

            //Create the camera to represent the player
            PlayerCamera = EntityManager.CreateEntity <ECamera>(OwningWorld, true);
            CameraManager.SetActiveCamera(PlayerCamera);

            //Create the sprite that represents us
            animation = AddComponent <CSpriteAnimation>();
            animation.SetupAnimation(PlayerSprite, 24, 1);
            animation.SetAnimationFrames(animations["idle"]);
            currentAnimation = "idle";
            animation.SetSpeed(2);
            animation.Play();

            transform.Scale = new Vector2(4, 4);

            //CreateSpherePhysics(10);
            PhysicsObject b = CreateSpherePhysics(32, ChipmunkSharp.cpBodyType.DYNAMIC, 10000, 1000000000);
        }
Ejemplo n.º 2
0
        public override void Initialize(World world)
        {
            base.Initialize(world);

            //Make it so we can be possesed
            Possesable = true;

            //Create the input controller
            inputController = new InputController();

            //Attach us to the input controller
            bool attached = inputController.Posses(this);

            if (!attached)
            {
                throw new System.InvalidOperationException("Failed to attach input controller to player entity!");
            }

            //Create the camera to represent the player
            PlayerCamera = EntityManager.CreateEntity <ECamera>(OwningWorld, true);
            CameraManager.SetActiveCamera(PlayerCamera);

            //Create the sprite that represents us
            animation = AddComponent <CSpriteAnimation>();
            animation.SetupAnimation(ControllerSprite, 24, 1);
            animation.SetAnimationFrames(new int[] { 1, 2, 3, 4 });
            animation.SetSpeed(2);
            animation.Play();

            transform.Scale = new Vector2(4, 4);
        }
Ejemplo n.º 3
0
        public override void Tick(World world, GameTime gameTime)
        {
            Console.WriteLine(this.transform.Position);

            //PhysicalBody.Body.UpdatePosition((float)gameTime.ElapsedGameTime.TotalSeconds);


            //Tick the input controller
            inputController.TickInput(gameTime);


            float zoomLerpSpeed = 0.1f;



            //Move the player in the direction
            if (MoveDirection.Length() != 0)
            {
                playerAcceleration = MathHelper.Lerp(playerAcceleration, Speed, Acceleration * ((float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f));

                Vector2 result = (MoveDirection * playerAcceleration * (float)gameTime.ElapsedGameTime.TotalMilliseconds);

                MoveDirection.Normalize();
                PhysicalBody.Body.SetVelocity(new ChipmunkSharp.cpVect(result.X, PhysicalBody.Body.GetVelocity().y));
                //this.transform.Position += (MoveDirection * playerAcceleration * (float)gameTime.ElapsedGameTime.TotalMilliseconds);

                //Handle flipping our animation based on our move direction
                float dot = Vector2.Dot(MoveDirection, new Vector2(1, 0));
                if (dot < 0)
                {
                    animation.ShouldFlipAnimation = true;
                }
                else if (dot != 0)
                {
                    animation.ShouldFlipAnimation = false;
                }
                else
                {
                    runType = "run_sideways";
                }

                if (dot != 0)
                {
                    runType = "run";
                }

                //Clear move direction
                MoveDirection = Vector2.Zero;
            }
            else
            {
                PhysicalBody.Body.SetVelocity(new ChipmunkSharp.cpVect(0, PhysicalBody.Body.GetVelocity().y));
                playerAcceleration = MathHelper.Lerp(playerAcceleration, 0, Acceleration * ((float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f));
                zoomLerpSpeed      = 0.2f;
            }

            base.Tick(world, gameTime);

            //Animation switching
            if (playerAcceleration > 0.1 && currentAnimation != runType)
            {
                animation.SetAnimationFrames(animations[runType]);
                currentAnimation = runType;
                animation.Reset();
            }

            if (playerAcceleration < 0.1 && currentAnimation != "idle")
            {
                animation.SetAnimationFrames(animations["idle"]);
                currentAnimation = "idle";
            }

            //Update camera to follow player?

            PlayerCamera.transform.Position = Vector2.Lerp(PlayerCamera.transform.Position, this.transform.Position, 2 * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f);
            PlayerCamera.transform.Rotation = (float)Math.Sin(gameTime.TotalGameTime.TotalMilliseconds / 800) / 64.0f;

            ChipmunkSharp.cpVect vel = PhysicalBody.Body.GetVelocity();

            float zoom = MathHelper.Lerp(0.2f, 1f, (float)Math.Clamp(((vel.x * vel.x) + (vel.y * vel.y)) / Speed, 0, 1.0f));

            camZoom = MathHelper.Lerp(camZoom, zoom, zoomLerpSpeed * ((float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f));
            PlayerCamera.transform.Scale = new Vector2(camZoom * 1000, camZoom * 1000);
        }