-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathces.c
209 lines (157 loc) · 4.56 KB
/
ces.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
// ces.c -- Component Entity System implementation.
#include "7drltypes.h"
World world;
int getEntityAt( int Y, int X )
{
unsigned int entity;
for ( entity = 0 ; entity < MAX_ENTITIES ; entity++ )
{
if (world.location[ entity ].X == X && world.location[ entity ].Y == Y )
{
return entity;
}
}
return entity;
}
unsigned int createEntity( )
{
unsigned int entity;
for ( entity = 0 ; entity < MAX_ENTITIES ; entity++ )
{
if ( world.mask[ entity ] == COMPONENT_NONE )
{
world.id[ entity ] = entity;
return entity;
}
}
return MAX_ENTITIES;
}
void destroyEntity( unsigned int entity )
{
world.mask[ entity ] = COMPONENT_NONE;
return;
}
unsigned int createProjectile( void )
{
int entity;
int y, x;
int dY, dX;
entity = createEntity( );
if ( entity < MAX_ENTITIES )
{
y = GetPlayerLocationY( );
x = GetPlayerLocationX( );
dY = GetPlayerLocationdY( );
dX = GetPlayerLocationdX( );
world.mask[ entity ] = COMPONENT_LOCATION |
COMPONENT_TARGET |
COMPONENT_ATTACK |
COMPONENT_MOVEMENT;
// Place the projectile in the environment.
world.location[ entity ].Y = y+dY;
world.location[ entity ].X = x+dX;
world.target[ entity ].dY = dY;
world.target[ entity ].dX = dX;
world.target[ entity ].distance = 0;
world.attack [ entity ].Attack = GetShotPower( );
world.movement[ entity ].Speed = 4;
world.movement[ entity ].State = 0;
if ( 0 ) {
char line[80];
sprintf( line, "Projectile entity %d running %d, %d", entity, y+dY, x+dX );
add_message( line );
}
if ( map_get_item( world.location[ entity ].Y,
world.location[ entity ].X ) == SPACE_ICON )
{
map_place_item( world.location[ entity ].Y, world.location[ entity ].X,
PROJECTILE_ICON );
}
else
{
// player attack.
IncrementProjectiles( );
destroyEntity( entity );
Attack( world.attack[ entity ].Attack,
world.location[ entity ].Y, world.location[ entity ].X,
PROJECTILE_ICON );
}
}
return entity;
}
unsigned int createAnimal( )
{
int entity;
int y, x;
entity = createEntity( );
if ( entity < MAX_ENTITIES )
{
world.mask[ entity ] = COMPONENT_LOCATION |
COMPONENT_HEALTH |
COMPONENT_XPVALUE |
COMPONENT_ANIMAL |
COMPONENT_MOVEMENT;
// Place the animal in the environment.
do {
y = getRand( Y_MAP_MAX );
x = getRand( X_MAP_MAX );
} while ( !IsAreaClear( y, x ) );
world.location[ entity ].Y = y;
world.location[ entity ].X = x;
world.movement[ entity ].Speed = 13 + getRand( 5 );
world.movement[ entity ].State = 0;
world.health[ entity ].Health = 5;
world.XPValue[ entity].XP = 5;
map_place_item( y, x, ANIMAL_ICON );
}
return entity;
}
unsigned int createProtector( )
{
int entity;
int y, x;
entity = createEntity( );
if ( entity < MAX_ENTITIES )
{
world.mask[ entity ] = COMPONENT_LOCATION |
COMPONENT_HEALTH |
COMPONENT_ATTACK |
COMPONENT_XPVALUE |
COMPONENT_PROTECTOR |
COMPONENT_MOVEMENT;
// Place the animal in the environment.
do {
y = getRand( Y_MAP_MAX );
x = getRand( X_MAP_MAX );
} while ( !IsAreaClear( y, x ) );
world.location[ entity ].Y = y;
world.location[ entity ].X = x;
world.movement[ entity ].Speed = 9 + getRand( 5 );
world.movement[ entity ].State = 0;
world.attack[ entity ].Attack = 2;
world.health[ entity ].Health = 20;
world.XPValue[ entity].XP = 20;
map_place_item( y, x, PROTECTOR_ICON );
}
return entity;
}
void InitEntities( )
{
unsigned int entity;
// Initialize the entities to null
for ( entity = 0 ; entity < MAX_ENTITIES ; entity++ )
{
destroyEntity( entity );
}
// Create some number of animals.
for ( entity = 0 ; entity < NUMBER_OF_ANIMALS ; entity++ )
{
createAnimal( );
}
// Create some number of protectors.
for ( entity = 0 ; entity < NUMBER_OF_PROTECTORS ; entity++ )
{
createProtector( );
}
return;
}