Last active
July 25, 2020 19:01
-
-
Save MarinMario/21efaee2a0985a9d2a7a61a49d5e57a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player | |
{ | |
Texture2D texture; | |
public Vector2 position; | |
Vector2 velocity = Vector2.Zero; | |
int speed = 300; | |
int acceleration = 600; | |
int friction = 1000; | |
public Player(Texture2D _texture, Vector2 _position) | |
{ | |
texture = _texture; | |
position = _position; | |
} | |
public void Update(float delta) | |
{ | |
Vector2 inputVector = Vector2.Zero; | |
if (Keyboard.GetState().IsKeyDown(Keys.D)) | |
inputVector.X = 1; | |
if (Keyboard.GetState().IsKeyDown(Keys.A)) | |
inputVector.X = -1; | |
if (Keyboard.GetState().IsKeyDown(Keys.W)) | |
inputVector.Y = -1; | |
if (Keyboard.GetState().IsKeyDown(Keys.S)) | |
inputVector.Y = 1; | |
if (inputVector != Vector2.Zero) | |
{ | |
inputVector /= inputVector.Length(); | |
velocity += MoveVector(velocity, inputVector * speed, acceleration * delta); | |
} | |
else | |
velocity += MoveVector(velocity, Vector2.Zero, friction * delta); | |
position += velocity * delta; | |
} | |
public void Draw(SpriteBatch spriteBatch) | |
{ | |
spriteBatch.Draw(texture, position, Color.White); | |
} | |
private Vector2 MoveVector(Vector2 vector, Vector2 target, float amount) | |
{ | |
Vector2 direction = (target - vector) / (target - vector).Length(); | |
if (vector != target) | |
return direction * amount; | |
return Vector2.Zero; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment