-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
167 lines (145 loc) · 4.23 KB
/
game.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#define RAYMATH_IMPLEMENTATION
#include "raylib.h"
#include "raymath.h"
typedef enum PlayerAnimationState
{
IDLE,
WALK,
} PlayerAnimationState;
typedef struct Animation
{
int currentFrame;
int framesCounter;
int framesSpeed;
} Animation;
typedef struct Player
{
Vector2 pos;
Vector2 velocity;
int size;
float speed;
PlayerAnimationState animationState;
Rectangle frameRect;
Animation animation;
Vector2 direction;
Texture2D frontTexture;
Texture2D backTexture;
Texture2D sideTexture;
} Player;
typedef struct GameState
{
Player player;
Texture2D playerTexture;
} GameState;
void PlayAnimation(Animation *animation, Rectangle *frameRect, int textureWidth)
{
animation->framesCounter++;
if (animation->framesCounter >= (60 / animation->framesSpeed))
{
animation->framesCounter = 0;
animation->currentFrame++;
if (animation->currentFrame > 5)
animation->currentFrame = 0;
frameRect->x = (float)animation->currentFrame * (float)textureWidth / 6;
}
}
int main(void)
{
InitWindow(1280, 720, "Untitled Wave Game");
SetTargetFPS(60);
GameState gameState = {
.player = {
.velocity = Vector2Zero(),
// TODO: temporarily value
.size = 256,
.speed = 120.0f,
.animationState = IDLE,
.frameRect = {
.width = 64,
.height = 64,
},
.animation = {
.framesSpeed = 8,
},
.direction = {
.x = 1,
.y = 1,
},
.frontTexture = LoadTexture("assets/Front/Walk.png"),
.backTexture = LoadTexture("assets/Back/Walk.png"),
.sideTexture = LoadTexture("assets/Side/Walk.png"),
},
};
// Set default player texture
gameState.playerTexture = gameState.player.frontTexture;
while (!WindowShouldClose())
{
// Update player
{
if (IsKeyDown(KEY_A))
{
gameState.player.direction.x = -1;
gameState.playerTexture = gameState.player.sideTexture;
gameState.player.velocity.x -= 1.0f;
gameState.player.animationState = WALK;
gameState.player.pos.x -= gameState.player.speed * GetFrameTime();
}
if (IsKeyDown(KEY_D))
{
gameState.player.direction.x = 1;
gameState.playerTexture = gameState.player.sideTexture;
gameState.player.velocity.x += 1.0f;
gameState.player.animationState = WALK;
gameState.player.pos.x += gameState.player.speed * GetFrameTime();
}
if (IsKeyDown(KEY_W))
{
gameState.player.direction.y = -1;
gameState.playerTexture = gameState.player.backTexture;
gameState.player.velocity.y -= 1.0f;
gameState.player.animationState = WALK;
gameState.player.pos.y -= gameState.player.speed * GetFrameTime();
}
if (IsKeyDown(KEY_S))
{
gameState.player.direction.y = 1;
gameState.playerTexture = gameState.player.frontTexture;
gameState.player.velocity.y += 1.0f;
gameState.player.animationState = WALK;
gameState.player.pos.y += gameState.player.speed * GetFrameTime();
}
// gameState.player.velocity = Vector2Zero();
if (gameState.player.animationState == WALK)
{
PlayAnimation(&gameState.player.animation, &gameState.player.frameRect, gameState.playerTexture.width);
}
}
BeginDrawing();
ClearBackground(BLACK);
// Draw player
{
Rectangle playerSource = {
.x = gameState.player.frameRect.x,
.y = gameState.player.frameRect.y,
// Depends on the direction.x to either flip player sprite to left or right
.width = gameState.player.direction.x * gameState.player.frameRect.width,
.height = gameState.player.frameRect.height,
};
Rectangle playerDest = {
.x = gameState.player.pos.x,
.y = gameState.player.pos.y,
.width = gameState.player.size,
.height = gameState.player.size,
};
DrawTexturePro(gameState.playerTexture,
playerSource,
playerDest,
(Vector2){0, 0},
0,
WHITE);
}
EndDrawing();
}
CloseWindow();
return 0;
}