From d997e6743dfa0df245bc3f59ee97ecd63efb3c4f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 6 Aug 2012 13:44:55 +0200 Subject: [PATCH] added Hero class for battle state --- Debug/src/battle/subdir.mk | 3 ++ Release/src/battle/subdir.mk | 3 ++ src/battle/BattleState.cpp | 11 ++++++ src/battle/BattleState.h | 3 ++ src/battle/Hero.cpp | 22 ++++++++++++ src/battle/Hero.h | 68 ++++++++++++++++++++++++++++++++++++ src/main.cpp | 24 ++++++++----- 7 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 src/battle/Hero.cpp create mode 100644 src/battle/Hero.h diff --git a/Debug/src/battle/subdir.mk b/Debug/src/battle/subdir.mk index 8eb479b..52e0e02 100644 --- a/Debug/src/battle/subdir.mk +++ b/Debug/src/battle/subdir.mk @@ -5,16 +5,19 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ ../src/battle/BattleState.cpp \ +../src/battle/Hero.cpp \ ../src/battle/Monster.cpp \ ../src/battle/PartyLayout.cpp OBJS += \ ./src/battle/BattleState.o \ +./src/battle/Hero.o \ ./src/battle/Monster.o \ ./src/battle/PartyLayout.o CPP_DEPS += \ ./src/battle/BattleState.d \ +./src/battle/Hero.d \ ./src/battle/Monster.d \ ./src/battle/PartyLayout.d diff --git a/Release/src/battle/subdir.mk b/Release/src/battle/subdir.mk index 123e7f8..6cff662 100644 --- a/Release/src/battle/subdir.mk +++ b/Release/src/battle/subdir.mk @@ -5,16 +5,19 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ ../src/battle/BattleState.cpp \ +../src/battle/Hero.cpp \ ../src/battle/Monster.cpp \ ../src/battle/PartyLayout.cpp OBJS += \ ./src/battle/BattleState.o \ +./src/battle/Hero.o \ ./src/battle/Monster.o \ ./src/battle/PartyLayout.o CPP_DEPS += \ ./src/battle/BattleState.d \ +./src/battle/Hero.d \ ./src/battle/Monster.d \ ./src/battle/PartyLayout.d diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index d1a983f..511d29e 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -26,6 +26,13 @@ void BattleState::AddMonster(const Monster &m) { monsters.push_back(m); } +void BattleState::AddHero(const Hero &h) { + if (heroes.size() >= heroesLayout->NumPositions()) { + throw std::overflow_error("too many heroes for layout"); + } + heroes.push_back(h); +} + void BattleState::Resize(int w, int h) { @@ -65,6 +72,10 @@ void BattleState::Render(SDL_Surface *screen) { for (vector::size_type i(0), end(monsters.size()); i < end; ++i) { monsters[i].Sprite()->DrawCenterBottom(screen, Point(monsterPositions[i].X() + destRect.x, monsterPositions[i].Y() + destRect.y)); } + + for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { + heroes[i].Sprite()->DrawCenterBottom(screen, Point(heroesPositions[i].X() + destRect.x, heroesPositions[i].Y() + destRect.y)); + } } } diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 06b8fbd..205397a 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -8,6 +8,7 @@ #ifndef BATTLE_BATTLESTATE_H_ #define BATTLE_BATTLESTATE_H_ +#include "Hero.h" #include "Monster.h" #include "../app/State.h" #include "../geometry/Point.h" @@ -30,6 +31,7 @@ public: public: void AddMonster(const Monster &); + void AddHero(const Hero &); public: virtual void EnterState(app::Application &ctrl, SDL_Surface *screen); @@ -48,6 +50,7 @@ private: std::vector > monsterPositions; std::vector > heroesPositions; std::vector monsters; + std::vector heroes; }; diff --git a/src/battle/Hero.cpp b/src/battle/Hero.cpp new file mode 100644 index 0000000..faaf841 --- /dev/null +++ b/src/battle/Hero.cpp @@ -0,0 +1,22 @@ +/* + * Hero.cpp + * + * Created on: Aug 6, 2012 + * Author: holy + */ + +#include "Hero.h" + +namespace battle { + +Hero::Hero() { + // TODO Auto-generated constructor stub + +} + +Hero::~Hero() { + // TODO Auto-generated destructor stub + +} + +} diff --git a/src/battle/Hero.h b/src/battle/Hero.h new file mode 100644 index 0000000..ec87f49 --- /dev/null +++ b/src/battle/Hero.h @@ -0,0 +1,68 @@ +/* + * Hero.h + * + * Created on: Aug 6, 2012 + * Author: holy + */ + +#ifndef BATTLE_HERO_H_ +#define BATTLE_HERO_H_ + +#include + +namespace graphics { class Sprite; } + +namespace battle { + +class Hero { + +public: + Hero(); + ~Hero(); + +public: + const char *Name() const { return name; } + Uint8 Level() const { return level; } + const graphics::Sprite *Sprite() const { return sprite; } + + Uint16 MaxHealth() const { return maxHealth; } + Uint16 Health() const { return health; } + int RelativeHealth(int max) { return health * max / maxHealth; } + + Uint16 MaxMana() const { return maxMana; } + Uint16 Mana() const { return mana; } + int RelativeMana(int max) { return mana * max / maxMana; } + + Uint16 Attack() const { return attack; } + Uint16 Defense() const { return defense; } + Uint16 Agility() const { return agility; } + Uint16 Intelligence() const { return intelligence; } + Uint16 Gut() const { return gut; } + Uint16 MagicResistance() const { return magicResistance; } + +// temporary setters until loader is implemented +public: + void SetSprite(graphics::Sprite *s) { sprite = s; } + +private: + const char *name; + graphics::Sprite *sprite; + // TODO: equipment and spells lists + + Uint16 maxHealth, health; + Uint16 maxMana, mana; + + Uint16 attack; + Uint16 defense; + Uint16 agility; + Uint16 intelligence; + Uint16 gut; + Uint16 magicResistance; + + Uint8 level; + +}; + +} + +#endif /* BATTLE_HERO_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 2ecdbb6..3ec9945 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "app/Application.h" #include "battle/BattleState.h" +#include "battle/Hero.h" #include "battle/Monster.h" #include "battle/PartyLayout.h" #include "geometry/Point.h" @@ -19,6 +20,7 @@ using app::Application; using battle::BattleState; +using battle::Hero; using battle::Monster; using battle::PartyLayout; using geometry::Point; @@ -50,15 +52,17 @@ int main(int argc, char **argv) { monstersLayout.AddPosition(Point(150, 100)); monstersLayout.AddPosition(Point(200, 100)); PartyLayout heroesLayout; - heroesLayout.AddPosition(Point(45, 132)); - heroesLayout.AddPosition(Point(174, 136)); - heroesLayout.AddPosition(Point(110, 143)); - heroesLayout.AddPosition(Point(239, 148)); - SDL_Surface *white100(SDL_CreateRGBSurface(0, 100, 100, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF)); - SDL_FillRect(white100, 0, SDL_MapRGB(bg->format, 0xFF, 0xFF, 0xFF)); - Sprite dummyMonsterSprite(white100, 100, 100); + heroesLayout.AddPosition(Point(27, 219)); + heroesLayout.AddPosition(Point(104, 227)); + heroesLayout.AddPosition(Point(66, 238)); + heroesLayout.AddPosition(Point(143, 246)); + SDL_Surface *white96(SDL_CreateRGBSurface(0, 96, 96, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF)); + SDL_FillRect(white96, 0, SDL_MapRGB(bg->format, 0xFF, 0xFF, 0xFF)); + Sprite dummySprite(white96, 96, 96); Monster monster; - monster.SetSprite(&dummyMonsterSprite); + monster.SetSprite(&dummySprite); + Hero hero; + hero.SetSprite(&dummySprite); try { InitSDL sdl; @@ -69,6 +73,10 @@ int main(int argc, char **argv) { battleState->AddMonster(monster); battleState->AddMonster(monster); battleState->AddMonster(monster); + battleState->AddHero(hero); + battleState->AddHero(hero); + battleState->AddHero(hero); + battleState->AddHero(hero); Application app(&screen, battleState); app.Run(); -- 2.39.2