From 9efe2aff9180fe14780ee7743a333570b233b586 Mon Sep 17 00:00:00 2001 From: Ryan Flegel Date: Sun, 16 May 2004 05:48:28 +0000 Subject: [PATCH] - trampoline stuff SVN-Revision: 1208 --- src/collision.h | 3 ++- src/gameobjs.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++--------- src/gameobjs.h | 3 +++ src/player.cpp | 21 ++++++++++++++++++++ src/world.cpp | 19 +++++++++++++++++++ 5 files changed, 94 insertions(+), 10 deletions(-) diff --git a/src/collision.h b/src/collision.h index f813f2a12..b9e841002 100644 --- a/src/collision.h +++ b/src/collision.h @@ -31,7 +31,8 @@ enum { CO_BULLET, CO_BADGUY, - CO_PLAYER + CO_PLAYER, + CO_TRAMPOLINE }; enum CollisionType { diff --git a/src/gameobjs.cpp b/src/gameobjs.cpp index a48412fdc..3f23894f8 100644 --- a/src/gameobjs.cpp +++ b/src/gameobjs.cpp @@ -223,7 +223,7 @@ void load_object_gfx() { char sprite_name[16]; sprintf(sprite_name, "trampoline-%i", i+1); - img_trampoline[0] = sprite_manager->load(sprite_name); + img_trampoline[i] = sprite_manager->load(sprite_name); } } @@ -238,6 +238,15 @@ Trampoline::init(float x, float y) } void +Trampoline::draw() +{ + img_trampoline[0]->draw((int)base.x, (int)base.y); + + if (debug_mode) + fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75, 75, 0, 150); +} + +void Trampoline::action(double frame_ratio) { physic.apply(frame_ratio, base.x, base.y); @@ -253,19 +262,50 @@ Trampoline::action(double frame_ratio) else physic.enable_gravity(true); - // TODO: - // If HELD - // - move with tux - // If jumped on - // - compress springs (reduce height) } +// TODO: +// If HELD +// - move with tux +// If jumped on +// - compress springs (reduce height) + void -Trampoline::draw() +Trampoline::collision(void *p_c_object, int c_object, CollisionType type) { - img_trampoline[0]->draw((int)base.x, (int)base.y); -} + Player* pplayer_c = NULL; + switch (c_object) + { + case CO_PLAYER: + pplayer_c = (Player*) p_c_object; + + if (type == COLLISION_NORMAL) + { + // TODO: Pick up if HELD + } + + else if (type == COLLISION_SQUISH) + { + // TODO: compress springs + // TODO: launch tux, if necessary + + base.y = pplayer_c->base.y + pplayer_c->base.height; + base.height = (32 - (int)pplayer_c->base.y % 32); + if (base.height < 16) + { + base.height = 32; + pplayer_c->physic.set_velocity_y(8); + } + } + + break; + + default: + break; + + } +} /* EOF */ diff --git a/src/gameobjs.h b/src/gameobjs.h index 2276faa7d..97a25b711 100644 --- a/src/gameobjs.h +++ b/src/gameobjs.h @@ -27,6 +27,7 @@ #include "timer.h" #include "scene.h" #include "physic.h" +#include "collision.h" enum ObjectType { OBJ_NONE, OBJ_BADGUY, OBJ_TRAMPOLINE }; @@ -127,6 +128,8 @@ class Trampoline : public GameObject init(data.x, data.y); }; + void collision(void *p_c_object, int c_object, CollisionType type); + Physic physic; private: diff --git a/src/player.cpp b/src/player.cpp index d28584949..c04bd03bb 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -18,6 +18,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include +#include #include "gameloop.h" #include "globals.h" #include "player.h" @@ -643,6 +644,7 @@ void Player::collision(void* p_c_object, int c_object) { BadGuy* pbad_c = NULL; + Trampoline* ptramp_c = NULL; switch (c_object) { @@ -693,6 +695,25 @@ Player::collision(void* p_c_object, int c_object) player_status.score_multiplier++; } break; + + case CO_TRAMPOLINE: + ptramp_c = (Trampoline*) p_c_object; + + if (physic.get_velocity_x() > 0) // RIGHT + { + physic.set_velocity_x(0); + base.x = ptramp_c->base.x - base.width; + } + else if (physic.get_velocity_x() < 0) // LEFT + { + physic.set_velocity_x(0); + base.x = ptramp_c->base.x + ptramp_c->base.width; + } + else + { + } + break; + default: break; } diff --git a/src/world.cpp b/src/world.cpp index 7ca3e96de..7db4ed5aa 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -499,6 +499,25 @@ World::collision_handler() upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL); } } + + // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY) + for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i) + { + if (rectcollision((*i)->base, tux.base)) + { + if (tux.previous_base.y < tux.base.y && + tux.previous_base.y + tux.previous_base.height + < (*i)->base.y + (*i)->base.height/2) + { + (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH); + } + else + { + tux.collision(*i, CO_TRAMPOLINE); + (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL); + } + } + } } void -- 2.11.0