Commit dc67039d authored by pa's avatar pa

Added better doors && homing speed patern

parent 19e6692d
......@@ -12,7 +12,7 @@ class Agent : public SceneNode{
enum Type{
Floor, qProjectile, tProjectile, Player, PlayerHit, Tower,
SwordFloor, Sword_0, Sword_1, Sword_2, Sword_3, Sword_4, Sword_5,
Doors, HP_full, HP_down, Poison, Void, Coin, Key, LockedDoors
Doors, HP_full, HP_down, Poison, Void, Coin, Key, LockedDoors, UltraLockedDoors
};
static Textures::ID toTextureID(Type type);
......
......@@ -4,6 +4,7 @@
#include "orbital.hpp"
#include "entity.hpp"
#include "category.hpp"
#include "collectible.hpp"
......@@ -28,6 +29,9 @@ public :
void moveRight();
void moveDown();
void loot();
void drop(Collectible::Type type);
void lock(sf::Time dt);
void unlock();
bool isLocked() const {return mLock;}
......
......@@ -11,7 +11,7 @@ public :
typedef Room* RPtr;
typedef std::vector<std::vector<int>> IMap;
typedef std::vector<RPtr> Map;
Carte(TextureHolder &textures, sf::IntRect& bounds, float r, float alpha, int n);
Carte(TextureHolder &textures, sf::IntRect& bounds, float r, float alpha, float inDepth, int n);
void move(Door::Dir dir){
mX += xOfDir(dir); mY += yOfDir(dir);
......@@ -26,7 +26,7 @@ private :
int yOfDir(Door::Dir);
int xOfDir(Door::Dir);
void firstPass(TextureHolder &textures, sf::IntRect& bounds, float r, float alpha);
void firstPass(TextureHolder &textures, sf::IntRect& bounds, float r, float alpha, float inDepth);
void addDoors();
private :
......
......@@ -5,6 +5,7 @@
class Door : public Agent{
public :
enum DoorType{Unlocked,Locked,UltraLocked,DeadEnd};
enum Dir{UP, RIGHT, DOWN, LEFT};
static Agent::Type ofLockLevel(int ll);
......
......@@ -16,7 +16,7 @@
class Room : public SceneNode{
public:
Room(TextureHolder& textures, sf::IntRect& bounds);
Room(TextureHolder& textures, sf::IntRect& bounds, int depth);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
......@@ -45,4 +45,6 @@ private:
sf::Vector2f mSpawnPosition;
sf::IntRect& mBounds;
int mDepth;
};
......@@ -34,6 +34,7 @@ public :
sf::Vector2f getWorldPosition() const;
virtual Category::Category identify() const{return Category::SceneNode;}
SceneNode* getParent() const{return mParent;}
virtual sf::FloatRect getBounds() const;
bool collidesWith(SceneNode& node) const;
......
......@@ -2,6 +2,7 @@
#include <iostream>
#include <SFML/Graphics.hpp>
#include "sceneNode.hpp"
class SpeedPatern : public sf::Transform{
public :
......@@ -15,6 +16,17 @@ private :
};
class HomingSpeedPatern : public SpeedPatern {
public :
HomingSpeedPatern(sf::Vector2f base, SceneNode* target, float max_speed, float max_acc);
virtual void step(sf::Time dt);
void specifySource(SceneNode* source){mSource = source;}
private :
SceneNode* mTarget, *mSource;
float mSpeed, mAcc;
};
class RotationSpeedPatern : public SpeedPatern{
public :
......
......@@ -10,7 +10,7 @@ namespace Textures{
enum ID{
Floor, qProjectile, tProjectile, Player, PlayerHit, Tower, Poison,
SwordFloor, Sword_0, Sword_1, Sword_2, Sword_3, Sword_4, Sword_5,
Doors, HP_full, HP_down, Void, Coin , Key, LockedDoors
Doors, HP_full, HP_down, Void, Coin , Key, LockedDoors, UltraLockedDoors
};
}
......
#pragma once
#include <iostream>
#include <ctime>
int random_int(int);
float random_float();
......@@ -45,6 +45,8 @@ Textures::ID Agent::toTextureID(Agent::Type type){
return Textures::ID::Coin;
case LockedDoors :
return Textures::ID::LockedDoors;
case UltraLockedDoors :
return Textures::ID::UltraLockedDoors;
default :
throw std::logic_error("Agent::toTextureID, non-exhaustive pattern-matching");
}
......
#include "../headers/alive.hpp"
#include "../headers/utilitary.hpp"
Alive::Alive(Agent::Type type, TextureHolder const& textures, int HP, Entity::Side side)
:Entity(type,textures), mHP(HP), mState(Normal), mSide(side), mPoisoned(false){}
......@@ -15,14 +16,17 @@ void Alive::updateCurrent(sf::Time dt){
hitAnimation(dt);
setType(typeOfState());
Entity::updateCurrent(dt);
setVelocity(0.f,0.f);}
setVelocity(0.f,0.f);
}
else{
act(dt);
mLockTime -= dt;
if(mLockTime.asSeconds() <= 0.f)
unlock();
hitAnimation(dt);
setType(typeOfState());
Entity::updateCurrent(dt);
setVelocity(0.f,0.f);
}
}
......@@ -46,10 +50,10 @@ void Alive::hit(bool poisonous){
mPoisoned = mPoisoned || poisonous;
if(!mLock){
--mHP;
lock(sf::milliseconds(500.f));
if(mHP <= 0) stageForRemoval();
lock(sf::milliseconds(900.f));
if(mHP <= 0){loot(); stageForRemoval();}
mState = HitA;
mHitCount = 14;
mHitCount = 6;
mHitTime = sf::milliseconds(150.f);
}
}
......@@ -74,16 +78,16 @@ void Alive::hitAnimation(sf::Time dt){
}
void Alive::moveUp(){
getVelocity() += sf::Vector2f(0.f,-400.f);
if(getState() == Normal) getVelocity() += sf::Vector2f(0.f,-400.f);
}
void Alive::moveDown(){
getVelocity() += sf::Vector2f(0.f,400.f);
if(getState() == Normal) getVelocity() += sf::Vector2f(0.f,400.f);
}
void Alive::moveLeft(){
getVelocity() += sf::Vector2f(-400.f,0.f);
if(getState() == Normal) getVelocity() += sf::Vector2f(-400.f,0.f);
}
void Alive::moveRight(){
getVelocity() += sf::Vector2f(400.f,0.f);
if(getState() == Normal) getVelocity() += sf::Vector2f(400.f,0.f);
}
void Alive::onCollideWith(SceneNode& collider){
......@@ -91,5 +95,22 @@ void Alive::onCollideWith(SceneNode& collider){
case Category::Orbital : case Category::Sword :
if(static_cast<Damageable&>(collider).getSide() != mSide)
hit(static_cast<Damageable&>(collider).isPoisonous());
break;
case Category::Ennemy : case Category::Player :
hit(false);
break;
}
}
void Alive::loot(){
float rf = random_float();
if(rf <= 0.1) drop(Collectible::Key);
else if(rf <= 0.9) drop(Collectible::Poison);
else drop(Collectible::Coin);
}
void Alive::drop(Collectible::Type type){
Collectible *coll = new Collectible(getTexHolder(),type);
getParent()->attachChild(coll);
coll->move(getWorldPosition());
}
#include "../headers/carte.hpp"
#include <iostream>
#include <ctime>
#include "../headers/utilitary.hpp"
#define NO_FILE 4
int random_int(){return 1 + rand() % NO_FILE;}
float random_float(){float f = (1.f * rand()) / (1.f * RAND_MAX); return f;}
Carte::Carte(TextureHolder& textures, sf::IntRect& bounds, float r, float alpha, int n)
Carte::Carte(TextureHolder& textures, sf::IntRect& bounds, float r, float alpha, float inDepth, int n)
: mCarte(n*n), mWidth(n),
mDoors(n,std::vector<int>(n,0)),
mX(n/2),mY(n/2){
firstPass(textures, bounds, r, alpha);
firstPass(textures, bounds, r, alpha, inDepth);
}
int Carte::xOfDir(Door::Dir dir){
......@@ -47,11 +44,12 @@ bool Carte::inCarte(int x, int y){
}
void Carte::firstPass(TextureHolder& textures, sf::IntRect& bounds, float r, float alpha){
void Carte::firstPass(TextureHolder& textures, sf::IntRect& bounds, float r, float alpha, float inDepth){
int terminaison = 1 << 4;
mDoors[mWidth/2][mWidth/2] = terminaison;
std::stack<std::pair<int,int>> st; st.push(std::make_pair(mWidth/2,mWidth/2));
std::vector<std::vector<int>> depths(mWidth,std::vector<int>(mWidth,0));
while(!st.empty()){
int x = st.top().first, y = st.top().second; st.pop();
......@@ -59,7 +57,10 @@ void Carte::firstPass(TextureHolder& textures, sf::IntRect& bounds, float r, flo
Door::Dir dir = static_cast<Door::Dir>(i);
int x2 = x + xOfDir(dir), y2 = y + yOfDir(dir);
if(inCarte(x2,y2) && !(mDoors[x][y]&(1<<dir)) && random_float() <= r){
if(mDoors[x2][y2] <= terminaison){st.push(std::make_pair(x2,y2)); mDoors[x2][y2]+=terminaison;}
if(mDoors[x2][y2] <= terminaison){
st.push(std::make_pair(x2,y2)); mDoors[x2][y2]+=terminaison;
depths[x2][y2] = depths[x][y] + (random_float() <= inDepth ? 1 : 0);
}
mDoors[x][y] += (1 << dir); mDoors[x2][y2] += 1 << invDir(dir);
}
}
......@@ -70,8 +71,8 @@ void Carte::firstPass(TextureHolder& textures, sf::IntRect& bounds, float r, flo
for(int j = 0 ; j < mWidth ; ++j){
if(mDoors[i][j]>0){
mDoors[i][j] -= terminaison;
Room *room = new Room(textures, bounds);
std::ostringstream osstr; osstr << "data/room_" << random_int () << ".dat";
Room *room = new Room(textures, bounds, std::min(depths[i][j],static_cast<int>(Door::DeadEnd)-1));
std::ostringstream osstr; osstr << "data/room_" << random_int(NO_FILE) << ".dat";
room->buildScene(osstr.str());
for(int k = 0 ; k < 4 ; ++k){
if((mDoors[i][j] & (1 << k)))
......@@ -83,12 +84,11 @@ void Carte::firstPass(TextureHolder& textures, sf::IntRect& bounds, float r, flo
}
int i = mWidth/2,j = mWidth/2;
delete mCarte[i*mWidth+j];
mCarte[i*mWidth+j] = new Room(textures,bounds);
mCarte[i*mWidth+j] = new Room(textures,bounds,0);
mCarte[i*mWidth+j]->buildScene("data/room_0.dat");
for(int k = 0 ; k < 4 ; ++k){
if((mDoors[i][j] & (1 << k)))
mCarte[i*mWidth+j]->addDoor(static_cast<Door::Dir>(k));
}
}
......@@ -4,6 +4,7 @@ Agent::Type Door::ofLockLevel(int ll){
switch(ll){
case 0 : return Agent::Doors;
case 1 : return Agent::LockedDoors;
case 2 : return Agent::UltraLockedDoors;
}
}
......
#include "../headers/ennemy.hpp"
Ennemy::Ennemy(TextureHolder const& textures) : Alive(Agent::Tower,textures,12,Entity::Ennemy), mIsQ(true), mIsW(true){
Ennemy::Ennemy(TextureHolder const& textures) : Alive(Agent::Tower,textures,12,Entity::Ennemy),
mIsQ(true), mIsW(true){
mQCD = sf::seconds(4.f);
markForReset();
}
......@@ -23,6 +24,8 @@ void Ennemy::q_spell(float sense){
for(int i = 0 ; i < 3 ; ++i){
Orbital* orb = new Orbital(Agent::tProjectile, getTexHolder(),
new HellSpeedPatern(positionSetter,sense*94.f,1.005f),Entity::Ennemy);
/*Orbital* orb = new Orbital(Agent::tProjectile, getTexHolder(),
new HomingSpeedPatern(positionSetter, mTGT, 100.f, 5.f),Entity::Ennemy);*/
orb->setPosition(positionSetter);
attachChild(orb);
positionSetter = transform.transformPoint(positionSetter);
......
#include "../headers/room.hpp"
Room::Room(TextureHolder& textures, sf::IntRect& bounds)
: mTextures(textures), mBounds(bounds), mSpawnPosition(bounds.width/2.f,bounds.height/2.f){
Room::Room(TextureHolder& textures, sf::IntRect& bounds, int depth)
: mTextures(textures), mBounds(bounds), mSpawnPosition(bounds.width/2.f,bounds.height/2.f), mDepth(depth){
for(int i = 0 ; i < LayerCount ; ++i)
mSceneLayers[i] = nullptr;
loadTextures();
//buildScene();
}
void Room::loadTextures(){
......@@ -29,6 +28,7 @@ void Room::loadTextures(){
mTextures.load(Textures::Coin, pathOfID(Textures::Coin));
mTextures.load(Textures::Key, pathOfID(Textures::Key));
mTextures.load(Textures::LockedDoors, pathOfID(Textures::LockedDoors));
mTextures.load(Textures::UltraLockedDoors, pathOfID(Textures::UltraLockedDoors));
}
......@@ -120,7 +120,7 @@ float Room::getDoorPlacement_Y(Door::Dir dir){
void Room::addDoor(Door::Dir dir){
float x = mBounds.width, y = mBounds.height;
Door* door(new Door(mTextures,dir,1));
Door* door(new Door(mTextures,dir,mDepth));
door->setPosition(getDoorPlacement_X(dir)*x,getDoorPlacement_Y(dir)*y);
mSceneLayers[Background]->attachChild(door);
}
......
#include "../headers/speedPatern.hpp"
#include <cmath>
SpeedPatern::SpeedPatern(sf::Vector2f base) : mBase(base){}
......@@ -9,6 +10,20 @@ sf::Vector2f SpeedPatern::getVelocity() const{
}
HomingSpeedPatern::HomingSpeedPatern(sf::Vector2f base, SceneNode* target, float mspeed, float macc)
: SpeedPatern(base), mSpeed(mspeed) , mAcc(macc) , mTarget(target){}
void HomingSpeedPatern::step(sf::Time dt){
sf::Vector2f dir = mTarget->getWorldPosition() - mSource->getWorldPosition();
if(dir.x == 0 && dir.y == 0) scale(0.f,0.f);
else{
float mag = sqrt(dir.x*dir.x + dir.y*dir.y);
dir /= mag * dt.asSeconds() * mAcc;
translate(dir);
}
}
RotationSpeedPatern::RotationSpeedPatern(sf::Vector2f base, float rpm)
: SpeedPatern(base), mRPM(rpm){
rotate(90.f);
......@@ -27,3 +42,4 @@ void HellSpeedPatern::step(sf::Time dt){
RotationSpeedPatern::step(dt);
scale(mSCL,mSCL);
}
......@@ -99,6 +99,9 @@ std::string pathOfID(Textures::ID id){
case Textures::LockedDoors:
str = "res/ldoor.png";
break;
case Textures::UltraLockedDoors:
str = "res/uldoor.png";
break;
default :
throw std::logic_error("TextureHolder::pathOfID, Non-exhaustive pattern-matching");
}
......
#include "../headers/utilitary.hpp"
int random_int(int max){return 1 + rand() % max;}
float random_float(){float f = (1.f * rand()) / (1.f * RAND_MAX); return f;}
......@@ -4,7 +4,7 @@
World::World(sf::RenderWindow& window)
: mWindow(window), mWorldView(window.getDefaultView()),
mWorldBounds(0.f,0.f,mWorldView.getSize().x,mWorldView.getSize().y),
mCarte(mTextures,mWorldBounds,0.8f,0.8f,10){
mCarte(mTextures,mWorldBounds,0.9f,0.9f,0.4f,20){
mRoom = mCarte.checkIn();
spawnPlayer(mRoom);
mSceneGraph.attachChild(mRoom);
......@@ -63,7 +63,6 @@ void World::checkCollision(){
for(auto &pair : stack){
if(pair.first->identify() == Category::Player && pair.second->identify() == Category::Door){
if(static_cast<Door&>(*pair.second).lockLevel() <= static_cast<Player&>(*pair.first).getKeys()){
std::cout << static_cast<Door&>(*pair.second).lockLevel() << ' ' << static_cast<Player&>(*pair.first).getKeys() << '\n';
mPlayer = static_cast<Player*>(mRoom->detachPlayer(*mPlayer));
Room* room = static_cast<Room*>(mSceneGraph.detachChild(*mRoom));
mCarte.checkOut(room);
......
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