Grog-Knight

some game in C++
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/Grog-Knight

commit 52b98cdd408621d22f71814c8f0232537dffdd6f
parent e0fa632dad11146d5f88ea762519fb99868d56ae
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 22 May 2015 15:08:59 +0200

Add(Enemy): Add an Enemy spawn:

Add a new way for spawning Enemies on a map
Little fact: I have to move Enemy->display() from Enemy constructor to Enemy->init()
(Needed to set X/Y before a display in Map Parser)
Little fixes in the getEnemyRandom too. (gg @N0ich)

Diffstat:
MMaps/MapCastle_3.json | 12++++++++++--
MMaps/MapCastle_4.json | 10+++++++---
MResources/Elements/Enemies/Enemy2.json | 2+-
MResources/Images/castle_1.png | 0
MSources/src/Characters.cpp | 9+++++++--
MSources/src/Elements.cpp | 4++++
MSources/src/Enemy.cpp | 10+---------
MSources/src/EnemyList.cpp | 8++++----
MSources/src/Game.cpp | 2--
MSources/src/Hero.cpp | 5+++++
MSources/src/Map.cpp | 11+++++++++++
11 files changed, 50 insertions(+), 23 deletions(-)

diff --git a/Maps/MapCastle_3.json b/Maps/MapCastle_3.json @@ -12,7 +12,7 @@ "y":0 }, { - "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 63, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "height":16, "name":"Tile Layer 2", "opacity":1, @@ -22,6 +22,7 @@ "x":0, "y":0 }], + "nextobjectid":1, "orientation":"orthogonal", "properties": { @@ -33,11 +34,12 @@ "forceStart":"1", "number":"1" }, + "renderorder":"right-down", "tileheight":32, "tilesets":[ { "firstgid":1, - "image":"Maps\/Resources\/Images\/castle_1.png", + "image":"Resources\/Images\/castle_1.png", "imageheight":288, "imagewidth":288, "margin":0, @@ -313,6 +315,12 @@ { "physic":"0" }, + "71": + { + "isFlying":"0", + "physic":"0", + "spawnEnemy":"true" + }, "76": { "hitbox":"lowerHalfHitbox", diff --git a/Maps/MapCastle_4.json b/Maps/MapCastle_4.json @@ -1,7 +1,7 @@ { "height":16, "layers":[ { - "data":[57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 40, 40, 40, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 50, 49, 49, 49, 49, 51, 40, 40, 40, 50, 49, 49, 49, 49, 51, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 50, 40, 40, 40, 40, 40, 40, 40, 51, 40, 40, 40, 40, 40, 40, 40, 40, 55, 65, 40, 40, 40, 40, 40, 40, 40, 40, 55, 80, 80, 80, 80, 80, 80, 80, 56, 40, 40, 40, 40, 40, 40, 40, 40, 64, 57, 48, 48, 47, 40, 40, 40, 40, 40, 55, 80, 80, 80, 80, 80, 80, 80, 56, 40, 40, 40, 40, 40, 46, 48, 48, 57, 56, 40, 40, 40, 40, 40, 40, 40, 40, 55, 80, 80, 80, 80, 80, 80, 80, 56, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 55, 80, 80, 80, 80, 80, 80, 80, 56, 40, 40, 40, 40, 40, 40, 40, 40, 55, 65, 40, 40, 40, 40, 40, 40, 40, 40, 64, 80, 80, 80, 80, 80, 80, 80, 65, 40, 40, 40, 40, 40, 40, 40, 40, 64, 49, 49, 49, 49, 49, 49, 49, 49, 49, 57, 80, 80, 80, 80, 80, 80, 80, 57, 49, 49, 49, 49, 49, 49, 49, 49, 49, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80], + "data":[57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 40, 40, 40, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 50, 49, 49, 49, 49, 51, 40, 40, 40, 50, 49, 49, 49, 49, 51, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 50, 40, 40, 40, 40, 40, 40, 40, 51, 40, 40, 40, 40, 40, 40, 40, 40, 55, 65, 40, 40, 40, 40, 40, 40, 40, 40, 55, 80, 80, 80, 80, 80, 80, 80, 56, 40, 40, 40, 40, 40, 40, 40, 40, 64, 57, 48, 48, 47, 40, 40, 40, 40, 40, 55, 80, 80, 80, 80, 80, 80, 80, 56, 40, 40, 40, 40, 40, 46, 48, 48, 57, 56, 40, 40, 40, 40, 40, 40, 40, 40, 55, 80, 80, 80, 80, 80, 80, 80, 56, 40, 40, 40, 40, 40, 40, 40, 40, 55, 56, 40, 40, 40, 40, 40, 40, 40, 40, 55, 80, 80, 80, 80, 80, 80, 80, 56, 40, 40, 40, 40, 40, 40, 40, 40, 55, 65, 40, 40, 40, 40, 40, 40, 40, 40, 64, 80, 80, 80, 80, 80, 80, 80, 65, 40, 40, 40, 40, 40, 40, 40, 40, 64, 49, 49, 49, 49, 49, 49, 49, 49, 49, 57, 80, 80, 80, 80, 80, 80, 80, 57, 49, 49, 49, 49, 49, 49, 49, 49, 49, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57], "height":16, "name":"Tile Layer 1", "opacity":1, @@ -312,7 +312,7 @@ "animate":"1", "hitbox":"lowerHalfHitbox", "next":"78", - "physic":"1", + "speType":"water", "time":"0.1" }, "78": @@ -320,9 +320,13 @@ "animate":"1", "hitbox":"lowerHalfHitbox", "next":"77", - "physic":"1", + "speType":"water", "time":"0.1" }, + "79": + { + "speType":"water" + }, "8": { "animate":"1", diff --git a/Resources/Elements/Enemies/Enemy2.json b/Resources/Elements/Enemies/Enemy2.json @@ -9,7 +9,7 @@ "hitboxType": "special", "hitbox": "enemyHitbox", "level": 2, - "flying": false + "flying": true }, "Actions": { "breath": { diff --git a/Resources/Images/castle_1.png b/Resources/Images/castle_1.png Binary files differ. diff --git a/Sources/src/Characters.cpp b/Sources/src/Characters.cpp @@ -94,8 +94,13 @@ void Characters::_parseJson(std::string file) { if (!read.parse(file, json)) Log::error("Error in json syntax :\n" + read.getFormattedErrorMessages()); - if (json["infos"].get("name", "").asString() != this->_name) - Log::warning("The class name is different with the name in the config file."); + if (!strncmp("Enemies/", this->_name.c_str(), 8)) { + if (this->_name.substr(8, json["infos"].get("name", "").asString().length()) != json["infos"].get("name", "").asString()) + Log::error("The class name is different with the name in the config file."); + } else { + if (json["infos"].get("name", "").asString() != this->_name) + Log::warning("The class name is different with the name in the config file."); + } this->_name = json["infos"].get("name", "").asString(); this->_id = json["infos"].get("id", "").asInt(); this->_size = json["infos"].get("size", "").asFloat(); diff --git a/Sources/src/Elements.cpp b/Sources/src/Elements.cpp @@ -161,6 +161,10 @@ void Elements::display(void) { this->SetShapeType(PhysicsActor::SHAPETYPE_BOX); } + if (this->getAttribute("speType") == "water") { + this->SetIsSensor(true); + this->SetFixedRotation(true); + } if (this->getAttribute("physic") != "") { this->InitPhysics(); } diff --git a/Sources/src/Enemy.cpp b/Sources/src/Enemy.cpp @@ -40,22 +40,13 @@ Enemy::Enemy(void) : Characters("Enemy") { * @param str Name of the new Enemy */ Enemy::Enemy(std::string str) : Characters(str) { - this->setXStart(13); - this->setYStart(-19); this->SetName("Enemy"); theSwitchboard.SubscribeTo(this, "startPathing" + this->GetName()); theSwitchboard.DeferredBroadcast(new Message("startPathing" + this->GetName()), 0.2f); - if (str == "Enemy") { - this->setXStart(5); - this->setYStart(-19); - } else { - // ???? - } this->addAttribute("type", "Enemy"); this->addAttribute("name", str); this->addAttribute("enemy", "1"); this->_isDead = false; - this->display(); return ; } @@ -72,6 +63,7 @@ Enemy::~Enemy(void) { * Do the first animation call. */ void Enemy::init(void) { + this->display(); this->AnimCallback("base"); this->_orientation = RIGHT; } diff --git a/Sources/src/EnemyList.cpp b/Sources/src/EnemyList.cpp @@ -91,8 +91,6 @@ void EnemyList::EnemyData::_parseJson(std::string file) { if (!read.parse(file, json)) Log::error("Error in json syntax :\n" + read.getFormattedErrorMessages()); - if (json["infos"].get("name", "").asString() != this->_name) - Log::warning("The class name is different with the name in the config file."); this->_level = json["infos"].get("level", "").asInt(); this->_flying = json["infos"].get("flying", "").asBool(); } @@ -120,16 +118,18 @@ std::string EnemyList::getEnemyRandom(bool flying) { } } + if (!enemies.size()) + Log::error("Enemies list is empty"); int i = 0; int value = (rand() % enemies.size()); - for (it = this->_allEnemies.begin(); it != this->_allEnemies.end(); it++) { + for (it = enemies.begin(); it != enemies.end(); it++) { if (i == value) { return ((*it)->getName()); } i++; } - return ((*this->_allEnemies.begin())->getName()); + return ((*enemies.begin())->getName()); } diff --git a/Sources/src/Game.cpp b/Sources/src/Game.cpp @@ -85,7 +85,6 @@ void Game::start(void) { Game::rList = new RingList(); Game::currentGame = this; Hero *hero = new Hero(); - Enemy *bad = new Enemy(Game::eList->getEnemyRandom(false)); LevelGenerator *levelGenerator = new LevelGenerator(9, 5, 60); levelGenerator->execute(); @@ -115,7 +114,6 @@ void Game::start(void) { theCamera.SetPosition(this->maps->getMapXY()[Game::currentY][Game::currentX]->getXMid(), this->maps->getMapXY()[Game::currentY][Game::currentX]->getYMid() + 1.8, 9.2); hero->init(); - bad->init(); hero->equipWeapon(Game::wList->getWeapon("Sword")); hero->equipRing(Game::rList->getRing("SmallRing")); hero->equipArmor(Game::aList->getArmor("ChestArmor")); diff --git a/Sources/src/Hero.cpp b/Sources/src/Hero.cpp @@ -96,6 +96,7 @@ void Hero::actionCallback(std::string name, int status) { */ void Hero::BeginContact(Elements* elem, b2Contact *contact) { Characters::BeginContact(elem, contact); + if (elem->getAttribute("type") == "Enemy" && elem->isDead() == false) { if (this->_invincibility == false) this->_takeDamage(elem); @@ -129,6 +130,8 @@ void Hero::BeginContact(Elements* elem, b2Contact *contact) { else this->_enemiesTouched.push_back(elem); } + /* if (elem->getAttribute("speType") == "water")*/ + /*this->GetBody()->SetGravityScale(0.3);*/ } //! End collision function @@ -149,6 +152,8 @@ void Hero::EndContact(Elements *elem, b2Contact *contact) { elem->getAttribute("speType") == "spikes") { this->_enemiesTouched.remove(elem); } + /* if (elem->getAttribute("speType") == "water")*/ + /*this->GetBody()->SetGravityScale(1);*/ } //! Function called when the hero is taking damage diff --git a/Sources/src/Map.cpp b/Sources/src/Map.cpp @@ -87,6 +87,8 @@ void Map::display(void) { for (layers = this->_map.begin(); layers != this->_map.end(); layers++, v++) { x = this->_xStart; y = this->_yStart; + Enemy *tmp; + for (it = (*layers).begin(); it != (*layers).end(); it++, x++) { if ((x - this->_xStart) >= this->_width) { x = this->_xStart; @@ -155,6 +157,15 @@ void Map::display(void) { } } } + if (elem->getAttribute("spawnEnemy") != "") { + if (elem->getAttribute("isFlying") != "") + tmp = new Enemy(Game::eList->getEnemyRandom(true)); + else + tmp = new Enemy(Game::eList->getEnemyRandom(false)); + tmp->setXStart(x); + tmp->setYStart(y); + tmp->init(); + } elem->display(); } }