-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.c
130 lines (109 loc) · 3.32 KB
/
Entity.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
#include "Entity.h"
#include <assert.h>
static inline void SetEntityHealth(Entity_s* entity, short int health);
Entity_s *CreateEntity(enum EntityType_E type)
{
Entity_s* entity = (Entity_s*) malloc(sizeof(Entity_s));
entity->m_type = type;
entity->m_last_move = 0;
switch (type)
{
case PLAYER:
SetEntityHealth(entity, 3);
break;
case MOB:
SetEntityHealth(entity, 5);
break;
default:
break;
}
return entity;
}
static inline void SetEntityHealth(Entity_s* entity, short int health)
{
entity->m_health = health;
}
inline void MoveEntityTo(Entity_s *entity, int x, int y)
{
entity->m_position.m_x = x;
entity->m_position.m_y = y;
}
inline void setDirection(Entity_s *entity, Direction_e direction)
{
entity->m_direction = direction;
}
void moveEntityInDirection(Entity_s *entity)
{
switch (entity->m_direction)
{
case NORTH:
entity->m_position.m_y--;
entity->m_chunk_position = getEntityChunkCoordinate(entity);
break;
case SOUTH:
entity->m_position.m_y++;
entity->m_chunk_position = getEntityChunkCoordinate(entity);
break;
case WEST:
entity->m_position.m_x--;
entity->m_chunk_position = getEntityChunkCoordinate(entity);
break;
case EAST:
entity->m_position.m_x++;
entity->m_chunk_position = getEntityChunkCoordinate(entity);
break;
default:
break;
}
}
// Return pointer to array containing block
Block_s **getFrontBlock(Entity_s *entity, Tilemap_s *tilemap)
{
assert(entity && tilemap);
int entity_x = entity->m_position.m_x,
entity_y = entity->m_position.m_y;
Block_s **blocks = tilemap->m_blocks[1];
// Get entity coordinate in tilemap space
Coordinate_s entity_tilemap_coord = getEntityTilemapCoordinate(entity);
Block_s **block_in_front;
switch (entity->m_direction)
{
case NORTH:
block_in_front = tilemap->m_blocks[(entity_tilemap_coord.m_y - 1) * CHUNK_SIZE * MAX_CHUNK_DISTANCE + entity_tilemap_coord.m_x];
break;
case SOUTH:
block_in_front = tilemap->m_blocks[(entity_tilemap_coord.m_y + 1) * CHUNK_SIZE * MAX_CHUNK_DISTANCE + entity_tilemap_coord.m_x];
break;
case WEST:
block_in_front = tilemap->m_blocks[entity_tilemap_coord.m_y * CHUNK_SIZE * MAX_CHUNK_DISTANCE + entity_tilemap_coord.m_x - 1];
break;
case EAST:
block_in_front = tilemap->m_blocks[entity_tilemap_coord.m_y * CHUNK_SIZE * MAX_CHUNK_DISTANCE + entity_tilemap_coord.m_x + 1];
break;
default:
break;
}
return block_in_front;
}
Entitieslist_s *createEntitieslist(Entity_s *entity)
{
Entitieslist_s *entities_list = (Entitieslist_s *)calloc(1, sizeof(Entitieslist_s));
entities_list->m_entity = entity;
return entities_list;
}
void addEntityToList(Entitieslist_s **list, Entity_s *entity)
{
if (!*list)
{
*list = createEntitieslist(entity);
return;
}
Entitieslist_s *new = createEntitieslist(entity);
new->m_next = *list;
*list = new;
}
Coordinate_s getEntityChunkCoordinate(Entity_s *entity)
{
Coordinate_s entity_coord = entity->m_position;
return (Coordinate_s){entity_coord.m_x / CHUNK_SIZE, entity_coord.m_y / CHUNK_SIZE};
}