aboutsummaryrefslogtreecommitdiff
path: root/src/engine/entity.c
blob: 1cb88beaa2f750ea7d710d47733ad6460bbaab63 (plain)
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
232
233
234
235
236
#include <stdbool.h>

#include "engine/entity.h"
#include "engine/maths.h"

/*
    PLAYER: (pos on X)
    ,___,
    |   |
    | X |
    |___|

*/

bool hh_collision(vec_cor pos1, vec2 pos2){
    if (pos2.x == CLAMP(pos2.x, pos1.x, pos1.x+16)){// hit x
        return true;
    }

    if (pos2.y == CLAMP(pos2.y, pos1.y, pos1.y+16)){// hit y
        return true;
    }
    return false;
}

void hh_solve_collision(vec2 pos_environment, hh_entity* entity){
	if (!hh_collision(pos_environment,entity->pos))
		return;

	printf("BONK!/n");
	// if (entity->vel.y > 0){
	// 	entity->pos.y = MAX(entity->pos.y,pos_environment.y);
	// 	entity->vel.y = 0;
	// } else {
	// 	entity->pos.y = MIN(entity->pos.y,pos_environment.y);
	// 	entity->vel.y = 0;
	// }
	// if (entity->vel.x <= 0){
	// 	entity->pos.x = MIN(entity->pos.x,pos_environment.x-16);
	// 	entity->vel.x = 0;
	// } else {
	// 	entity->pos.x = MAX(entity->pos.x,pos_environment.x+16);
	// 	entity->vel.x = 0;
	// } 
}
hh_entity hh_background_collision (hh_entity temp_old_entity,hh_entity temp_new_entity){

		temp_new_entity.is_grounded = false;
 
// solves x collision
	if (temp_old_entity.vel.x <= 0) {
		if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_old_entity.pos.y + 0})) || 
			hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) {
			temp_new_entity.pos.x = (temp_new_entity.pos.x & ~15) + 16,
			temp_new_entity.vel.x = 0;
		}
	} else {
		if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + temp_old_entity.size.x, .y=temp_old_entity.pos.y + 0})) || 
			hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + temp_old_entity.size.x, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) {
			temp_new_entity.pos.x = temp_new_entity.pos.x & ~15, // <-- magic comma, NOT TOUCHY
			temp_new_entity.vel.x = 0;
		}
	}

	//solves y collision
	if (temp_old_entity.vel.y <= 0) {
		if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_new_entity.pos.y + 0})) || 
			hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + (temp_new_entity.size.x-1), .y=temp_new_entity.pos.y + 0}))) {
			temp_new_entity.pos.y = (temp_new_entity.pos.y & ~15) + 16,
			temp_new_entity.vel.y = 0;
		}
	} else {
		if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_new_entity.pos.y + (temp_new_entity.size.y-1)})) || 
			hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + (temp_new_entity.size.x-1), .y=temp_new_entity.pos.y + (temp_new_entity.size.y-1)}))) {
			temp_new_entity.pos.y = temp_new_entity.pos.y & ~15,
			temp_new_entity.vel.y = 0;
			temp_new_entity.is_grounded = true;
		}
	}
	temp_old_entity = temp_new_entity;
	// temp_old_entity.is_grounded = temp_new_entity.is_grounded;
	// temp_old_entity.pos = temp_new_entity.pos; 
	// temp_old_entity.vel = temp_new_entity.vel;
	return temp_old_entity;
}

