-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cs
More file actions
85 lines (76 loc) · 2.72 KB
/
Player.cs
File metadata and controls
85 lines (76 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using Raylib_cs;
using System.Numerics;
namespace SharpAsteroids
{
public class Player
{
List<Bullet> bullets = new List<Bullet>();
float angle;
float rotateSpeed = 3f;
Vector2 startDir = new Vector2(0f, -1f);
Vector2 dir = new Vector2();
public event Action<float, float, Vector2>? BulletSpawn;
Texture2D playerTexture;
int sizeX = 11;
int sizeY = 37;
float posX = Raylib.GetRenderWidth() / 2;
float posY = (Raylib.GetRenderHeight() / 3) * 2;
public Rectangle Rec
{
get
{
// centered hitbox, wings are not part of hitbox
int centerX = (int)(posX + (playerTexture.Width - sizeX) * 0.5f);
int centerY = (int)(posY + (playerTexture.Height - sizeY) * 0.5f);
return new Rectangle(centerX, centerY, sizeX, sizeY);
}
}
public Player(List<Bullet> b, Texture2D t)
{
this.bullets = b;
BulletSpawn += (x, y, dir) => { };
this.playerTexture = t;
}
public void Update()
{
//shoot
if (Raylib.IsKeyPressed(KeyboardKey.KEY_SPACE))
{
//centered bullet spawn
float centerX = posX + (playerTexture.Width - sizeX) * 0.5f;
float centerY = posY + (playerTexture.Height - sizeY) * 0.5f;
BulletSpawn?.Invoke(centerX, centerY, dir);
}
//movement
if (Raylib.IsKeyDown(KeyboardKey.KEY_W) || Raylib.IsKeyDown(KeyboardKey.KEY_UP))
{
posX += dir.X;
posY += dir.Y;
}
else if (Raylib.IsKeyDown(KeyboardKey.KEY_S) || Raylib.IsKeyDown(KeyboardKey.KEY_DOWN))
{
posX -= dir.X;
posY -= dir.Y;
}
if (Raylib.IsKeyDown(KeyboardKey.KEY_A) || Raylib.IsKeyDown(KeyboardKey.KEY_LEFT))
{
angle -= rotateSpeed;
dir = Raymath.Vector2Rotate(startDir, Raylib.DEG2RAD * angle);
}
else if (Raylib.IsKeyDown(KeyboardKey.KEY_D) || Raylib.IsKeyDown(KeyboardKey.KEY_RIGHT))
{
angle += rotateSpeed;
dir = Raymath.Vector2Rotate(startDir, Raylib.DEG2RAD * angle);
}
//if window border is reached, move to other side
if (posY < 0) posY = 480;
if (posY > 480) posY = 0;
if (posX < 0) posX = 800;
if (posX > 800) posX = 0;
}
public void Draw()
{
Raylib.DrawTextureEx(playerTexture, new(posX, posY), angle, 1, Color.WHITE);
}
}
}