Skip to content

Commit

Permalink
episode 14 - the hud - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Limeoats committed Oct 7, 2015
1 parent 3aade4b commit 6a86723
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 2 deletions.
53 changes: 53 additions & 0 deletions notes/episode 14 - the hud - part 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=====================================================
Remaking Cavestory in C++
Episode 14 - The HUD - Part 1
By: Limeoats
Website: www.limeoats.com
Twitter: @Limeoats
Github: https://github.com/Limeoats/cavestory-development
Reddit: http://www.reddit.com/r/Limeoats
=====================================================


=====================================================
Problem
=====================================================
-We need to see certain information on the screen
-Health
-Gun EXP
-Gun Level
-In this episode, we'll be getting health on the screen

=====================================================
Details
=====================================================
TextBox.pbm
-Health bar sprite:
x: 0
y: 40
width: 64
height: 8
screen pos x: 35
screen pos y: 70
-Health number sprite:
x: 0
y: 56
width: 8
height: 8
screen pos x: 66
screen pos y: 70

=====================================================
Solution
=====================================================
-Add player health
-Create a bunch of Sprite objects in a new HUD class
-Special update/drawing logic
-Add it to the game class



=====================================================
Next time
=====================================================
-More HUD
3 changes: 3 additions & 0 deletions source/headers/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "player.h"
#include "level.h"
#include "hud.h"

class Graphics;

Expand All @@ -18,6 +19,8 @@ class Game {
Player _player;

Level _level;

HUD _hud;
};

#endif
23 changes: 23 additions & 0 deletions source/headers/hud.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef HUD_Hf
#define HUD_H

#include "sprite.h"
#include "player.h"

class Graphics;

class HUD {
public:
HUD();
HUD(Graphics &graphics, Player &player);
void update(int elapsedTime);
void draw(Graphics &graphics);

private:
Player _player;

Sprite _healthBarSprite;
Sprite _healthNumber1;
};

#endif
6 changes: 6 additions & 0 deletions source/headers/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class Player : public AnimatedSprite {
const float getX() const;
const float getY() const;

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

private:
float _dx, _dy;

Expand All @@ -73,6 +76,9 @@ class Player : public AnimatedSprite {

bool _lookingUp;
bool _lookingDown;

int _maxHealth;
int _currentHealth;
};

#endif
6 changes: 6 additions & 0 deletions source/headers/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class Sprite {
const Rectangle getBoundingBox() const;
const sides::Side getCollisionSide(Rectangle &other) const;

const inline float getX() const { return this->_x; }
const inline float getY() const { return this->_y; }

void setSourceRectX(int value);
void setSourceRectY(int value);

protected:
SDL_Rect _sourceRect;
SDL_Texture* _spriteSheet;
Expand Down
4 changes: 4 additions & 0 deletions source/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void Game::gameLoop() {

this->_level = Level("Map 1", Vector2(100, 100), graphics);
this->_player = Player(graphics, this->_level.getPlayerSpawnPoint());
this->_hud = HUD(graphics, this->_player);

int LAST_UPDATE_TIME = SDL_GetTicks();
//Start the game loop
Expand Down Expand Up @@ -95,12 +96,15 @@ void Game::draw(Graphics &graphics) {
this->_level.draw(graphics);
this->_player.draw(graphics);

this->_hud.draw(graphics);

graphics.flip();
}

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

//Check collisions
std::vector<Rectangle> others;
Expand Down
20 changes: 20 additions & 0 deletions source/src/hud.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "hud.h"

#include "graphics.h"

HUD::HUD() {}

HUD::HUD(Graphics &graphics, Player &player) {
this->_player = player;
this->_healthBarSprite = Sprite(graphics, "content/sprites/TextBox.png", 0, 40, 64, 8, 35, 70);
this->_healthNumber1 = Sprite(graphics, "content/sprites/TextBox.png", 0, 56, 8, 8, 66, 70);
}

void HUD::update(int elapsedTime) {
this->_healthNumber1.setSourceRectX(8 * this->_player.getCurrentHealth());
}

void HUD::draw(Graphics &graphics) {
this->_healthBarSprite.draw(graphics, this->_healthBarSprite.getX(), this->_healthBarSprite.getY());
this->_healthNumber1.draw(graphics, this->_healthNumber1.getX(), this->_healthNumber1.getY());
}
8 changes: 6 additions & 2 deletions source/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ Player::Player(Graphics &graphics, Vector2 spawnPoint) :
_dx(0),
_dy(0),
_facing(RIGHT),
_grounded(false)
_grounded(false),
_lookingUp(false),
_lookingDown(false),
_maxHealth(3),
_currentHealth(3)
{
graphics.loadImage("content/sprites/MyChar.png");

this->setupAnimations();
this->playAnimation("RunRight");
this->playAnimation("IdleRight");
}

void Player::setupAnimations() {
Expand Down
9 changes: 9 additions & 0 deletions source/src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ const sides::Side Sprite::getCollisionSide(Rectangle &other) const {
sides::NONE;

}


void Sprite::setSourceRectX(int value) {
this->_sourceRect.x = value;
}

void Sprite::setSourceRectY(int value) {
this->_sourceRect.y = value;
}

0 comments on commit 6a86723

Please sign in to comment.