1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific: |
---|
13 | main-programmer: Patrick Boenzli |
---|
14 | co-programmer: Christian Meyer |
---|
15 | */ |
---|
16 | |
---|
17 | #include <iostream> |
---|
18 | #include <stdlib.h> |
---|
19 | #include <cmath> |
---|
20 | |
---|
21 | #include "world.h" |
---|
22 | |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | |
---|
27 | /** |
---|
28 | \brief Create a new World |
---|
29 | |
---|
30 | This creates a new empty world! |
---|
31 | */ |
---|
32 | World::World () |
---|
33 | { |
---|
34 | entities = new List<WorldEntitites>(); |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | World::~World () |
---|
39 | { |
---|
40 | unload (); |
---|
41 | delete entities; |
---|
42 | } |
---|
43 | |
---|
44 | template<class T> T* World::spawn<T>(Location* loc = NULL, WorldEntity* owner = NULL) |
---|
45 | { |
---|
46 | Location zeroloc; |
---|
47 | T* entity = new T(); |
---|
48 | entities->add ((WorldEntity*)entity, LIST_ADD_NEXT); |
---|
49 | if( loc == NULL) |
---|
50 | { |
---|
51 | zeroloc.dist = 0; |
---|
52 | zeroloc.part = 0; |
---|
53 | zeroloc.pos = Vector(); |
---|
54 | zeroloc.rot = Rotation(); |
---|
55 | loc = &zeroloc; |
---|
56 | } |
---|
57 | entity->init (loc, owner); |
---|
58 | if (entity->bFree) |
---|
59 | { |
---|
60 | track[loc->part].map_coords( loc, entity->get_placement()) |
---|
61 | } |
---|
62 | entity->post_spawn (); |
---|
63 | return entity; |
---|
64 | } |
---|
65 | |
---|
66 | template<class T> T* World::spawn<T>(Placement* plc, WorldEntity* owner = NULL) |
---|
67 | { |
---|
68 | T* entity = new T(); |
---|
69 | entities->add ((WorldEntity*)entity, LIST_ADD_NEXT); |
---|
70 | entity->init (plc, owner); |
---|
71 | if (!entity->bFree) |
---|
72 | { |
---|
73 | printf("Can't spawn unfree entity with placement\n"); |
---|
74 | entities->remove( (WorldEntity*)entity, LIST_FIND_FW); |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | entity->post_spawn (); |
---|
78 | return entity; |
---|
79 | } |
---|
80 | |
---|
81 | World::collide () |
---|
82 | { |
---|
83 | List<WorldEntity> *a, *b; |
---|
84 | WorldEntity *aobj, *bobj; |
---|
85 | |
---|
86 | a = entities->get_next(); |
---|
87 | |
---|
88 | while( a != NULL) |
---|
89 | { |
---|
90 | aobj = a->get_object(); |
---|
91 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
92 | { |
---|
93 | b = a->get_next(); |
---|
94 | while( b != NULL) |
---|
95 | { |
---|
96 | bobj = b->get_object(); |
---|
97 | if( bobj->bCollide && bobj->collisioncluster != NULL) |
---|
98 | { |
---|
99 | Uint32 ahitflg, bhitflg; |
---|
100 | if check_collision ( aobj->place, aobj->collisioncluster, &ahitflg, bobj->place, bobj->collisioncluster, &bhitflg); |
---|
101 | { |
---|
102 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
103 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
104 | } |
---|
105 | } |
---|
106 | b = b->get_next(); |
---|
107 | } |
---|
108 | } |
---|
109 | a = a->get_next(); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void World::draw () |
---|
114 | { |
---|
115 | // draw geometry |
---|
116 | |
---|
117 | // draw entities |
---|
118 | List<WorldEntity> *l; |
---|
119 | WorldEntity* entity; |
---|
120 | |
---|
121 | l = entities->get_next(); |
---|
122 | while( l != NULL) |
---|
123 | { |
---|
124 | entity = l->get_object(); |
---|
125 | if( entity->bDraw) entity->draw(); |
---|
126 | l = l->get_next(); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | void World::update () |
---|
131 | { |
---|
132 | List<WorldEntity> *l; |
---|
133 | WorldEntity* entity; |
---|
134 | Location* loc; |
---|
135 | Placement* plc; |
---|
136 | Uint32 t; |
---|
137 | |
---|
138 | l = entities->get_next(); |
---|
139 | while( l != NULL) |
---|
140 | { |
---|
141 | entity = l->get_object(); |
---|
142 | |
---|
143 | if( entity != bFree) |
---|
144 | { |
---|
145 | loc = entity->get_location(); |
---|
146 | plc = entity->get_placement(); |
---|
147 | t = loc->part; |
---|
148 | |
---|
149 | if( t >= tracklen) |
---|
150 | { |
---|
151 | printf("An entity is out of the game area\n"); |
---|
152 | entity->left_world (); |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | while( track[t].map_coords( loc, plc)) |
---|
157 | { |
---|
158 | track[t]->post_leave (entity); |
---|
159 | if( loc->part >= tracklen) |
---|
160 | { |
---|
161 | printf("An entity has left the game area\n"); |
---|
162 | entity->left_world (); |
---|
163 | break; |
---|
164 | } |
---|
165 | track[loc->part]->post_enter (entity); |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | // TO DO: implement check whether this particular free entity is out of the game area |
---|
172 | // TO DO: call function to notify the entity that it left the game area |
---|
173 | } |
---|
174 | |
---|
175 | l = l->get_next(); |
---|
176 | } |
---|
177 | |
---|
178 | } |
---|
179 | |
---|
180 | void World::time_slice (Uint32 deltaT) |
---|
181 | { |
---|
182 | List<WorldEntity> *l; |
---|
183 | WorldEntity* entity; |
---|
184 | float seconds = deltaT; |
---|
185 | |
---|
186 | seconds /= 1000; |
---|
187 | |
---|
188 | l = entities->get_next(); |
---|
189 | while( l != NULL) |
---|
190 | { |
---|
191 | entity = l->get_object(); |
---|
192 | entity->tick (seconds); |
---|
193 | l = l->get_next(); |
---|
194 | } |
---|
195 | |
---|
196 | for( int i = 0; i < tracklen) track[i].tick (seconds); |
---|
197 | } |
---|
198 | |
---|
199 | void World::unload() |
---|
200 | { |
---|
201 | if( pathnodes) delete []pathnodes; |
---|
202 | if( track) delete []pathnodes; |
---|
203 | } |
---|
204 | |
---|
205 | void World::load_debug_level() |
---|
206 | { |
---|
207 | // create some path nodes |
---|
208 | pathnodes = new Vector[6]; |
---|
209 | pathnodes[0] = Vector(0, 0, 0); |
---|
210 | pathnodes[1] = Vector(-100, 40, 0); |
---|
211 | pathnodes[2] = Vector(-100, 140, 0); |
---|
212 | pathnodes[3] = Vector(0, 180, 0); |
---|
213 | pathnodes[4] = Vector(100, 140, 0); |
---|
214 | pathnodes[5] = Vector(100, 40, 0); |
---|
215 | |
---|
216 | // create the tracks |
---|
217 | tracklen = 6; |
---|
218 | track = new Track[6]; |
---|
219 | for( int i = 0; i < tracklen; i++) |
---|
220 | { |
---|
221 | track[i] = Track( i, (i+1)%tracklen, &pathnodes[i], &pathnodes[(i+1)%tracklen]); |
---|
222 | } |
---|
223 | |
---|
224 | // create a player |
---|
225 | |
---|
226 | // bind input |
---|
227 | // bind camera |
---|
228 | } |
---|