bool hh_background_collision_bulllet (hh_entity temp_old_entity){

	//	temp_new_entity.is_grounded = false;
 
// solves x collision
	if (temp_old_entity.vel.x <= 0) {
		if (hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + 0, .y=temp_old_entity.pos.y + 0})) || 
			hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + 0, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) {
				return true;
		}
	} else {
		if (hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + temp_old_entity.size.x, .y=temp_old_entity.pos.y + 0})) || 
			hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + temp_old_entity.size.x, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) {
				return true;
		}
	}

	//solves y collision
	if (temp_old_entity.vel.y <= 0) {
		if (hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + 0, .y=temp_old_entity.pos.y + 0})) || 
			hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + (temp_old_entity.size.x-1), .y=temp_old_entity.pos.y + 0}))) {
			return true;
		}
	} else {
		if (hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + 0, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)})) || 
			hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + (temp_old_entity.size.x-1), .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) {
				return true;
		}
	}
	// temp_old_entity = temp_new_entity;
	// temp_old_entity.is_grounded = temp_new_entity.is_grounded;
	// temp_old_entity.pos = temp_new_entity.pos; 
	// temp_old_entity.vel = temp_new_entity.vel;
	return false;
}
hh_entity hh_enemy_collision(hh_entity temp_player, hh_entity temp_enemy){
	
	bool collide = hh_distance_circles( temp_player.pos,  temp_enemy.pos, temp_player.radius, temp_enemy.radius);

	if (collide == true && temp_player.is_hit == false)
	{
		temp_player.is_hit = true;
		//angle = atan2( tempEntity.pos_y - tempPlayer.pos_y, tempEntity.pos_x - tempPlayer.pos_x);
		if(temp_player.pos.x <= temp_enemy.pos.x) //player left of enemy -- creates flinch movement L or R
		{
		//	printf("BONK-left!/n");
			temp_player.vel.y = -5;
			temp_player.vel.x = -8;
		}	else {
		//	printf("BONK-right!/n");
			temp_player.vel.y = -5;
			temp_player.vel.x = 8;
		}
		// ghost mode / invulnerable or other things on hit
		//	temp_player.hp--;
			
	} else {
		temp_player.is_hit = false;
		
	}


return temp_player;
}


bool hh_distance_circles (vec2 object_1, vec2 object_2, int radius_1, int radius_2){
	int a_squared = (object_1.x - object_2.x) * (object_1.x - object_2.x);
	int b_squared = (object_1.y - object_2.y) * (object_1.y - object_2.y);
	int c_squared = a_squared + b_squared; 
	int radius = ( radius_1 + radius_2) * ( radius_1 + radius_2 );

		if( c_squared <= radius ){
			return true;
		} else {
			return false;
		}
}

void hh_jump_entity(hh_entity* object_1){
	if (object_1->is_grounded == true) {//JUMP
	 	object_1->vel.y = -10;
		object_1->is_grounded = false;
	}
}
void hh_gravity_entity(hh_entity* object_1){
	if (object_1->is_grounded == false && object_1->vel.y < 6) {
		object_1->vel.y += 1; //gravity
	}
}
void hh_hit_entity(hh_entity* object_1, int8_t* hit_timer, int8_t* direction){
	if(object_1->is_hit == true){
		hit_timer = 9;
		object_1->is_hit = false;	
	} 
	if(hit_timer > -10){
			hit_timer--;
	}
	
	if(hit_timer <= 0){
		if(direction != 0){
			if(object_1->vel.x > -1 * object_1->speed && object_1->vel.x < object_1->speed) {
				object_1->vel.x = object_1->vel.x + direction;
			} else {
				if (object_1->vel.x > 0) {
					object_1->vel.x--;
				} else if(object_1->vel.x < 0) {
					object_1->vel.x++; 
				}
			}
		} else {
			if (object_1->vel.x > 0) {
				object_1->vel.x--;
			} else if(object_1->vel.x < 0) {
			object_1->vel.x++; 
			}
		}
		
	} else {
		if (object_1->vel.x > 0) {
			object_1->vel.x--;
		} else if(object_1->vel.x < 0) {
			object_1->vel.x++; 
		}
			object_1->vel.y++;
	}
	
}

void hh_check_all_collisions(hh_entity* player, hh_entity* enemies, int total_enemies, hh_entity* bullets, int total_bullets, vec_cor cam_pos){
	for(int enemy = 0; enemy < total_enemies; enemy++){
		*player = hh_enemy_collision(*player, enemies[enemy]);
		enemies[enemy].is_hit=false;
	}

	for(int i = 0; i < total_bullets; i++){
		if(!bullets[i].is_grounded){ 
			for (int enemy = 0; enemy < total_enemies; enemy++){
				
				if(hh_distance_circles(bullets[i].pos,enemies[enemy].pos,bullets[i].radius,enemies[enemy].radius)){
					enemies[enemy].is_hit=true;
					hh_bullet_death(&bullets[i]);
				}
			}	
		}
	}
	
}