Skip to content

Commit

Permalink
episode 17 - enemies
Browse files Browse the repository at this point in the history
  • Loading branch information
Limeoats committed Oct 14, 2015
1 parent 8da5d8c commit 96a9e44
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 4 deletions.
19 changes: 18 additions & 1 deletion maps/Map 2.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="20" height="16" tilewidth="16" tileheight="16" nextobjectid="44">
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="20" height="16" tilewidth="16" tileheight="16" nextobjectid="49">
<tileset firstgid="1" name="PrtCave" tilewidth="16" tileheight="16">
<image source="../content/tilesets/PrtCave.png" width="256" height="80"/>
</tileset>
Expand Down Expand Up @@ -713,4 +713,21 @@
<polyline points="0,0 -123.399,59.2051"/>
</object>
</objectgroup>
<objectgroup color="#4100ff" name="enemies">
<object id="44" name="bat" x="115.498" y="135.057" width="3.96448" height="3.96448">
<ellipse/>
</object>
<object id="45" name="bat" x="193.995" y="48.1023" width="3.17158" height="3.70018">
<ellipse/>
</object>
<object id="46" name="bat" x="291.521" y="32.2444" width="3.70018" height="3.43588">
<ellipse/>
</object>
<object id="47" name="bat" x="202.981" y="199.281" width="3.70018" height="3.96448">
<ellipse/>
</object>
<object id="48" name="bat" x="214.61" y="84.3112" width="3.43588" height="3.43588">
<ellipse/>
</object>
</objectgroup>
</map>
45 changes: 45 additions & 0 deletions notes/episode 17 - enemies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=====================================================
Remaking Cavestory in C++
Episode 17 - Enemies
By: Limeoats
Website: www.limeoats.com
Twitter: @Limeoats
Github: https://github.com/Limeoats/cavestory-development
Reddit: http://www.reddit.com/r/Limeoats
=====================================================


=====================================================
Problem
=====================================================
-We don't have any monsters in our game yet :(



=====================================================
Details
=====================================================
-We'll be using the Bat in NpcCemet

3, 2, 32, "FlyLeft", 16, 16
3, 2, 48, "FlyRight", 16, 16


=====================================================
Solution
=====================================================
-Get NpcCemet.pbm, make background transparent, make it a png and put it in sprites folder
-Create Enemy base class
-Create Bat class extending off of Enemy
-Add bat spawn points to Tiled
-Parse enemy info out of tmx file and into an Enemy vector
-Update and draw from the level class


=====================================================
Next time
=====================================================
-Move the bat
-Collision with the bat (and damage to the player)
-Damage numbers? (maybe two episodes from now)

43 changes: 43 additions & 0 deletions source/headers/enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef ENEMY_H
#define ENEMY_H

#include "animatedsprite.h"
#include "globals.h"
#include "player.h"

#include <string>

class Graphics;

class Enemy : public AnimatedSprite {
public:
Enemy();
Enemy(Graphics &graphics, std::string filePath, int sourceX, int sourceY,
int width, int height, Vector2 spawnPoint, int timeToUpdate);
virtual void update(int elapsedTime, Player &player);
virtual void draw(Graphics &graphics);

const inline int getMaxHealth() const { return this->_maxHealth; }
const inline int getCurrentHealth() const { return this->_currentHealth; }
protected:
Direction _direction;

int _maxHealth;
int _currentHealth;
};


class Bat : public Enemy {
public:
Bat();
Bat(Graphics &graphics, Vector2 spawnPoint);
void update(int elapsedTime, Player &player);
void draw(Graphics &graphics);

void animationDone(std::string currentAnimation);
void setupAnimations();
private:
};


#endif
6 changes: 5 additions & 1 deletion source/headers/level.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "door.h"

