-
Notifications
You must be signed in to change notification settings - Fork 3
/
MOVEMENT AND SHOOTING.c
231 lines (194 loc) · 7.64 KB
/
MOVEMENT AND SHOOTING.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "raylib.h"
#define MAX_FRAME_SPEED 15
#define MIN_FRAME_SPEED 1
#define NUM_SHOOTS 99999
#define LIMITE_ESQUERDA -2400
#define LIMITE_DIREITA 4800
#define LIMITE_CIMA -1350
#define LIMITE_BAIXO 2700
#define LIMITE_ZOOM_MAX 1.5f
#define LIMITE_ZOOM_MIN 0.3f
typedef enum ShootDirection {CIMA, BAIXO, DIREITA, ESQUERDA} ShootDirection;
typedef struct Shoot
{
Rectangle rect;
Vector2 speed;
bool active;
Color color;
ShootDirection direction;
} Shoot;
typedef struct Player
{
Vector2 position;
Vector2 speed;
} Player;
const int screenWidth = 800; //DIZ O TAMANHO DA JANELA
const int screenHeight = 450;
static Shoot shoot[NUM_SHOOTS];
static Player player;
static int shootRate;
int shootCounter = 0;
ShootDirection shootDirection = DIREITA;
int main(void){
int currentframe = 0;
int framesCounter = 0;
int frameSpeed = 15;
player.position.x = 350.0f; //INICIALIZA AS CARACTERISTICAS DO PERSONAGEM
player.position.y = 250.0f;
player.speed.x = 7;
player.speed.y = 7;
Camera2D camera = { 0 };
camera.target = (Vector2) {player.position.x + 20, player.position.y + 20};
camera.offset = (Vector2) {screenWidth/2.0f, screenHeight/2.0f};
camera.rotation = 0.0f;
camera.zoom = 0.7f;
for (int i = 0; i < NUM_SHOOTS; i++) //INICIALIZA AS CARACTERISTICAS DA BALA
{
shoot[i].rect.x = player.position.x;
shoot[i].rect.y = player.position.y;
shoot[i].rect.width = 10;
shoot[i].rect.height = 10;
shoot[i].speed.x = 10;
shoot[i].speed.y = 10;
shoot[i].active = false;
shoot[i].color = MAROON;
}
int direction;
InitWindow(screenWidth, screenHeight, "making a character move "); //INICIALIZA A TELA
Texture2D character = LoadTexture("resourses\\SpriteSheetRight.png"); //CARREGA A TEXTURA DO PERSONAGEM
Texture2D background = LoadTexture("resourses\\cenario.png");
background.width = 3*background.width;
background.height = 3*background.height;
Rectangle frameRec = {0.0f, 0.0f, (float)character.width/4 ,(float)character.height}; //RECORTA A TEXTURA
Rectangle frameRecXInverted = {0.0f, 0.0f, (float)-character.width/4 ,(float)character.height}; //ESPELHA A TEXTURA
SetTargetFPS(60);
while(!WindowShouldClose()){ //DETECTA SE A TELA FOI FECHADA OU SE APERTARAM ESC
framesCounter ++;
camera.target = (Vector2) {player.position.x, player.position.y};
camera.zoom += ((float)GetMouseWheelMove()*0.05f);
if (camera.zoom >= LIMITE_ZOOM_MAX) camera.zoom = LIMITE_ZOOM_MAX;
if (camera.zoom <= LIMITE_ZOOM_MIN) camera.zoom = LIMITE_ZOOM_MIN;
if(IsKeyDown(KEY_RIGHT)){ //MOVIMENTA O PERSONAGEM PRA DIREITA
shootDirection = DIREITA;
direction = 1;
player.position.x += player.speed.x;
if (player.position.x >= LIMITE_DIREITA) player.position.x = LIMITE_DIREITA;
if(framesCounter >= (60/frameSpeed)){
framesCounter = 0;
currentframe++;
if(currentframe>4)
currentframe=0;
frameRec.x = (float)currentframe*(float)character.width/4;
}
}
if(IsKeyDown(KEY_LEFT)){ //MOVIMENTA O PERSONAGEM PRA ESQUERDA
shootDirection = ESQUERDA;
direction = 0;
player.position.x -= player.speed.x;
if (player.position.x <= LIMITE_ESQUERDA) player.position.x = LIMITE_ESQUERDA;
if(framesCounter >= (60/frameSpeed)){
framesCounter = 0;
currentframe++;
if(currentframe>4)
currentframe=0;
frameRecXInverted.x = (float)currentframe*(float)character.width/4;
}
}
if(IsKeyDown(KEY_UP)){ //MOVIMENTA O PERSONAGEM PRA CIMA
shootDirection = CIMA;
player.position.y -= player.speed.y;
if (player.position.y <= LIMITE_CIMA) player.position.y = LIMITE_CIMA;
if(framesCounter >= (60/frameSpeed)){
framesCounter = 0;
currentframe++;
if(currentframe>4)
currentframe=0;
if(shootDirection == 0)
frameRecXInverted.x = (float)currentframe*(float)character.width/4;
else
frameRec.x = (float)currentframe*(float)character.width/4;
}
}
if(IsKeyDown(KEY_DOWN)){ //MOVIMENTA O PERSONAGEM PRA BAIXO
shootDirection = BAIXO;
player.position.y += player.speed.y;
if (player.position.y >= LIMITE_BAIXO) player.position.y = LIMITE_BAIXO;
if(framesCounter >= (60/frameSpeed)){
framesCounter = 0;
currentframe++;
if(currentframe>4)
currentframe=0;
if(direction == 0)
frameRecXInverted.x = (float)currentframe*(float)character.width/4;
else
frameRec.x = (float)currentframe*(float)character.width/4;
}
}
if (IsKeyDown(KEY_SPACE))
{
shootRate += 5;
shootCounter++;
for (int i = 0; i < NUM_SHOOTS; i++)
{
if (!shoot[i].active && shootRate % 40 == 0) //LIMITA A QUANTIDADE DE BALAS/SEG
{
shoot[i].rect.x = player.position.x + 50; //SPAWNA A BALA NESSAS COORDENADAS
shoot[i].rect.y = player.position.y+80;
shoot[i].active = true;
shoot[i].direction = shootDirection;
break;
}
}
}
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera);
//DrawTexture(background, 0, 0, WHITE);
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
DrawTexture(background, i*background.width, j*background.height, WHITE);
}
} //desenhar background 9 vezes
for (int i = 0; i < NUM_SHOOTS; i++){
if (shoot[i].active){
//MOVIMENTA A BALA
switch (shoot[i].direction)
{
case CIMA:
shoot[i].rect.y -= shoot[i].speed.y;
break;
case BAIXO:
shoot[i].rect.y += shoot[i].speed.y;
break;
case DIREITA:
shoot[i].rect.x += shoot[i].speed.x;
break;
case ESQUERDA:
shoot[i].rect.x -= shoot[i].speed.x;
break;
default:
break;
}
if (shoot[i].rect.x >= LIMITE_DIREITA || shoot[i].rect.x <= LIMITE_ESQUERDA ||shoot[i].rect.y >= LIMITE_BAIXO || shoot[i].rect.y <= LIMITE_CIMA){ //EXCLUI A BALA DEPOIS QUE SAI DA TELA
shoot[i].active = false;
shootRate = 0;
}
}
}
for (int i = 0; i < NUM_SHOOTS; i++){
if (shoot[i].active)
DrawRectangleRec(shoot[i].rect, shoot[i].color); //DESENHA A BALA
}
if((direction == 0)){
DrawTextureRec(character, frameRecXInverted, player.position, RAYWHITE);
}
else
DrawTextureRec(character, frameRec, player.position, RAYWHITE);
EndMode2D();
DrawText(TextFormat("x: %d, y: %d", (int) player.position.x, (int) player.position.y), 50, 400, 20, WHITE);
EndDrawing();
}
UnloadTexture(character);
CloseWindow();
return 0;
}