You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

84 lines
2.3 KiB

#include "world.h"
#include "spritenode.h"
#include "enemy.h"
#include <cmath>
World::World(sf::RenderWindow& window) :
world_window(window),
world_view(window.getDefaultView()),
scene_graph(new SceneNode()),
world_bounds(0.f, 0.f, world_view.getSize().x, 2000.f),
world_spawn_pos(world_view.getSize().x / 2.f, world_bounds.height - world_view.getSize().y),
player(nullptr)
{
loadTextures();
buildScene();
world_view.setCenter(world_spawn_pos);
}
void World::update(const sf::Time &dt)
{
world_view.move(0.f, world_speed * dt.asSeconds());
player->setVelocity(0.f, 0.f);
while (!queue_commands.empty())
{
scene_graph->onCommand(queue_commands.front(), dt);
queue_commands.pop();
}
//sf::Vector2f position = player->getPosition();
sf::Vector2f velocity = player->velocity();
if (velocity.x != 0.f && velocity.y != 0.f)
player->setVelocity(velocity / std::sqrt(2.f));
player->accelerate({0.f, world_speed});
scene_graph->update(dt);
}
void World::draw()
{
world_window.setView(world_view);
world_window.draw(*scene_graph);
}
std::queue<Command>& World::getCommandQueue()
{
return queue_commands;
}
bool World::loadTextures()
{
return /*texture_holder.load(Textures::Id::Weakling, "sprite_weakling.png")
&& texture_holder.load(Textures::Id::Shotguner, "sprite_shotguner.png")
&& */texture_holder.load(Textures::Id::Background, "sprite_back.png")
&& texture_holder.load(Textures::Id::Player, "sprite.png");
}
void World::buildScene()
{
for (std::size_t i = 0; i < LayerCount; ++i)
{
SceneNodeSPtr layer(new SceneNode());
arr_layers[i] = layer;
scene_graph->attachChild(layer);
}
sf::Texture texture = texture_holder.get(Textures::Id::Background);
sf::IntRect texture_rect(world_bounds);
texture.setRepeated(true);
SpriteNodeSPtr background_sprite(new SpriteNode(texture, texture_rect));
background_sprite->setPosition(world_bounds.left, world_bounds.top);
arr_layers[Background]->attachChild(background_sprite);
player = std::make_shared<Enemy>(Enemy::Type::Player, texture_holder);
player->setPosition(world_spawn_pos);
player->setVelocity(40.f, world_speed);
arr_layers[Air]->attachChild(player);
}