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 "world.h" |
---|
18 | #include "world_entity.h" |
---|
19 | #include "collision.h" |
---|
20 | #include "track.h" |
---|
21 | #include "player.h" |
---|
22 | #include "command_node.h" |
---|
23 | #include "camera.h" |
---|
24 | |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | |
---|
28 | /** |
---|
29 | \brief Create a new World |
---|
30 | |
---|
31 | This creates a new empty world! |
---|
32 | */ |
---|
33 | World::World () |
---|
34 | { |
---|
35 | entities = new List<WorldEntity>(); |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | World::~World () |
---|
40 | { |
---|
41 | unload (); |
---|
42 | delete entities; |
---|
43 | } |
---|
44 | |
---|
45 | void World::collide () |
---|
46 | { |
---|
47 | List<WorldEntity> *a, *b; |
---|
48 | WorldEntity *aobj, *bobj; |
---|
49 | |
---|
50 | a = entities->get_next(); |
---|
51 | |
---|
52 | while( a != NULL) |
---|
53 | { |
---|
54 | aobj = a->get_object(); |
---|
55 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
56 | { |
---|
57 | b = a->get_next(); |
---|
58 | while( b != NULL) |
---|
59 | { |
---|
60 | bobj = b->get_object(); |
---|
61 | if( bobj->bCollide && bobj->collisioncluster != NULL) |
---|
62 | { |
---|
63 | unsigned long ahitflg, bhitflg; |
---|
64 | if( check_collision ( &aobj->place, aobj->collisioncluster, &ahitflg, &bobj->place, bobj->collisioncluster, &bhitflg)); |
---|
65 | { |
---|
66 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
67 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
68 | } |
---|
69 | } |
---|
70 | b = b->get_next(); |
---|
71 | } |
---|
72 | } |
---|
73 | a = a->get_next(); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void World::draw () |
---|
78 | { |
---|
79 | // draw geometry |
---|
80 | |
---|
81 | // draw entities |
---|
82 | List<WorldEntity> *l; |
---|
83 | WorldEntity* entity; |
---|
84 | |
---|
85 | l = entities->get_next(); |
---|
86 | while( l != NULL) |
---|
87 | { |
---|
88 | entity = l->get_object(); |
---|
89 | if( entity->bDraw) entity->draw(); |
---|
90 | l = l->get_next(); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | void World::update () |
---|
95 | { |
---|
96 | List<WorldEntity> *l; |
---|
97 | WorldEntity* entity; |
---|
98 | Location* loc; |
---|
99 | Placement* plc; |
---|
100 | Uint32 t; |
---|
101 | |
---|
102 | l = entities->get_next(); |
---|
103 | while( l != NULL) |
---|
104 | { |
---|
105 | entity = l->get_object(); |
---|
106 | |
---|
107 | if( !entity->isFree()) |
---|
108 | { |
---|
109 | loc = entity->get_location(); |
---|
110 | plc = entity->get_placement(); |
---|
111 | t = loc->part; |
---|
112 | |
---|
113 | if( t >= tracklen) |
---|
114 | { |
---|
115 | printf("An entity is out of the game area\n"); |
---|
116 | entity->left_world (); |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | while( track[t].map_coords( loc, plc)) |
---|
121 | { |
---|
122 | track[t].post_leave (entity); |
---|
123 | if( loc->part >= tracklen) |
---|
124 | { |
---|
125 | printf("An entity has left the game area\n"); |
---|
126 | entity->left_world (); |
---|
127 | break; |
---|
128 | } |
---|
129 | track[loc->part].post_enter (entity); |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | // TO DO: implement check whether this particular free entity is out of the game area |
---|
136 | // TO DO: call function to notify the entity that it left the game area |
---|
137 | } |
---|
138 | |
---|
139 | l = l->get_next(); |
---|
140 | } |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | void World::time_slice (Uint32 deltaT) |
---|
145 | { |
---|
146 | List<WorldEntity> *l; |
---|
147 | WorldEntity* entity; |
---|
148 | float seconds = deltaT; |
---|
149 | |
---|
150 | seconds /= 1000; |
---|
151 | |
---|
152 | l = entities->get_next(); |
---|
153 | while( l != NULL) |
---|
154 | { |
---|
155 | entity = l->get_object(); |
---|
156 | entity->tick (seconds); |
---|
157 | l = l->get_next(); |
---|
158 | } |
---|
159 | |
---|
160 | for( int i = 0; i < tracklen; i++) track[i].tick (seconds); |
---|
161 | } |
---|
162 | |
---|
163 | void World::unload() |
---|
164 | { |
---|
165 | if( pathnodes) delete []pathnodes; |
---|
166 | if( track) delete []pathnodes; |
---|
167 | } |
---|
168 | |
---|
169 | void World::load_debug_level() |
---|
170 | { |
---|
171 | // create some path nodes |
---|
172 | pathnodes = new Vector[6]; |
---|
173 | pathnodes[0] = Vector(0, 0, 0); |
---|
174 | pathnodes[1] = Vector(-100, 40, 0); |
---|
175 | pathnodes[2] = Vector(-100, 140, 0); |
---|
176 | pathnodes[3] = Vector(0, 180, 0); |
---|
177 | pathnodes[4] = Vector(100, 140, 0); |
---|
178 | pathnodes[5] = Vector(100, 40, 0); |
---|
179 | |
---|
180 | // create the tracks |
---|
181 | tracklen = 6; |
---|
182 | track = new Track[6]; |
---|
183 | for( int i = 0; i < tracklen; i++) |
---|
184 | { |
---|
185 | track[i] = Track( i, (i+1)%tracklen, &pathnodes[i], &pathnodes[(i+1)%tracklen]); |
---|
186 | } |
---|
187 | |
---|
188 | // create a player |
---|
189 | WorldEntity* myPlayer = (WorldEntity*) spawn<Player>(); |
---|
190 | |
---|
191 | // bind input |
---|
192 | Orxonox *orx = Orxonox::getInstance(); |
---|
193 | orx->get_localinput()->bind (myPlayer); |
---|
194 | |
---|
195 | // bind camera |
---|
196 | orx->get_camera()->bind (myPlayer); |
---|
197 | } |
---|