Browse Source

make level fade away with timer

master
noerw 1 year ago
parent
commit
2b19d79509
3 changed files with 42 additions and 26 deletions
  1. 1
    0
      game.cpp
  2. 24
    18
      level.cpp
  3. 17
    8
      level.hpp

+ 1
- 0
game.cpp View File

@@ -34,6 +34,7 @@ namespace Proto4 {
break;

case GameState::Running:
level.update(timestep);
player.update(timestep, window.mapPixelToCoords(sf::Mouse::getPosition(window), mainView));
// mainView acts as camera. TODO: introduce lagging camera
mainView.setCenter(player.getPosition());

+ 24
- 18
level.cpp View File

@@ -6,45 +6,51 @@ namespace Proto4 {

Ground::Ground() {}

Ground::Ground(sf::Vector2f pos, float size, sf::Time timeout) {
shape = sf::CircleShape{size, 50};
shape.setFillColor(sf::Color(90, 100, 120));
shape.setOrigin(size, size);
shape.setPosition(pos);
Ground::Ground(sf::Vector2f pos, float size, sf::Time fadeDuration)
: sf::CircleShape(size, 50)
, Timer(fadeDuration) {
setFillColor(sf::Color(90, 100, 120));
setOrigin(size, size);
setPosition(pos);
}

void Ground::update(float timestep) {}

void Ground::draw(sf::RenderTarget &window, sf::RenderStates states) const {
window.draw(shape);
// TODO: self destruct once 0?
void Ground::update(sf::Time timestep) {
Timer::update(timestep);
sf::Color col = getFillColor();
col.a = getPercent() * 255;
setFillColor(col);
}

/////////////////////////////////////////

Level::Level() {}
Level::Level(sf::Vector2f pos = sf::Vector2f(0, 0)) {
lastSpawn.position = pos;
Proto4::rotate(lastSpawn.direction, sf::Vector2f(1, 1), randomFloat() * 360.f);
setPosition(pos);

// initialize with first ground
spawn(400);
Proto4::rotate(lastSpawn.direction, sf::Vector2f(1, 1), randomFloat() * 360.f);
}

void Level::update(float timestep) {}
void Level::update(sf::Time timestep) {
for (auto &ground : groundArea) {
ground.update(timestep);
}
}

void Level::draw(sf::RenderTarget &window, sf::RenderStates states) const {
void Level::draw(sf::RenderTarget &target, sf::RenderStates states) const {
states.transform *= getTransform();
for (auto &ground : groundArea) {
window.draw(ground);
target.draw(ground, states);
}
}

void Level::spawn(float size) {
float firstOffset = lastSpawn.size == 0 ? 0.f : 1.f;

lastSpawn.direction = normalized(Proto4::rotate(lastSpawn.direction, randomFloat() * 160 - 80));
lastSpawn.position += lastSpawn.direction * (lastSpawn.size + firstOffset * 0.7f * size);
lastSpawn.position += lastSpawn.direction * (lastSpawn.size + 0.7f * size);
lastSpawn.size = size;

groundArea.push_back(Ground(lastSpawn.position, size, sf::seconds(10000)));
groundArea.push_back(Ground(lastSpawn.position, size, sf::seconds(10)));
}
}

+ 17
- 8
level.hpp View File

@@ -1,20 +1,29 @@
#pragma once

#include <cmath>
#include <SFML/Graphics.hpp>

namespace Proto4 {

class Timer {
const sf::Time duration;
sf::Time elapsed = sf::seconds(0.f);

class Ground : public sf::Drawable, public sf::Transformable {
sf::CircleShape shape{};
public:
Timer() : duration(sf::seconds(10)) {}
Timer(sf::Time duration) : duration(duration) {}
void update(sf::Time timestep) { elapsed += timestep; }
float getPercent() {
return std::max(0.f, 1 - elapsed / duration);
}
};

class Ground : public sf::CircleShape, Timer {
public:
Ground();
Ground(sf::Vector2f pos, float size, sf::Time timeout);
// Ground(Killable parentEntity);
Ground(sf::Vector2f pos, float size, sf::Time fadeDuration);

virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
void update(float timestep);
void update(sf::Time timestep);
};


@@ -32,8 +41,8 @@ namespace Proto4 {
Level();
Level(sf::Vector2f pos);

virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
void update(float timestep);
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
void update(sf::Time timestep);
void spawn(float size);
};
}

Loading…
Cancel
Save