-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
215 lines (181 loc) · 6.52 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
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
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
Mix_Chunk *jump = NULL;
Mix_Chunk *die = NULL;
#include "scene.h"
#include "player.h"
#include "util.h"
Object *tempPlayerAccessObject = NULL;
Object *tempOtherAccessObject = NULL;
unsigned long playerID;
SDL_Rect bullet;
void CreatePlayer(int x, int y, int width, int height, const char *URL){
bullet.x = 0;
bullet.y = 0;
bullet.w = 2;
bullet.h = 2;
SDL_Rect worldPlayer;
worldPlayer.x = x;
worldPlayer.y = y;
worldPlayer.w = width;
worldPlayer.h = height;
playerID = AddObject(TYPE_PLAYER, worldPlayer, (SDL_Rect)findSheet(192, 96, 3, 6, 0), URL);//need types for collision later
tempPlayerAccessObject = GetObject(playerID);
tempPlayerAccessObject->hasPhysics = 1;
tempPlayerAccessObject->hasCollision = 1;
jump = Mix_LoadWAV("data/global/Jump.wav");
die = Mix_LoadWAV("data/global/die_bugdeath.wav");
//modify something?
}
void UpdatePlayer(){//dont worry about collision or physics here. Just accellerate the player
//for the font renderer, set the type to be font
static int jumped = 0;
static int delay = 0, i = 0;
tempPlayerAccessObject = GetObject(playerID);
if(global.isNoclip){//no physics enabled or collision
tempPlayerAccessObject->hasCollision = 0;
tempPlayerAccessObject->hasPhysics = 0;
if(global.playerMoveLeft){
tempPlayerAccessObject->objectVelX = -2.5;
tempPlayerAccessObject->isFlipped = 1;
}
if(global.playerMoveRight){
tempPlayerAccessObject->objectVelX = 2.5;
tempPlayerAccessObject->isFlipped = 0;
}
if(global.playerJump){
tempPlayerAccessObject->objectVelY = -2.5;
}
if(global.playerCrouch){
tempPlayerAccessObject->objectVelY = 2.5;
}
if(global.playerJump == 0 && global.playerCrouch == 0){
tempPlayerAccessObject->objectVelY = 0;
} else if(global.playerJump == 1 && global.playerCrouch == 1){
tempPlayerAccessObject->objectVelY = 0;
}
if(global.playerMoveLeft == 0 && global.playerMoveRight == 0){
tempPlayerAccessObject->objectVelX = 0;
}else if(global.playerMoveLeft == 1 && global.playerMoveRight == 1){
tempPlayerAccessObject->objectVelX = 0;
}
} else{
tempPlayerAccessObject->hasCollision = 1;
tempPlayerAccessObject->hasPhysics = 1;
if(global.playerMoveLeft){//==================================================
if(abs(tempPlayerAccessObject->objectVelX) <= 1.8){
tempPlayerAccessObject->objectVelX -= 1.8;
}
if(global.playerHasGun){
if(delay == 0){
delay = 5;
if(i < 9){
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, i);
i++;
} else{
i = 6;
}
} else{
delay--;
}
} else{
if(delay == 0){
delay = 5;
if(i < 4){
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, i);
i++;
} else{
i = 0;
}
} else{
delay--;
}
}
tempPlayerAccessObject->isFlipped = 1;
}
if(global.playerMoveRight){//==================================================
if(abs(tempPlayerAccessObject->objectVelX) <= 1.8){
tempPlayerAccessObject->objectVelX += 1.8;
}
if(global.playerHasGun){
if(delay == 0){
delay = 5;
if(i < 9){
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, i);
i++;
} else{
i = 6;
}
} else{
delay--;
}
} else{
if(delay == 0){
delay = 5;
if(i < 4){
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, i);
i++;
} else{
i = 0;
}
} else{
delay--;
}
}
tempPlayerAccessObject->isFlipped = 0;
}
if(global.playerJump){//==================================================
if(!jumped && global.playerCollided){
tempPlayerAccessObject->objectVelY -= 8.5;
jumped = 1;
Mix_PlayChannel(-1, jump, 0);
if(global.playerHasGun){
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, 10);
} else{
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, 4);
}
} else{
jumped = 0;
}
//make sure it has collided with the ground again before jumping
}
if(global.playerCrouch){//==================================================
//tempPlayerAccessObject->objectVelY += 2.5;
if(global.playerHasGun){
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, 11);
} else{
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, 5);
}
}
if(!global.playerCrouch && !global.playerJump && !global.playerMoveLeft && !global.playerMoveRight && !global.inCinematic){//because its at the end of the player handler
if(global.playerHasGun){
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, 6);
} else{
tempPlayerAccessObject->textureSelect = findSheet(192, 96, 3, 6, 0);
}
}
if(global.playerUse && global.playerHasGun){//==================================================
//Mix_PlayChannel(-1, die, 0);
unsigned long tID = AddObject(TYPE_BULLET, bullet, (SDL_Rect)findSheet(16, 16, 1, 1, 0), "data/global/gunshot.png");
tempOtherAccessObject = GetObject(tID);
tempOtherAccessObject->hasPhysics = 1;
tempOtherAccessObject->hasCollision = 0;
tempOtherAccessObject->objectCollision.x = tempPlayerAccessObject->objectCollision.x;
tempOtherAccessObject->objectCollision.y = tempPlayerAccessObject->objectCollision.y;
tempOtherAccessObject->objectVelY -= abs(rand() % 3);
if(tempPlayerAccessObject->isFlipped){
tempOtherAccessObject->objectVelX = -12;
} else{
tempOtherAccessObject->objectVelX = 12;
}
//printf("pew\n");
}
}//TODO TODO update tile animations
tempPlayerAccessObject = GetObject(playerID);
global.cameraX = (float)tempPlayerAccessObject->objectCollision.x - ((global.cameraWidth/2) - (float)(tempPlayerAccessObject->objectCollision.w/2));// / tempPlayerAccessObject->objectCollision.w;
global.cameraY = (float)tempPlayerAccessObject->objectCollision.y - ((global.cameraHeight/2) - (float)(tempPlayerAccessObject->objectCollision.h/2));// / tempPlayerAccessObject->objectCollision.h;
//tempPlayerAccessObject->objectCollision.x += (int)tempPlayerAccessObject->objectVelX;
//tempPlayerAccessObject->objectCollision.y += (int)tempPlayerAccessObject->objectVelY;
//printf("%d %f %d %f\n", tempPlayerAccessObject->objectCollision.x, tempPlayerAccessObject->objectVelX, tempPlayerAccessObject->objectCollision.y, tempPlayerAccessObject->objectVelY);
}