Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
Implements roomRotateTo
Browse files Browse the repository at this point in the history
This references #38
  • Loading branch information
scemino committed Jun 19, 2019
1 parent 8505097 commit 5287c33
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
4 changes: 3 additions & 1 deletion include/Room.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include <vector>
#include "squirrel.h"
#include "nlohmann/json.hpp"
#include "ScriptObject.h"

namespace ng
Expand Down Expand Up @@ -58,6 +57,9 @@ class Room : public ScriptObject
void removeEntity(Entity *pEntity);
std::vector<RoomScaling>& getScalings();

float getRotation() const;
void setRotation(float angle);

Light* createLight(sf::Color color, sf::Vector2i pos);

private:
Expand Down
36 changes: 25 additions & 11 deletions src/Room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct Room::Impl
SpriteSheet _spriteSheet;
Room *_pRoom{nullptr};
std::vector<std::unique_ptr<Light>> _lights;
float _rotation{0};

Impl(TextureManager &textureManager, EngineSettings &settings)
: _textureManager(textureManager),
Expand Down Expand Up @@ -277,7 +278,7 @@ struct Room::Impl
}
for (auto jSubScaling : jScaling["scaling"].array_value)
{
if(jSubScaling.isString())
if (jSubScaling.isString())
{
auto value = jSubScaling.string_value;
auto index = value.find('@');
Expand All @@ -287,8 +288,8 @@ struct Room::Impl
s.scale = scale;
s.yPos = yPos;
scaling.getScalings().push_back(s);
}
else if(jSubScaling.isArray())
}
else if (jSubScaling.isArray())
{
for (auto jSubScalingScaling : jSubScaling.array_value)
{
Expand Down Expand Up @@ -584,22 +585,24 @@ void Room::draw(sf::RenderWindow &window, const sf::Vector2f &cameraPos) const
{
sf::RenderStates states;
auto screen = window.getView().getSize();
auto ratio = screen.y / pImpl->_roomSize.y;
auto w = screen.x / 2.f;
auto h = screen.y / 2.f;

for (const auto &layer : pImpl->_layers)
{
auto w = screen.x / 2.f;
auto h = screen.y / 2.f;
auto parallax = layer->getParallax();
auto posX = (w - cameraPos.x) * parallax.x - w;
auto posY = (h - cameraPos.y) * parallax.y - h;

sf::Transform t;
t.rotate(pImpl->_rotation, w, h);
t.translate(posX, posY);
states.transform = t;
layer->draw(window, states);
}

sf::Transform t;
t.rotate(pImpl->_rotation, w, h);
t.translate(-cameraPos);
states.transform = t;
drawWalkboxes(window, states);
Expand All @@ -611,6 +614,7 @@ void Room::draw(sf::RenderWindow &window, const sf::Vector2f &cameraPos) const
auto posY = (screen.y / 2 - cameraPos.y) * parallax.y - screen.y / 2;

sf::Transform t2;
t2.rotate(pImpl->_rotation, w, h);
t2.translate(posX, posY);
states.transform = t2;
layer->drawForeground(window, states);
Expand All @@ -622,9 +626,9 @@ const RoomScaling &Room::getRoomScaling() const
return pImpl->_scaling;
}

void Room::setRoomScaling(const RoomScaling & scaling)
void Room::setRoomScaling(const RoomScaling &scaling)
{
pImpl->_scaling = scaling;
pImpl->_scaling = scaling;
}

void Room::setWalkboxEnabled(const std::string &name, bool isEnabled)
Expand All @@ -649,7 +653,7 @@ bool Room::inWalkbox(const sf::Vector2f &pos) const
return inWalkbox;
}

std::vector<RoomScaling>& Room::getScalings()
std::vector<RoomScaling> &Room::getScalings()
{
return pImpl->_scalings;
}
Expand All @@ -659,10 +663,20 @@ std::vector<sf::Vector2i> Room::calculatePath(const sf::Vector2i &start, const s
return pImpl->_pf->calculatePath(start, end);
}

Light* Room::createLight(sf::Color color, sf::Vector2i pos)
float Room::getRotation() const
{
return pImpl->_rotation;
}

void Room::setRotation(float angle)
{
pImpl->_rotation = angle;
}

Light *Room::createLight(sf::Color color, sf::Vector2i pos)
{
auto light = std::make_unique<Light>(color, pos);
Light* pLight = light.get();
Light *pLight = light.get();
pImpl->_lights.emplace_back(std::move(light));
return pLight;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Scripting/_RoomPack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "squirrel.h"
#include "Animation.h"
#include "Engine.h"
#include "Function.h"
#include "Light.h"
#include "_RoomTrigger.h"

Expand Down Expand Up @@ -285,7 +286,17 @@ class _RoomPack : public Pack

static SQInteger roomRotateTo(HSQUIRRELVM v)
{
std::cerr << "TODO: roomRotateTo: not implemented" << std::endl;
auto pRoom = g_pEngine->getRoom();
SQFloat rotation = 0;
if (SQ_FAILED(sq_getfloat(v, 2, &rotation)))
{
return sq_throwerror(v, _SC("failed to get rotation"));
}
auto get = std::bind(&Room::getRotation, pRoom);
auto set = std::bind(&Room::setRotation, pRoom, std::placeholders::_1);
auto t = sf::seconds(0.200);
auto rotateTo = std::make_unique<ChangeProperty<float>>(get, set, rotation, t);
g_pEngine->addFunction(std::move(rotateTo));
return 0;
}

Expand Down

0 comments on commit 5287c33

Please sign in to comment.