Merge branch 'master' of 192.168.1.106:/home/pi/data/git/dungeon-gunner

This commit is contained in:
Felix Ableitner 2013-09-17 23:56:35 +02:00 committed by Felix Ableitner
commit df94bad5e8
10 changed files with 64 additions and 37 deletions

View file

@ -10,6 +10,7 @@ R: reload
Q: use left Gadget
E: use right Gadget
F: pick up item or swap Gadgets
Esc: exit
## Dependencies
- SFML

4
res/yaml/window.yaml Normal file
View file

@ -0,0 +1,4 @@
resolution_width: 1024
resolution_height: 768
fullscreen: false
borderless: false

View file

@ -66,12 +66,30 @@ Game::initPlayer() {
Weapon::WeaponType::PISTOL, Weapon::WeaponType::KNIFE,
Gadget::GadgetType::NONE, Gadget::GadgetType::NONE
};
mGenerator.generateCurrentAreaIfNeeded(Vector2f(), playerItems);
auto enemySpawns = mGenerator.generateCurrentAreaIfNeeded(Vector2f());
mPlayer = std::shared_ptr<Player>(new Player(mWorld, mPathfinder,
mGenerator.getPlayerSpawn(), playerItems));
mWorld.insertCharacter(mPlayer);
insertEnemies(enemySpawns);
}
/**
* Inserts enemies at the given positions, if they are more than
* Character::VISION_DISTANCE away from the player's postion.
*/
void
Game::insertEnemies(const std::vector<Vector2f>& positions) {
for (const auto& spawn : positions) {
if (thor::length(spawn - mPlayer->getPosition()) >
Character::VISION_DISTANCE)
mWorld.insertCharacter(std::make_shared<Enemy>(mWorld,
mPathfinder, spawn, mPlayer->getEquippedItems()));
}
}
/**
* Initializes the lights held by the player and sets light system parameters.
*/
void
Game::initLight() {
Yaml config("light.yaml");
@ -132,7 +150,8 @@ Game::loop() {
render();
mGenerator.generateCurrentAreaIfNeeded(mPlayer->getPosition(), mPlayer->getEquippedItems());
auto enemySpawns = mGenerator.generateCurrentAreaIfNeeded(mPlayer->getPosition());
insertEnemies(enemySpawns);
}
}
@ -341,7 +360,10 @@ Game::render() {
// Update light
mPlayerAreaLight->SetCenter(mPlayer->getPosition().toVec2f());
mPlayerDirectionLight->SetCenter(mPlayer->getPosition().toVec2f());
// Avoid light light drawing partially onto player sprite.
Vector2f playerLightPosition = mPlayer->getPosition() +
thor::rotatedVector(Vector2f(0, - 13), mPlayer->getDirection());
mPlayerDirectionLight->SetCenter(playerLightPosition.toVec2f());
mPlayerDirectionLight->SetDirectionAngle(degreeToRadian(90 - mPlayer->getDirection()));
mLightSystem.SetView(mWorldView);

View file

@ -43,6 +43,7 @@ private:
void updateGui();
void initPlayer();
void initLight();
void insertEnemies(const std::vector<Vector2f>& positions);
private:
static const int FPS_GOAL = 60;

View file

@ -121,9 +121,9 @@ World::applyMovement(std::shared_ptr<Sprite> sprite, int elapsed) {
void
World::think(int elapsed) {
for (auto it = mCharacters.begin(); it != mCharacters.end(); ) {
if ((*it)->getDelete() && (*it)->getCategory() != Sprite::CATEGORY_ACTOR) {
if ((*it)->getDelete()) {
mCharacters.erase(it);
auto& d = mDrawables[(*it)->getCategory()];
auto& d = mDrawables[Sprite::CATEGORY_ACTOR];
d.erase(std::find(d.begin(), d.end(), *it));
}
else {

View file

@ -39,15 +39,17 @@ Generator::Generator(World& world, Pathfinder& pathfinder,
/**
* Generates tiles near player position (maximum distance is determined by
* GENERATE_AREA_SIZE and GENERATE_AREA_RANGE).
*
* @return Potential spawn points for enemies. Guaranteed to be on floor tiles.
*/
void
Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition,
const Character::EquippedItems& playerItems) {
std::vector<Vector2f>
Generator::generateCurrentAreaIfNeeded(const Vector2f& position) {
std::vector<Vector2f> enemySpawns;
std::map<Vector2i, float> open;
std::set<Vector2i> closed;
Vector2i start((int) floor(playerPosition.x / Tile::TILE_SIZE.x),
(int) floor(playerPosition.y / Tile::TILE_SIZE.y));
Vector2i start((int) floor(position.x / Tile::TILE_SIZE.x),
(int) floor(position.y / Tile::TILE_SIZE.y));
start /= mAreaSize;
auto makePair = [&start](const Vector2i& point) {
return std::make_pair(point, thor::length(Vector2f(point - start)));
@ -65,12 +67,8 @@ Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition,
Vector2i(mAreaSize, mAreaSize) / 2,
Vector2i(mAreaSize, mAreaSize));
generateTiles(area);
for (const auto& spawn : getEnemySpawns(area)) {
float distance = thor::length(spawn - playerPosition);
if (distance > Character::VISION_DISTANCE)
mWorld.insertCharacter(std::shared_ptr<Enemy>(new Enemy(
mWorld, mPathfinder, spawn, playerItems)));
}
auto spawnTemp = getEnemySpawns(area);
enemySpawns.insert(enemySpawns.end(), spawnTemp.begin(), spawnTemp.end());
}
if (mGenerated[current.x][current.y] && distance <= mMaxRange) {
if (closed.find(Vector2i(current.x + 1, current.y)) == closed.end())
@ -83,6 +81,7 @@ Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition,
open.insert(makePair(Vector2i(current.x, current.y - 1)));
}
}
return enemySpawns;
}
/**

View file

@ -28,10 +28,8 @@ class Generator : public sf::Drawable {
public:
explicit Generator(World& world, Pathfinder& pathfinder,
ltbl::LightSystem& lightSystem, const Yaml& config);
void generateCurrentAreaIfNeeded(const Vector2f& position,
const Character::EquippedItems& playerItems);
std::vector<Vector2f> generateCurrentAreaIfNeeded(const Vector2f& position);
Vector2f getPlayerSpawn() const;
std::vector<Vector2f> getEnemySpawns(const sf::IntRect& area);
private:
typedef std::map<int, std::map<int, Tile::Type> > array;
@ -43,6 +41,7 @@ private:
std::vector<Vector2i> createMinimalSpanningTree(
const Vector2i& start, const float limit);
void connectRooms(const Vector2i& start);
std::vector<Vector2f> getEnemySpawns(const sf::IntRect& area);
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
private:

View file

@ -17,12 +17,22 @@
*/
int main(int argc, char* argv[]) {
Yaml::setFolder("res/yaml/");
Loader::i().setFolder("res/");
Loader::i().setSubFolder<sf::Texture>("textures/");
tgui::Window window(sf::VideoMode(1024, 768, 32), "Dungeon Gunner",
sf::Style::Close | sf::Style::Titlebar);
Yaml windowConfig("window.yaml");
sf::VideoMode mode(windowConfig.get("resolution_width", 800),
windowConfig.get("resolution_height", 600),
32);
sf::Uint32 style;
if (windowConfig.get("fullscreen", false))
style = sf::Style::Fullscreen;
else if (windowConfig.get("borderless", false))
style = sf::Style::None;
else
style = sf::Style::Titlebar;
tgui::Window window(mode, "Dungeon Gunner", style);
Vector2f::SCREEN_HEIGHT = window.getSize().y;
if (!window.globalFont.loadFromFile("res/DejaVuSans.ttf"))

View file

@ -34,7 +34,7 @@ Enemy::Enemy(World& world, Pathfinder& pathfinder,
Character::EquippedItems
Enemy::generateItems(EquippedItems playerItems) {
// Uses cast from enum to int to enum in order to increment enum values.
switch (rand() % 4) {
switch (rand() % 2) {
case 0:
if (playerItems.primary + 1 != Weapon::WeaponType::_LAST)
playerItems.primary =
@ -46,20 +46,12 @@ Enemy::generateItems(EquippedItems playerItems) {
playerItems.secondary =
(Weapon::WeaponType) (((int) (playerItems.secondary) + 1));
break;
case 2:
if (playerItems.left + 1 != Gadget::GadgetType::_LAST)
playerItems.left = (Gadget::GadgetType) (((int) (playerItems.left)
+ 1));
break;
case 3:
if (playerItems.right + 1 != Gadget::GadgetType::_LAST)
playerItems.left = (Gadget::GadgetType) (((int) (playerItems.left)
+ 1));
break;
}
playerItems.left = (Gadget::GadgetType) (rand() % 4);
playerItems.right = (Gadget::GadgetType) (rand() % 4);
return playerItems;
}

View file

@ -23,8 +23,7 @@ public:
NONE,
SHIELD,
HEAL,
RINGOFFIRE,
_LAST
RINGOFFIRE
};
public: