Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace EngineTest
- {
- public class Player : Entity
- {
- public Player(Texture2D texture)
- : base(texture)
- {
- }
- private Vector2 move;
- private float grav = 0.3f;
- private Int32 input_left;
- private Int32 input_right;
- private bool input_up;
- public Vector2 speed = new Vector2(2f, -4f);
- public override void Update(GameTime gameTime, List<Entity> entities)
- {
- input_left = Convert.ToInt32(Keyboard.GetState().IsKeyDown(Keys.A));
- input_right = Convert.ToInt32(Keyboard.GetState().IsKeyDown(Keys.D));
- input_up = Keyboard.GetState().IsKeyDown(Keys.Space);
- Move();
- foreach (var entity in entities) //Looks at all entities created
- {
- if (entity == this) //If it is player, don't check collision
- continue;
- //Horizontal Collision
- if (PlaceMeeting(new Vector2(Position.X + Velocity.X, Position.Y), entity))
- {
- while (!PlaceMeeting(new Vector2(Position.X + Math.Sign(Velocity.X), Position.Y), entity))
- {
- Position.X += Math.Sign(Velocity.X);
- }
- Velocity.X = 0;
- }
- //Vertical Collision
- if (PlaceMeeting(new Vector2(Position.X, Position.Y + Velocity.Y), entity)) // checking if it's going to collide in 1 step
- {
- while (!PlaceMeeting(new Vector2(Position.X, Position.Y + Math.Sign(Velocity.Y)), entity))// checking if it's not going to collide in 1 pixel
- {
- Position.Y += Math.Sign(Velocity.Y); // incrementing pixel By pixel until it's going to collide in 1 pixel
- }
- Velocity.Y = 0; //stoping the player
- }
- }
- Position += Velocity;
- }
- private void Move()
- {
- move.X = (input_right - input_left);
- Velocity.X = move.X * speed.X;
- Velocity.Y += grav;
- if (input_up)
- Velocity.Y = speed.Y;
- }
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement