-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.c
69 lines (63 loc) · 2.86 KB
/
Player.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
//
// Created by Mathi on 5/29/2024.
//
#include "Player.h"
Player* createPlayer(int posX, int posY, int size, int FOV) {
Player* newPlayer = (Player*) malloc(sizeof(Player));
newPlayer->posX = posX;
newPlayer->posY = posY;
newPlayer->size = size;
newPlayer->FOV = FOV;
return newPlayer;
}
void showPlayer(Player* player, SDL_Renderer* renderer) {
SDL_RenderDrawPoint(renderer, player->posX, player->posY);
}
void movePlayer(Player *firstPlayer, int speed, const Uint8 *keyState, SDL_Rect *playerRect, SDL_FPoint* viewPoint, float angle) {
if (keyState[SDL_GetScancodeFromKey(SDLK_z)] || keyState[SDL_GetScancodeFromKey(SDLK_UP)]) {
firstPlayer->posY += speed*sin(angle);
firstPlayer->posX += speed*cos(angle);
(*playerRect).y += speed*sin(angle);
(*playerRect).x += speed*cos(angle);
viewPoint->y += speed*sin(angle);
viewPoint->x += speed*cos(angle);
}
if (keyState[SDL_GetScancodeFromKey(SDLK_s)] || keyState[SDL_GetScancodeFromKey(SDLK_DOWN)]) {
firstPlayer->posY -= speed*sin(angle);
firstPlayer->posX -= speed*cos(angle);
(*playerRect).y -= speed*sin(angle);
(*playerRect).x -= speed*cos(angle);
viewPoint->y -= speed*sin(angle);
viewPoint->x -= speed*cos(angle);
}
if (keyState[SDL_GetScancodeFromKey(SDLK_q)] || keyState[SDL_GetScancodeFromKey(SDLK_RIGHT)]) {
firstPlayer->posY += speed*cos(angle);
firstPlayer->posX += speed*sin(angle);
(*playerRect).y += speed*cos(angle);
(*playerRect).x += speed*sin(angle);
viewPoint->y += speed*cos(angle);
viewPoint->x += speed*sin(angle);
}
if (keyState[SDL_GetScancodeFromKey(SDLK_d)] || keyState[SDL_GetScancodeFromKey(SDLK_LEFT)]) {
firstPlayer->posY -= speed*cos(angle);
firstPlayer->posX -= speed*sin(angle);
(*playerRect).y -= speed*cos(angle);
(*playerRect).x -= speed*sin(angle);
viewPoint->y -= speed*cos(angle);
viewPoint->x -= speed*sin(angle);
}
}
void calculateAnglePointPlayer(const Player *firstPlayer, int angle, float x, float y, float xPlayerPosView,
float yPlayerPosView, SDL_FPoint *viewPointWall, SDL_FPoint *viewPointPlayer) {
(*viewPointWall).x = (cosf((angle * PI / 180)) * x) - (sinf(angle * PI / 180) * y);
(*viewPointWall).y = (cosf(angle * PI / 180) * y) + (sinf(angle * PI / 180) * x);
(*viewPointPlayer).x = firstPlayer->posX+25+((cosf((angle*PI/180))*xPlayerPosView) - (sinf(angle*PI/180)*yPlayerPosView));
(*viewPointPlayer).y = firstPlayer->posY+25+((cosf(angle*PI/180)*yPlayerPosView) + (sinf(angle*PI/180)*xPlayerPosView));
}
void changeAngle(int* angle, const Uint8* keyState) {
if (keyState[SDL_GetScancodeFromKey(SDLK_a)]) {
(*angle)--;
} else if (keyState[SDL_GetScancodeFromKey(SDLK_e)]) {
(*angle)++;
}
}