class Graphics;
class Enemy;
class Player;
struct SDL_Texture;
struct SDL_Rect;
struct Tileset;
Expand All @@ -21,7 +23,7 @@ class Level {
Level();
Level(std::string mapName, Graphics &graphics);
~Level();
void update(int elapsedTime);
void update(int elapsedTime, Player &player);
void draw(Graphics &graphics);

std::vector<Rectangle> checkTileCollisions(const Rectangle &other);
Expand Down Expand Up @@ -49,6 +51,8 @@ class Level {

std::vector<Door> _doorList;

std::vector<Enemy*> _enemies;

/* void loadMap
* Loads a map
*/
Expand Down
51 changes: 51 additions & 0 deletions source/src/enemy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "enemy.h"

//Base enemy class
Enemy::Enemy() {}

Enemy::Enemy(Graphics &graphics, std::string filePath, int sourceX, int sourceY, int width, int height,
Vector2 spawnPoint, int timeToUpdate) :
AnimatedSprite(graphics, filePath, sourceX, sourceY, width, height,
spawnPoint.x, spawnPoint.y, timeToUpdate),
_direction(LEFT),
_maxHealth(0),
_currentHealth(0)
{}

void Enemy::update(int elapsedTime, Player &player) {
AnimatedSprite::update(elapsedTime);
}

void Enemy::draw(Graphics &graphics) {
AnimatedSprite::draw(graphics, this->_x, this->_y);
}


//Bat class
Bat::Bat() {}

Bat::Bat(Graphics &graphics, Vector2 spawnPoint) :
Enemy(graphics, "content/sprites/NpcCemet.png", 32, 32, 16, 16, spawnPoint, 140)
{
this->setupAnimations();
this->playAnimation("FlyLeft");
}

void Bat::update(int elapsedTime, Player &player) {
this->_direction = player.getX() > this->_x ? RIGHT : LEFT;
this->playAnimation(this->_direction == RIGHT ? "FlyRight" : "FlyLeft");
Enemy::update(elapsedTime, player);
}

void Bat::draw(Graphics &graphics) {
Enemy::draw(graphics);
}

void Bat::animationDone(std::string currentAnimation) {

}

void Bat::setupAnimations() {
this->addAnimation(3, 2, 32, "FlyLeft", 16, 16, Vector2(0,0));
this->addAnimation(3, 2, 48, "FlyRight", 16, 16, Vector2(0,0));
}
2 changes: 1 addition & 1 deletion source/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void Game::draw(Graphics &graphics) {

void Game::update(float elapsedTime) {
this->_player.update(elapsedTime);
this->_level.update(elapsedTime);
this->_level.update(elapsedTime, this->_player);
this->_hud.update(elapsedTime);

//Check collisions
Expand Down
30 changes: 29 additions & 1 deletion source/src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "graphics.h"
#include "globals.h"
#include "utils.h"
#include "player.h"
#include "enemy.h"

#include "tinyxml2.h"

Expand Down Expand Up @@ -311,15 +313,38 @@ void Level::loadMap(std::string mapName, Graphics &graphics) {
}
}

else if (ss.str() == "enemies") {
float x, y;
XMLElement* pObject = pObjectGroup->FirstChildElement("object");
if (pObject != NULL) {
while (pObject) {
x = pObject->FloatAttribute("x");
y = pObject->FloatAttribute("y");
const char* name = pObject->Attribute("name");
std::stringstream ss;
ss << name;
if (ss.str() == "bat") {
this->_enemies.push_back(new Bat(graphics, Vector2(std::floor(x) * globals::SPRITE_SCALE,
std::floor(y) * globals::SPRITE_SCALE)));
}

pObject = pObject->NextSiblingElement("object");
}
}
}

pObjectGroup = pObjectGroup->NextSiblingElement("objectgroup");
}
}
}

void Level::update(int elapsedTime) {
void Level::update(int elapsedTime, Player &player) {
for (int i = 0; i < this->_animatedTileList.size(); i++) {
this->_animatedTileList.at(i).update(elapsedTime);
}
for (int i = 0; i < this->_enemies.size(); i++) {
this->_enemies.at(i)->update(elapsedTime, player);
}
}

void Level::draw(Graphics &graphics) {
Expand All @@ -329,6 +354,9 @@ void Level::draw(Graphics &graphics) {
for (int i = 0; i < this->_animatedTileList.size(); i++) {
this->_animatedTileList.at(i).draw(graphics);
}
for (int i = 0; i < this->_enemies.size(); i++) {
this->_enemies.at(i)->draw(graphics);
}
}

std::vector<Rectangle> Level::checkTileCollisions(const Rectangle &other) {
Expand Down

0 comments on commit 96a9e44

Please sign in to comment.