143 lines
3.4 KiB
C++
143 lines
3.4 KiB
C++
#include <glm/gtx/transform.hpp>
|
|
|
|
#include "ObjDecoder.hh"
|
|
|
|
#include "GSShowObject.hh"
|
|
#include "render/Object.hh"
|
|
|
|
namespace game {
|
|
GSShowObject::GSShowObject(render::Renderer& renderer, ObjDecoder& obj,
|
|
PaletteDecoder& palt)
|
|
: GameState(renderer), obj_(obj), palt_(palt),
|
|
object_(nullptr), delta_(0),
|
|
rotatingUD_(Direction::None), rotatingRL_(Direction::None),
|
|
moveRL_(Direction::None), moveFB_(Direction::None),
|
|
rotUD_(0.0f), rotRL_(0.0f), posX_(0.0f), posZ_(0.0f),
|
|
lod_(0)
|
|
{
|
|
object_ = std::make_unique<render::Object>(renderer, obj, palt, lod_);
|
|
}
|
|
|
|
GSShowObject::~GSShowObject()
|
|
{
|
|
}
|
|
|
|
bool GSShowObject::handleEvent(SDL_Event& event)
|
|
{
|
|
switch (event.type) {
|
|
case SDL_KEYDOWN:
|
|
switch (event.key.keysym.sym) {
|
|
case SDLK_UP:
|
|
rotatingUD_ = Direction::Up;
|
|
break;
|
|
case SDLK_DOWN:
|
|
rotatingUD_ = Direction::Down;
|
|
break;
|
|
case SDLK_LEFT:
|
|
rotatingRL_ = Direction::Left;
|
|
break;
|
|
case SDLK_RIGHT:
|
|
rotatingRL_ = Direction::Right;
|
|
break;
|
|
case SDLK_w:
|
|
moveFB_ = Direction::Back;
|
|
break;
|
|
case SDLK_s:
|
|
moveFB_ = Direction::Front;
|
|
break;
|
|
case SDLK_a:
|
|
moveRL_ = Direction::Left;
|
|
break;
|
|
case SDLK_d:
|
|
moveRL_ = Direction::Right;
|
|
break;
|
|
}
|
|
break;
|
|
case SDL_KEYUP:
|
|
switch (event.key.keysym.sym) {
|
|
case SDLK_UP:
|
|
if (rotatingUD_ == Direction::Up)
|
|
rotatingUD_ = Direction::None;
|
|
break;
|
|
case SDLK_DOWN:
|
|
if (rotatingUD_ == Direction::Down)
|
|
rotatingUD_ = Direction::None;
|
|
break;
|
|
case SDLK_LEFT:
|
|
if (rotatingRL_ == Direction::Left)
|
|
rotatingRL_ = Direction::None;
|
|
break;
|
|
case SDLK_RIGHT:
|
|
if (rotatingRL_ == Direction::Right)
|
|
rotatingRL_ = Direction::None;
|
|
break;
|
|
case SDLK_w:
|
|
if (moveFB_ == Direction::Back)
|
|
moveFB_ = Direction::None;
|
|
break;
|
|
case SDLK_s:
|
|
if (moveFB_ == Direction::Front)
|
|
moveFB_ = Direction::None;
|
|
break;
|
|
case SDLK_a:
|
|
if (moveRL_ == Direction::Left)
|
|
moveRL_ = Direction::None;
|
|
break;
|
|
case SDLK_d:
|
|
if (moveRL_ == Direction::Right)
|
|
moveRL_ = Direction::None;
|
|
break;
|
|
case SDLK_l:
|
|
if (event.key.keysym.mod & KMOD_SHIFT) {
|
|
if (lod_+1 < obj_.getDetailLevels()) {
|
|
++lod_;
|
|
object_ = std::make_unique<render::Object>(renderer_, obj_, palt_, lod_);
|
|
}
|
|
} else {
|
|
if (lod_ > 0) {
|
|
--lod_;
|
|
object_ = std::make_unique<render::Object>(renderer_, obj_, palt_, lod_);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void GSShowObject::draw(unsigned delta_ms)
|
|
{
|
|
const float ROTATE_SCALE = 0.0005f, MOVE_SCALE = 50.0f;
|
|
|
|
delta_ += delta_ms;
|
|
|
|
if (rotatingUD_ == Direction::Up)
|
|
rotUD_ += delta_ms*ROTATE_SCALE;
|
|
else if (rotatingUD_ == Direction::Down)
|
|
rotUD_ -= delta_ms*ROTATE_SCALE;
|
|
|
|
if (rotatingRL_ == Direction::Left)
|
|
rotRL_ += delta_ms*ROTATE_SCALE;
|
|
else if (rotatingRL_ == Direction::Right)
|
|
rotRL_ -= delta_ms*ROTATE_SCALE;
|
|
|
|
if (moveFB_ == Direction::Back)
|
|
posZ_ += delta_ms*MOVE_SCALE;
|
|
else if (moveFB_ == Direction::Front)
|
|
posZ_ -= delta_ms*MOVE_SCALE;
|
|
|
|
if (moveRL_ == Direction::Left)
|
|
posX_ += delta_ms*MOVE_SCALE;
|
|
else if (moveRL_ == Direction::Right)
|
|
posX_ -= delta_ms*MOVE_SCALE;
|
|
|
|
object_->setTransform(glm::translate(glm::vec3(posX_, 0.0f, posZ_))*
|
|
glm::rotate(rotUD_, glm::vec3(1.0f, 0.0f, 0.0f))*
|
|
glm::rotate(rotRL_, glm::vec3(0.0f, 1.0f, 0.0f)));
|
|
object_->setAnimFrame((delta_/125)%8);
|
|
object_->draw();
|
|
}
|
|
}
|