-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.c
More file actions
99 lines (82 loc) · 3.19 KB
/
entity.c
File metadata and controls
99 lines (82 loc) · 3.19 KB
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
/*******************************************************************************************
*
* Smurf Invaders. Technical Demo as an explorative project of Raylib.
* Random game based on Space Invaders and alike.
*
* Copyright (c) 2020 Oscar Lostes Cazorla, under MIT license.
*
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Raylib by Ramon Santamaria (@raysan5), Copyright (c) 2013-2020
*
********************************************************************************************/
#include "entity.h"
void initEntity(Entity* entity, Model* model, BoundingBox* bounds, Color tint,
Color border, Vector3 position, float speed, float scale, int score, bool active){
// Inits the entity struct, element by element
entity->model = model;
entity->bounds = *bounds;
entity->refBounds = bounds;
entity->tint = tint;
entity->border = border;
entity->speed = speed;
entity->scale = scale;
entity->active = active;
entity->score = score;
moveEntityVect(entity, position);
}
void initEntityPool(Entity* pool, int size, Model* model, BoundingBox* bounds, Color tint,
Color border, Vector3 position, float speed, float scale, int score){
// Iterates through the given entity array
for (int i = 0; i < size; i++)
{
// Inits each entity
initEntity(&pool[i], model, bounds, tint, border,
position, speed, scale, score, false);
}
}
void recalculateBounds(Entity* entity, BoundingBox referenceBox){
// referenceBox is the pattern BoundingBox of the entity, located at (0,0,0).
// This means that the actual boundingBox of given entity is referenceBox + entity->position
entity->bounds.min = Vector3Add(referenceBox.min, entity->position);
entity->bounds.max = Vector3Add(referenceBox.max, entity->position);
// DEBUG
//TraceLog(LOG_DEBUG, "Bounds min: (%.2f, %.2f, %.2f)", entity->bounds.min.x, entity->bounds.min.y, entity->bounds.min.z);
//TraceLog(LOG_DEBUG, "Bounds max: (%.2f, %.2f, %.2f)", entity->bounds.max.x, entity->bounds.max.y, entity->bounds.max.z));
}
void moveEntityVect(Entity* entity, Vector3 newPos){
entity->position = newPos; // Set new position
recalculateBounds(entity, *entity->refBounds); // Recalculate collision box
}
void moveEntity(Entity* entity, float x, float y, float z){
entity->position = (Vector3){x, y, z}; // Set new position
recalculateBounds(entity, *entity->refBounds); // Recalculate collision box
}
void drawEntity(Entity* entity){
DrawModel(*entity->model, entity->position, entity->scale, entity->tint);
DrawModelWires(*entity->model, entity->position, entity->scale, entity->border);
DrawModel(*entity->model, Vector3Add(entity->position, defaultShadow.offset), entity->scale, defaultShadow.color);
//DrawBoundingBox(entity->bounds, YELLOW); //DEBUG
}
void drawEntityPool(Entity* pool, int size){
for (int i = 0; i < size; i++)
{
if (pool[i].active){
// Draw entity + border + shadow
drawEntity(&pool[i]);
}
}
}
int getEntityFromPool(Entity* pool, int size){
int i = 0;
while (i < size)
{
// Entity not active = available entity
if (!pool[i].active)
{
pool[i].active = true; // Mark entity as used (render & interactive)
return i;
}
i++; // Thx my pana Dubledoh!
}
return -1;
}