Commit b60fc960 authored by pa's avatar pa

Room system - still needs to add map on top of it

parent fa0d2891
......@@ -10,7 +10,7 @@
class Agent : public SceneNode{
public :
enum Type{
qProjectile, tProjectile, Player, PlayerHit, Tower,
Floor, qProjectile, tProjectile, Player, PlayerHit, Tower,
Sword_0, Sword_1, Sword_2, Sword_3, Sword_4, Sword_5
};
Textures::ID toTextureID(Type type);
......
......@@ -8,10 +8,9 @@ class Game{
public :
Game();
void run();
void processEvents();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
void update(sf::Time deltaTime);
void render();
void processEvents();
private :
sf::RenderWindow mWindow;
......
......@@ -15,7 +15,7 @@ public :
Player(TextureHolder const& Textures);
void q_spell();
void w_spell();
void w_spell(int dir);
virtual void updateCurrent(sf::Time dt);
virtual void act(sf::Time dt);
......
#pragma once
#include "category.hpp"
#include "textureHolder.hpp"
#include "sceneNode.hpp"
#include <SFML/Graphics.hpp>
#include "spriteNode.hpp"
#include "ennemy.hpp"
#include <array>
class Room : public SceneNode{
public:
Room(TextureHolder& textures, sf::IntRect& bounds);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
void attachPlayer(SceneNode::Ptr player);
SceneNode::Ptr detachPlayer(const SceneNode& player);
private:
void loadTextures();
void buildScene();
private:
enum Layer {
Background, Air, LayerCount
};
std::array<SceneNode*, LayerCount> mSceneLayers;
TextureHolder& mTextures;
sf::Vector2f mSpawnPosition;
sf::IntRect& mBounds;
};
......@@ -20,7 +20,7 @@ public :
void stageForRemoval();
bool isStageForRemoval() const{return isMarkedForRemoval;}
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
void update(sf::Time dt);
......
......@@ -18,7 +18,8 @@ template <typename Ressource, typename Identifier> class RessourceHolder {
public:
void load(Identifier id, const std::string& filename);
const Ressource& get(Identifier id) const;
Ressource const& get(Identifier id) const;
Ressource& get(Identifier id);
private:
......
......@@ -8,7 +8,7 @@
#include "ennemy.hpp"
#include "orbital.hpp"
#include <SFML/Graphics.hpp>
#include "room.hpp"
#include <iostream>
#include <array>
......@@ -21,31 +21,18 @@ public:
void update(sf::Time dt);
void draw();
void spawnPlayer(std::unique_ptr<Room>& room);
int getPlayerHP() const;
private:
void loadTextures();
void buildScene();
void checkCollision();
private:
enum Layer
{
Background,
Air,
LayerCount
};
private:
sf::RenderWindow& mWindow;
sf::View mWorldView;
sf::IntRect mWorldBounds;
TextureHolder mTextures;
SceneNode mSceneGraph;
std::array<SceneNode*, LayerCount> mSceneLayers;
sf::FloatRect mWorldBounds;
sf::Vector2f mSpawnPosition;
float mScrollSpeed;
Player* mPlayer;
};
......@@ -3,6 +3,8 @@
Textures::ID Agent::toTextureID(Agent::Type type){
switch(type){
case Floor :
return Textures::ID::Floor;
case qProjectile :
return Textures::ID::qProjectile;
case tProjectile :
......
......@@ -37,21 +37,19 @@ Agent::Type Alive::typeOfState(){
}
void Alive::hit(){
if(!mLock) --mHP;
lock(sf::milliseconds(500.f));
if(mHP <= 0) stageForRemoval();
mState = HitA;
mHitCount = 8;
mHitTime = sf::milliseconds(150.f);
hitAnimation(sf::seconds(0.f));
if(!mLock){
--mHP;
lock(sf::milliseconds(500.f));
if(mHP <= 0) stageForRemoval();
mState = HitA;
mHitCount = 14;
mHitTime = sf::milliseconds(150.f);
}
}
void Alive::hitAnimation(sf::Time dt){
std::cout << mState<< '\n';
switch(mState){
case Normal :
setType(Agent::Player);
break;
case Normal : break;
case HitA : case HitB :
mHitTime -= dt;
......
#include "../headers/ennemy.hpp"
Ennemy::Ennemy(TextureHolder const& textures) : Alive(Agent::Tower,textures,2), mIsQ(true), mIsW(true){
mQCD = sf::seconds(3.f);
mQCD = sf::seconds(2.f);
}
Agent::Type Ennemy::typeOfState(){
......@@ -22,7 +22,7 @@ void Ennemy::q_spell(float sense){
for(int i = 0 ; i < 3 ; ++i){
std::unique_ptr<Orbital>
escortR(new Orbital(Agent::tProjectile, getTexHolder(),
new HellSpeedPatern(positionSetter,sense*49.f,1.007f),false));
new HellSpeedPatern(positionSetter,sense*94.f,1.01f),false));
escortR->setPosition(positionSetter);
attachChild(std::move(escortR));
positionSetter = transform.transformPoint(positionSetter);
......
#include "../headers/game.hpp"
Game::Game() : mWindow(sf::VideoMode(640, 480), "Tests"), mWorld(mWindow){
Game::Game() : mWindow(sf::VideoMode(1920, 1080), "Tests"), mWorld(mWindow){
TimePerFrame = sf::seconds(1.f/60.f);
}
......@@ -24,19 +24,10 @@ void Game::processEvents(){
case sf::Event::Closed :
mWindow.close();
break;
case sf::Event::KeyPressed :
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased :
handlePlayerInput(event.key.code, false);
break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed){}
void Game::update(sf::Time deltaTime){
mWorld.update(deltaTime);
}
......@@ -44,6 +35,9 @@ void Game::update(sf::Time deltaTime){
void Game::render(){
mWindow.clear();
mWorld.draw();
// Truc degeulasse pour afficher des HPs
for(int i = 0 ; i < 8 ; ++i){
sf::Sprite sprite;
sf::Texture tex;
......@@ -55,6 +49,9 @@ void Game::render(){
sprite.setPosition(i*32.f,0.f);
mWindow.draw(sprite);
}
// \Truc degeulasse pour afficher les HP
mWindow.display();
}
......@@ -16,7 +16,7 @@ Agent::Type Player::typeOfState(){
Player::Player(TextureHolder const& textures)
: Alive(Agent::Player,textures,8), mIsQAvailable(true), mIsWAvailable(true){
mQCD = sf::seconds(8.f);
mWCD = sf::seconds(3.f);
mWCD = sf::seconds(1.f);
}
void Player::q_spell(){
......@@ -35,11 +35,12 @@ void Player::q_spell(){
}
}
void Player::w_spell(){
void Player::w_spell(int dir){
if(mIsWAvailable){
mIsWAvailable = false;
std::unique_ptr<Sword> sword(new Sword(getTexHolder()));
sword->rotate(dir*90.f);
attachChild(std::move(sword));
}
}
......@@ -68,6 +69,9 @@ void Player::keyBindings(){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) moveUp();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) moveDown();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) q_spell();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) w_spell();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) q_spell();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) w_spell(3);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) w_spell(2);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) w_spell(1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) w_spell(0);
}
#include "../headers/room.hpp"
Room::Room(TextureHolder& textures, sf::IntRect& bounds)
: mTextures(textures), mBounds(bounds), mSpawnPosition(bounds.width/2.f,bounds.height/2.f){
for(int i = 0 ; i < LayerCount ; ++i)
mSceneLayers[i] = nullptr;
loadTextures();
buildScene();
}
void Room::loadTextures(){
mTextures.load(Textures::Floor, pathOfID(Textures::Floor)); mTextures.get(Textures::Floor).setRepeated(true);
mTextures.load(Textures::qProjectile, pathOfID(Textures::qProjectile));
mTextures.load(Textures::tProjectile, pathOfID(Textures::tProjectile));
mTextures.load(Textures::Tower, pathOfID(Textures::Tower));
mTextures.load(Textures::Player, pathOfID(Textures::Player));
mTextures.load(Textures::PlayerHit, pathOfID(Textures::PlayerHit));
mTextures.load(Textures::Sword_0, pathOfID(Textures::Sword_0));
mTextures.load(Textures::Sword_1, pathOfID(Textures::Sword_1));
mTextures.load(Textures::Sword_2, pathOfID(Textures::Sword_2));
mTextures.load(Textures::Sword_3, pathOfID(Textures::Sword_3));
mTextures.load(Textures::Sword_4, pathOfID(Textures::Sword_4));
mTextures.load(Textures::Sword_5, pathOfID(Textures::Sword_5));
}
void Room::buildScene(){
for(std::size_t i = 0; i < LayerCount ; ++i){
SceneNode::Ptr layer(new SceneNode());
mSceneLayers[i] = layer.get();
attachChild(std::move(layer));
}
std::unique_ptr<SpriteNode> background(new SpriteNode(Agent::Floor,mTextures,mBounds));
background->setPosition(mBounds.width/2.f,mBounds.height/2.f);
mSceneLayers[Background]->attachChild(std::move(background));
std::unique_ptr<Ennemy> ennemy(new Ennemy(mTextures));
ennemy->setPosition(mSpawnPosition);
mSceneLayers[Air]->attachChild(std::move(ennemy));
}
void Room::attachPlayer(SceneNode::Ptr player){
player->setPosition(mSpawnPosition);
mSceneLayers[Air]->attachChild(std::move(player));
}
SceneNode::Ptr Room::detachPlayer(const SceneNode& player){
return mSceneLayers[Air]->detachChild(player);
}
void Room::draw(sf::RenderTarget& target, sf::RenderStates states) const{
states.transform *= getTransform();
for(std::size_t i = 0 ; i < LayerCount ; ++i)
if(mSceneLayers[i] != nullptr)
mSceneLayers[i]->draw(target,states);
}
......@@ -41,7 +41,6 @@ void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const{
void SceneNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const{}
void SceneNode::update(sf::Time dt){
updateCurrent(dt);
for(const auto& child : mChildren) child->update(dt);
......
......@@ -2,7 +2,7 @@
Sword::Sword(TextureHolder const& mTextures)
: Agent(Agent::Sword_1, mTextures), mAnimationTime(sf::milliseconds(150.f)), mIsIncreasing(true){
: Agent(Agent::Sword_1, mTextures), mAnimationTime(sf::milliseconds(80.f)), mIsIncreasing(true){
getSprite().setOrigin(20.f,20.f);
}
......@@ -14,11 +14,17 @@ void Sword::nextType(){
case Agent::Sword_5 :
mIsIncreasing = false;
setType(Agent::Sword_4);
mAnimationTime = sf::milliseconds(150.f);
break;
default :
setType(static_cast<Agent::Type> (static_cast<int>(getType())+(mIsIncreasing? 1 : -1)));
mAnimationTime = sf::milliseconds(150.f);
break;
}
switch(getType()){
case Agent::Sword_5 :
mAnimationTime = sf::milliseconds(200.f);
break;
default :
mAnimationTime = sf::milliseconds(40.f);
break;
}
}
......
......@@ -16,7 +16,16 @@ void RessourceHolder<Ressource,Identifier>::load(Identifier id, const std::strin
}
template<typename Ressource, typename Identifier>
const Ressource& RessourceHolder<Ressource,Identifier>::get(Identifier id) const{
Ressource& RessourceHolder<Ressource,Identifier>::get(Identifier id){
auto found = mRessourceMap.find(id);
if(found == mRessourceMap.end()) throw std::logic_error("TextureHolder::get - Failed to get");
return *(found->second);
}
template<typename Ressource, typename Identifier>
Ressource const& RessourceHolder<Ressource,Identifier>::get(Identifier id) const{
auto found = mRessourceMap.find(id);
if(found == mRessourceMap.end()) throw std::logic_error("TextureHolder::get - Failed to get");
......
......@@ -4,54 +4,17 @@
World::World(sf::RenderWindow& window)
: mWindow(window), mWorldView(window.getDefaultView()),
mWorldBounds(0.f,0.f,mWorldView.getSize().x,2000.f),
mSpawnPosition(mWorldView.getSize().x / 2.f, mWorldView.getSize().y / 2.f){
for(int i = 0 ; i < LayerCount ; ++i)
mSceneLayers[i] = nullptr;
loadTextures();
buildScene();
mWorldBounds(0.f,0.f,mWorldView.getSize().x,mWorldView.getSize().y){
std::unique_ptr<Room> room(new Room(mTextures,mWorldBounds));
spawnPlayer(room);
mSceneGraph.attachChild(std::move(room));
}
void World::loadTextures(){
mTextures.load(Textures::Floor, pathOfID(Textures::Floor));
mTextures.load(Textures::qProjectile, pathOfID(Textures::qProjectile));
mTextures.load(Textures::tProjectile, pathOfID(Textures::tProjectile));
mTextures.load(Textures::Tower, pathOfID(Textures::Tower));
mTextures.load(Textures::Player, pathOfID(Textures::Player));
mTextures.load(Textures::PlayerHit, pathOfID(Textures::PlayerHit));
mTextures.load(Textures::Sword_0, pathOfID(Textures::Sword_0));
mTextures.load(Textures::Sword_1, pathOfID(Textures::Sword_1));
mTextures.load(Textures::Sword_2, pathOfID(Textures::Sword_2));
mTextures.load(Textures::Sword_3, pathOfID(Textures::Sword_3));
mTextures.load(Textures::Sword_4, pathOfID(Textures::Sword_4));
mTextures.load(Textures::Sword_5, pathOfID(Textures::Sword_5));
}
void World::buildScene(){
for(std::size_t i = 0; i < LayerCount ; ++i){
SceneNode::Ptr layer(new SceneNode());
mSceneLayers[i] = layer.get();
mSceneGraph.attachChild(std::move(layer));
}
// sf::IntRect textureRect(mWorldBounds);
// std::unique_ptr<SpriteNode> backgroundSprite(new SpriteNode(SpriteNode::Desert,textureRect));
// backgroundSprite->setTexture(mTextures);
// backgroundSprite->setPosition(mWorldBounds.left, mWorldBounds.top);
// mSceneLayers[Background]->attachChild(std::move(backgroundSprite));
void World::spawnPlayer(std::unique_ptr<Room>& room){
std::unique_ptr<Player> leader(new Player(mTextures));
mPlayer = leader.get();
mPlayer->setPosition(mSpawnPosition/2.f);
mPlayer->setVelocity(40.f,mScrollSpeed);
mSceneLayers[Air]->attachChild(std::move(leader));
std::unique_ptr<Ennemy> ennemy(new Ennemy(mTextures));
ennemy->setPosition(mSpawnPosition);
ennemy->setVelocity(40.f,mScrollSpeed);
mSceneLayers[Air]->attachChild(std::move(ennemy));
room->attachPlayer(std::move(leader));
}
int World::getPlayerHP() const{
......@@ -60,10 +23,7 @@ int World::getPlayerHP() const{
void World::draw(){
mWindow.setView(mWorldView);
for(std::size_t i = 0 ; i < LayerCount ; ++i)
if(mSceneLayers[i] != nullptr)
mWindow.draw(*mSceneLayers[i]);
mWindow.draw(mSceneGraph);
}
void World::update(sf::Time dt){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment