#include "Layer.h" #include "Object.h" #include "Layer.h" #include "Bugsy.h" #include "Bunny.h" using namespace wgd; const float Object::GRAVITY = 9.81f; Object::Object(Sprite* spr, Layer* layer, const wgd::vector3d& size) : _layer(layer), _size(size), moveable(true) { sprite(spr); layer->addObject(this); } vector3d Object::pos() { return _pos; } void Object::update(float timeStep) { // Update the height based on current velocity. vector3d dir; dir.y = 1.0f; pos(pos() + dir.normalise() * 0.5f * timeStep * _velocity * GRAVITY); _velocity -= GRAVITY * timeStep; // If we fell below the current height, put us back. if (_pos.y <= _layer->getHeight((int) _pos.x, (int) _pos.z)) { _velocity = 0; pos(vector3d(_pos.x, (float)_layer->getHeight((int) _pos.x, (int) _pos.z), _pos.z)); } } bool Object::checkCollision(Object *o1, Object *o2) { if (!o1->getMoveable() && !o2->getMoveable()) return false; wgd::vector3d pos1, pos2, size1, size2; pos1 = o1->pos(); pos2 = o2->pos(); size1 = o1->size(); size2 = o2->size(); /*printf("(%.2f+%.2f,%.2f+%.2f), (%.2f+%.2f,%.2f+%.2f)\n", pos1.x, size1.x, pos1.z, size1.z, pos2.x, size2.x, pos2.z, size2.z);*/ //return (pos2 - pos1).length() <= (size1 + size2).length() * 0.5f; vector3d dPos = pos2 - pos1, dMin = (size1 + size2) * 0.5f; return abs(dPos.x) <= dMin.x && abs(dPos.y) <= dMin.y && abs(dPos.z) <= dMin.z; return (pos1.x + size1.x > pos2.x && pos2.x + size2.x > pos1.x && pos1.z + size1.z > pos2.z && pos2.z + size2.z > pos1.z); } void Object::pos(const vector3d& v) { _pos = v; _pos.x -= 1.0f; _pos.z -= 1.0f; vector2d i(32.0f, 16.0f), j(0.0f, -16.0f), k(-32.0f, 16.0f); position((_pos.x * i) + (_pos.y * j) + (_pos.z * k) + vector2d(0, _layer->getTop())); depth((float)(_pos.x + _pos.z + (_pos.y * Layer::LEDGE_HEIGHT) + 2.0f) * -0.1f); _pos.x += 1.0f; _pos.z += 1.0f; } void Object::draw(SceneGraph& graph, Camera2D* camera) { ISprite2D::draw(graph, camera); if (Bugsy::debugRender() && _layer == _layer->getLevel()->getCurLayer()) { bool isBunny = dynamic_cast(this) != NULL; DebugVertex v; vector3d _pos = pos(); const float scale = Bugsy::DEBUG_RENDER_SCALE; vector2d offset=vector2d(5 * scale, 5 * scale); if (isBunny) { int tileX = (int)_pos.x, tileZ = (int)_pos.z; //x and y co-ords of the tile we're on v.colour = colour(0.0f, 1.0f, 0.0f); vector2d corners[4] = { vector2d(tileX * scale, tileZ * scale), vector2d((tileX + 1) * scale, tileZ * scale), vector2d((tileX + 1) * scale, (tileZ + 1) * scale), vector2d(tileX * scale, (tileZ + 1) * scale) }; v.position = corners[0] + vector2d(1, 1) - offset; Bugsy::lines->add(v); v.position = corners[1] + vector2d(-1, 1) - offset; Bugsy::lines->add(v); Bugsy::lines->add(v); v.position = corners[2] + vector2d(-1, -1) - offset; Bugsy::lines->add(v); Bugsy::lines->add(v); v.position = corners[3] + vector2d(1, -1) - offset; Bugsy::lines->add(v); Bugsy::lines->add(v); v.position = corners[0] + vector2d(1, 1) - offset; Bugsy::lines->add(v); } if (isBunny) v.colour = colour(1.0f, 0.0f, 0.0f); else v.colour = colour(1.0f, 1.0f, 0.0f); vector3d halfSize = _size * 0.5f; v.position = vector2d((_pos.x - halfSize.x) * scale, (_pos.z - halfSize.z) * scale) - offset; Bugsy::lines->add(v); v.position = vector2d((_pos.x - halfSize.x) * scale, (_pos.z + halfSize.z) * scale) - offset; Bugsy::lines->add(v); Bugsy::lines->add(v); v.position = vector2d((_pos.x + halfSize.x) * scale, (_pos.z + halfSize.z) * scale) - offset; Bugsy::lines->add(v); Bugsy::lines->add(v); v.position = vector2d((_pos.x + halfSize.x) * scale, (_pos.z - halfSize.z) * scale) - offset; Bugsy::lines->add(v); Bugsy::lines->add(v); v.position = vector2d((_pos.x - halfSize.x) * scale, (_pos.z - halfSize.z) * scale) - offset; Bugsy::lines->add(v); } }