]>
git.r.bdr.sh - rbdr/pico-engine/blob - game_overwatch.cpp
4 Game_overwatch::Game_overwatch(){
5 //Initialize our population
11 void Game_overwatch::move_out(Actor
*npc
){
13 //Moving out means killing a citizen from the citizen linked list.
18 for(curr
=head
;curr
!=NULL
;prev
=curr
,curr
=curr
->next
){
19 if (curr
->val
== npc
) {
22 std::cout
<< "I have deleted an NPC from the top!\n";
24 prev
->next
= curr
->next
;
25 std::cout
<< "I have deleted an NPC from the middle!\n";
35 void Game_overwatch::move_in(Actor
* npc
){
36 //Moving in means adding a citizen to the linkd list.
46 int Game_overwatch::get_population(){
50 void Game_overwatch::draw(SDL_Surface
*viewport
, Map
*mainmap
){
52 //Generic draw function, iterates over the list and draws
55 //NOTE: Further along it may be wise to allow Lua scripts to
56 // override the draw function and just call curr->draw();
61 Gfx::drawsurface(curr
->val
->get_clip_x(),curr
->val
->get_clip_y(),
62 curr
->val
->get_w(),curr
->val
->get_h(),
63 curr
->val
->get_sprite(),
64 curr
->val
->get_x()-mainmap
->get_sx(),curr
->val
->get_y()-mainmap
->get_sy(),
70 void Game_overwatch::act(SDL_Surface
*viewport
, Map
*mainmap
, bool keys
[323]){
72 //Iterates over the list and runs the "act" function from the lua script.
78 handleinput(curr
->val
, keys
);
81 curr
->val
->SelectScriptFunction ("act");
88 void Game_overwatch::handleinput(Actor
*actor
, bool keys
[323]){
90 //Right now these actions just map keys to variables the scripts can use
91 //In the future it will be wise to abstract the keys to what they do
92 //Also, take into account gamepads.
93 //Preferably, let lua handle this somehow.
97 leftkey
= SDL_GetTicks();
103 if(keys
[SDLK_RIGHT
]){
106 rightkey
= SDL_GetTicks();
114 zkey
= SDL_GetTicks();
122 xkey
= SDL_GetTicks();
130 ckey
= SDL_GetTicks();
136 actor
->set_leftkey(leftkey
);
137 actor
->set_rightkey(rightkey
);
138 actor
->set_zkey(zkey
);
139 actor
->set_xkey(xkey
);
140 actor
->set_ckey(ckey
);
143 void Game_overwatch::animate(){
144 //Iterate and run the animate method.
148 while (curr
!= NULL
){
149 curr
->val
->animate();
154 void Game_overwatch::check_collisions(Map
*mainmap
){
155 citizen
*curr
, *others
;
157 bool collided
= false;
162 //now check everyone. Never with itself.
163 //It does line collisions, not corner collisions.
165 while (curr
!= NULL
){
167 while (others
!= NULL
) {
170 if (curr
->val
!= others
->val
) {
171 if(Game_overwatch::line_colliding(ac1
->get_x(), ac1
->get_w(),
172 ac1
->get_y(), ac1
->get_h(),
173 ac2
->get_x(), ac2
->get_w(),
174 ac2
->get_y(), ac2
->get_h(),
177 ac2
->set_collision(true, DOWN
);
178 ac2
->set_speed(ac1
->get_ownspeed());
179 ac2
->set_vertical_speed(ac2
->get_vertical_speed()+ac1
->get_vertical_speed());
181 ac1
->set_collision(true, UP
);
184 if(Game_overwatch::line_colliding(ac1
->get_x(), ac1
->get_w(),
185 ac1
->get_y(), ac1
->get_h(),
186 ac2
->get_x(), ac2
->get_w(),
187 ac2
->get_y(), ac2
->get_h(),
190 ac2
->set_collision(true, UP
);
191 ac1
->set_collision(true, DOWN
);
194 if(Game_overwatch::line_colliding(ac1
->get_x(), ac1
->get_w(),
195 ac1
->get_y(), ac1
->get_h(),
196 ac2
->get_x(), ac2
->get_w(),
197 ac2
->get_y(), ac2
->get_h(),
200 ac2
->set_collision(true, RIGHT
);
201 ac1
->set_collision(true, LEFT
);
204 if(Game_overwatch::line_colliding(ac1
->get_x(), ac1
->get_w(),
205 ac1
->get_y(), ac1
->get_h(),
206 ac2
->get_x(), ac2
->get_w(),
207 ac2
->get_y(), ac2
->get_h(),
210 ac2
->set_collision(true, LEFT
);
211 ac1
->set_collision(true, RIGHT
);
216 others
= others
->next
;
218 collision_callback(ac1
, ac2
);
230 void Game_overwatch::collision_callback(Actor
*ac1
, Actor
*ac2
){
232 std::cout
<< "The collision has called ";
234 if (ac1
->get_enemy() == WEAPON
&& !ac2
->get_dying()) {
235 if (ac2
->get_enemy() == EVIL
) {
236 ac1
->set_dying(true);
237 ac2
->set_dying(true);
240 std::cout
<< "And the shot has destroyed the enemy.";
244 if (ac1
->get_enemy() == SUPEREVIL
) {
245 ac2
->set_dying(true);
246 std::cout
<< "And the shot has destroyed the enemy.";
249 if (ac1
->get_controllable()) {
250 if (ac2
->get_enemy() == EVIL
) {
251 ac1
->set_dying(true);
254 std::cout
<< "And the enemy has destroyed the main actor.";
258 if (ac1
->get_enemy() == EVIL
) {
259 if (ac2
->get_controllable()) {
260 ac2
->set_dying(true);
263 std::cout
<< "And the enemy has destroyed the main actor.";
272 bool Game_overwatch::line_colliding(int x1
, int w1
, int y1
, int h1
, int x2
, int w2
, int y2
, int h2
, int direction
){
275 if (y1
<= y2
+h2
&& y1
>= y2
+(h2
/2) &&
276 ((x1
> x2
&& x1
< x2
+w2
) ||
277 (x1
+w1
> x2
&& x1
+w2
< x2
+w2
))) {
278 std::cout
<< "Foo' is collidin' top\n";
283 if (y1
+h1
>= y2
&& y1
+h1
<= y2
+(h2
/2) &&
284 ((x1
> x2
&& x1
< x2
+w2
) ||
285 (x1
+w1
> x2
&& x1
+w2
< x2
+w2
))) {
286 std::cout
<< "Foo' is collidin' bottom\n";
291 if (x1
<= x2
+w2
&& x1
>= x2
+(w2
/2) &&
292 ((y1
> y2
&& y1
< y2
+h2
) ||
293 (y1
+h1
> y2
&& y1
+h1
< y2
+h2
))) {
294 std::cout
<< "Foo' is collidin' left\n";
299 if (x1
+w1
>= x2
&& x1
+w1
<= x2
+(w2
/2) &&
300 ((y1
> y2
&& y1
< y2
+h2
) ||
301 (y1
+h1
> y2
&& y1
+h1
< y2
+h2
))) {
302 std::cout
<< "Foo' is collidin' right\n";
311 void Game_overwatch::handlephysics(Map
*mainmap
, int ticks1
, int ticks2
){
314 while (curr
!= NULL
){
316 //some are not affected by physics.
317 if(curr
->val
->get_physics()){
319 if(curr
->val
->can_move(DOWN
, curr
->val
->get_x(), curr
->val
->get_y())){
321 //change sprite to its jump.
322 switch(curr
->val
->get_oldclip()){
324 curr
->val
->set_clip_y(curr
->val
->get_h()*0);
327 curr
->val
->set_clip_y(curr
->val
->get_h()*3);
331 //start the fall process
332 if(curr
->val
->get_falltime() == 0){
333 curr
->val
->set_falltime(.5);
335 curr
->val
->set_falltime(curr
->val
->get_falltime()+(ticks2
-ticks1
)/1000);
341 curr
->val
->set_clip_y(curr
->val
->get_h()*curr
->val
->get_oldclip());
342 curr
->val
->set_falltime(0);
343 if(curr
->val
->get_vertical_speed() > 0){
344 curr
->val
->set_vertical_speed(0);
348 //check for top collision
349 if( !curr
->val
->can_move(UP
, curr
->val
->get_x(), curr
->val
->get_y()) && curr
->val
->get_vertical_speed() < 0){
350 curr
->val
->set_vertical_speed(0);
353 //Avoid falling through the ground
354 while(!curr
->val
->can_move(DOWN
, curr
->val
->get_x(), curr
->val
->get_y()+curr
->val
->get_vertical_speed()) && curr
->val
->get_vertical_speed() > 0){
355 curr
->val
->set_vertical_speed( curr
->val
->get_vertical_speed() - 1 );
359 curr
->val
->set_vertical_speed( curr
->val
->get_vertical_speed() + (GRAVITY
*curr
->val
->get_falltime()));
362 curr
->val
->set_y( curr
->val
->get_y() + curr
->val
->get_vertical_speed());
370 void Game_overwatch::reset_collisions(){
373 while (curr
!= NULL
){
374 curr
->val
->set_collision(false, LEFT
);
375 curr
->val
->set_collision(false, RIGHT
);
376 curr
->val
->set_collision(false, DOWN
);
377 curr
->val
->set_collision(false, UP
);
382 void Game_overwatch::dieloop(){
385 // Every frame, we find out who needs to be killed
390 while (curr
!= NULL
){
391 bool killthis
= false;
392 if ( curr
->val
->get_dying() ){
397 Actor
*victim
= curr
->val
;
406 void Game_overwatch::kill(Actor
*actor
){
408 //This should probably delegate to the lua die function
409 //So it can animate/change states.
411 if ( actor
->get_controllable() ){
414 actor
->set_dying(false);