Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UI::CombatPanel widget #640

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Under development
- [feature] New video settings: window position(x,y) and always_on_top flag (adamkewley)
- [feature] Added missing logo in the Pipboy window and special date greetings (JanSimek)
- [feature] Added ladders and stairs funtionality (667bdrm)
- [feature] Added combat panel widget (667bdrm)
- [feature] Added elevators support (667bdrm)

0.3.1 (2018-01-14)
Expand Down
132 changes: 132 additions & 0 deletions src/UI/CombatPanel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include "../UI/CombatPanel.h"
#include "../UI/Factory/ImageButtonFactory.h"
#include "../UI/ImageButton.h"
#include "../UI/ResourceManager.h"
#include "../UI/Base.h"
#include "../UI/Animation.h"
#include "../Event/Event.h"
#include "../Event/Mouse.h"
#include "../Event/Keyboard.h"
#include "../Input/Mouse.h"
#include "../Game/Game.h"
#include "../Graphics/Renderer.h"
#include "../Audio/Mixer.h"

namespace Falltergeist
{
using namespace std;
using ImageButtonType = UI::Factory::ImageButtonFactory::Type;

namespace UI
{
CombatPanel::CombatPanel(bool enabled, const Point& pos) : Base(pos), _enabled(false)
{
_playerTurnSprite = make_shared<Graphics::Sprite>("art/intrface/endltgrn.frm");
_enemyTurnSprite = make_shared<Graphics::Sprite>("art/intrface/endltred.frm");

resourceManager = std::make_shared<UI::ResourceManager>();
imageButtonFactory = std::make_unique<UI::Factory::ImageButtonFactory>(resourceManager);

_panelAnimation = std::shared_ptr<Animation>(new Animation("art/intrface/endanim.frm", 0));
_panelAnimation->setPosition(position());
_panelAnimation->animationEndedHandler().add([this](Event::Event* event){
Game::Game::getInstance()->mixer()->playACMSound("sound/sfx/icombat2.acm");
_btnEndTurn->setEnabled(true);
_btnEndCombat->setEnabled(true);
});

_btnEndTurn = std::shared_ptr<ImageButton>(imageButtonFactory->getByType(ImageButtonType::PANEL_END_TURN, position() + Point(11, 6)));
_btnEndTurn->setEnabled(false);


_btnEndCombat = std::shared_ptr<ImageButton>(imageButtonFactory->getByType(ImageButtonType::PANEL_END_COMBAT, position() + Point(11, 28)));
_btnEndCombat->setEnabled(false);
}

CombatPanel::CombatPanel(const Point& pos) : CombatPanel(false, pos)
{
}

void CombatPanel::handle(Event::Event *event)
{

UI::Base::handle(event);
if (auto mouseEvent = dynamic_cast<Event::Mouse*>(event))
{
mouseEvent->setObstacle(false);
mouseEvent->setHandled(false);

if (event->handled()) {
return;
}

if (activated()) {
_btnEndTurn->handle(dynamic_cast<Event::Mouse*>(event));
_btnEndCombat->handle(dynamic_cast<Event::Mouse*>(event));
}
}

}


bool CombatPanel::activated()
{
return _enabled;
}

void CombatPanel::setActivated(bool activated)
{
if (_enabled == activated) return;
_enabled = activated;
_panelAnimation->setReverse(!activated);
_panelAnimation->play();
if (activated == false) {
_btnEndTurn->setEnabled(false);
_btnEndCombat->setEnabled(false);
}
Game::Game::getInstance()->mixer()->playACMSound("sound/sfx/iciboxx1.acm");
}

void CombatPanel::think(const float &deltaTime)
{
UI::Base::think(deltaTime);
_panelAnimation->think(deltaTime);
_btnEndTurn->think(deltaTime);
_btnEndCombat->think(deltaTime);
}

CombatPanel::CombatMode CombatPanel::mode() const
{
return _mode;
}

void CombatPanel::setMode(CombatPanel::CombatMode mode)
{
_mode = mode;
}

bool CombatPanel::opaque(const Point &pos)
{
if (pos.x() > _size.width() || pos.x()<0 || pos.y() > _size.height() || pos.y()<0) {
return false;
}
return _panelAnimation->opaque(pos);
}

void CombatPanel::render(bool eggTransparency)
{
_panelAnimation->render();

if (activated() && _panelAnimation->currentFrame() == 4) {

if (_mode == CombatMode::ENEMY_TURN) {
_enemyTurnSprite->render(position().x(), position().y(), true, false, 0, 0);
} else if (_mode == CombatMode::PLAYER_TURN) {
_playerTurnSprite->render(position().x(), position().y(), true, false, 0, 0);
}
_btnEndTurn->render(eggTransparency);
_btnEndCombat->render(eggTransparency);
}
}
}
}
63 changes: 63 additions & 0 deletions src/UI/CombatPanel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include "../Graphics/Sprite.h"
#include "../UI/Base.h"
#include "../UI/Animation.h"
#include "../UI/ImageButton.h"
#include "../UI/IResourceManager.h"

namespace Falltergeist
{
namespace Event
{
class Keyboard;
class Mouse;
}
namespace UI
{


namespace Factory
{
class ImageButtonFactory;
}

class Image;

class CombatPanel : public UI::Base
{
public:

enum CombatMode
{
ENEMY_TURN = 0,
PLAYER_TURN,
};

CombatPanel(const Point& pos = Point(0, 0));
CombatPanel(bool enabled, const Point& pos = Point(0, 0));

CombatPanel::CombatMode mode() const;
void setMode(CombatPanel::CombatMode mode);
bool activated();
void setActivated(bool activated);
void handle(Event::Event *event) override;
void think(const float &deltaTime) override;
bool opaque(const Point &pos) override;
void render(bool eggTransparency) override;
std::shared_ptr<UI::ImageButton> _btnEndTurn;
std::shared_ptr<UI::ImageButton> _btnEndCombat;

protected:
CombatPanel::CombatMode _mode;
Size _size;
bool _enabled;
std::shared_ptr<Graphics::Sprite> _playerTurnSprite;
std::shared_ptr<Graphics::Sprite> _enemyTurnSprite;
std::vector<SDL_Rect> _rects;
std::shared_ptr<UI::IResourceManager> resourceManager;
std::unique_ptr<UI::Factory::ImageButtonFactory> imageButtonFactory;
std::shared_ptr<UI::Animation> _panelAnimation;
};
}
}
8 changes: 8 additions & 0 deletions src/UI/Factory/ImageButtonFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace Falltergeist
{Type::PANEL_MAP, "art/intrface/mapup.frm"},
{Type::PANEL_CHA, "art/intrface/chaup.frm"},
{Type::PANEL_PIP, "art/intrface/pipup.frm"},
{Type::PANEL_END_TURN, "art/intrface/endturnu.frm"},
{Type::PANEL_END_COMBAT, "art/intrface/endcmbtu.frm"},
{Type::OPTIONS_BUTTON, "art/intrface/opbtnoff.frm"},
{Type::SKILLDEX_BUTTON, "art/intrface/skldxoff.frm"},
{Type::INVENTORY_UP_ARROW, "art/intrface/invupout.frm"},
Expand Down Expand Up @@ -71,6 +73,8 @@ namespace Falltergeist
{Type::PANEL_MAP, "art/intrface/mapdn.frm"},
{Type::PANEL_CHA, "art/intrface/chadn.frm"},
{Type::PANEL_PIP, "art/intrface/pipdn.frm"},
{Type::PANEL_END_TURN, "art/intrface/endturnd.frm"},
{Type::PANEL_END_COMBAT, "art/intrface/endcmbtd.frm"},
{Type::OPTIONS_BUTTON, "art/intrface/opbtnon.frm"},
{Type::SKILLDEX_BUTTON, "art/intrface/skldxon.frm"},
{Type::INVENTORY_UP_ARROW, "art/intrface/invupin.frm"},
Expand Down Expand Up @@ -102,6 +106,8 @@ namespace Falltergeist
{Type::PANEL_MAP, "sound/sfx/ib2p1xx1.acm"},
{Type::PANEL_CHA, "sound/sfx/ib2p1xx1.acm"},
{Type::PANEL_PIP, "sound/sfx/ib2p1xx1.acm"},
{Type::PANEL_END_TURN, "sound/sfx/ib2p1xx1.acm"},
{Type::PANEL_END_COMBAT, "sound/sfx/ib2p1xx1.acm"},
{Type::OPTIONS_BUTTON, "sound/sfx/ib3p1xx1.acm"},
{Type::SKILLDEX_BUTTON, "sound/sfx/ib2lu1x1.acm"},
{Type::INVENTORY_UP_ARROW, "sound/sfx/ib1p1xx1.acm"},
Expand All @@ -126,6 +132,8 @@ namespace Falltergeist
{Type::PANEL_MAP, "sound/sfx/ib2lu1x1.acm"},
{Type::PANEL_CHA, "sound/sfx/ib2lu1x1.acm"},
{Type::PANEL_PIP, "sound/sfx/ib2lu1x1.acm"},
{Type::PANEL_END_TURN, "sound/sfx/ib2lu1x1.acm"},
{Type::PANEL_END_COMBAT, "sound/sfx/ib2lu1x1.acm"},
{Type::OPTIONS_BUTTON, "sound/sfx/ib3lu1x1.acm"},
{Type::SKILLDEX_BUTTON, "sound/sfx/ib1p1xx1.acm"},
{Type::INVENTORY_UP_ARROW, "sound/sfx/ib1lu1x1.acm"},
Expand Down
2 changes: 2 additions & 0 deletions src/UI/Factory/ImageButtonFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace Falltergeist
SMALL_UP_ARROW,
SMALL_DOWN_ARROW,
MAP_HOTSPOT,
PANEL_END_TURN,
PANEL_END_COMBAT,
ELEVATOR_CIRCLE
};

Expand Down
43 changes: 42 additions & 1 deletion src/UI/PlayerPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../State/Skilldex.h"
#include "../State/WorldMap.h"
#include "../UI/Animation.h"
#include "../UI/CombatPanel.h"
#include "../UI/Factory/ImageButtonFactory.h"
#include "../UI/ImageButton.h"
#include "../UI/PlayerPanel.h"
Expand Down Expand Up @@ -86,8 +87,11 @@ namespace Falltergeist
_ui.back()->mouseDownHandler().add([this](Event::Event* event){
if(auto mouse = dynamic_cast<Event::Mouse*>(event))
{
if(mouse->leftButton())
if(mouse->leftButton()) {
_isAttackBtnPressed = true;
if (!_combatPanel->activated())
_combatPanel->setActivated(true);
}
}
});

Expand Down Expand Up @@ -132,6 +136,22 @@ namespace Falltergeist
this->openPipBoy();
});

// Combat panel
_combatPanel = std::make_shared<CombatPanel>(position() + Point(580, 38));
_combatPanel->setMode(CombatPanel::CombatMode::PLAYER_TURN);

auto _btnEndTurn = std::shared_ptr<UI::ImageButton>(_combatPanel->_btnEndTurn);
_btnEndTurn->mouseClickHandler().add([this](Event::Event* event){
endCombatTurn();
});

auto _btnEndCombat = std::shared_ptr<UI::ImageButton>(_combatPanel->_btnEndCombat);
_btnEndCombat->mouseClickHandler().add([this](Event::Event* event){
_combatPanel->setActivated(false);
});

_ui.push_back(_combatPanel);

// Message log
_messageLog = std::make_shared<UI::TextArea>(position() + Point(23, 24));
_messageLog->setSize({165, 60});
Expand Down Expand Up @@ -346,12 +366,22 @@ namespace Falltergeist
playWindowOpenSfx();
}

void PlayerPanel::endCombatTurn()
{
// @TODO: implement combat sequence
if (_combatPanel->activated()) {
Game::Game::getInstance()->mixer()->playACMSound("sound/sfx/icombat1.acm");
}
}

void PlayerPanel::onKeyDown(Event::Keyboard* event)
{
switch (event->keyCode())
{
case SDLK_a:
// @TODO: initiateCombat();
if (!_combatPanel->activated())
_combatPanel->setActivated(true);
break;
case SDLK_c:
openCharacterScreen();
Expand Down Expand Up @@ -405,9 +435,20 @@ namespace Falltergeist
Game::Game::getInstance()->pushState(new State::ExitConfirm(resourceManager, logger));
playWindowOpenSfx();
}
break;
case SDLK_RETURN:
// @TODO: endCombat();
if (_combatPanel->activated()) {
_combatPanel->setActivated(false);
}
break;
case SDLK_SLASH:
// @TODO: printCurrentTime();
break;
case SDLK_SPACE:
// @TODO: endCombatTurn();
endCombatTurn();
break;
case SDLK_TAB:
openMap();
break;
Expand Down
3 changes: 3 additions & 0 deletions src/UI/PlayerPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../UI/Base.h"
#include "../ILogger.h"
#include "../UI/ImageButton.h"
#include "../UI/CombatPanel.h"

namespace Falltergeist
{
Expand Down Expand Up @@ -48,6 +49,7 @@ namespace Falltergeist
std::shared_ptr<Image> _background;
std::shared_ptr<SmallCounter> _hitPoints;
std::shared_ptr<SmallCounter> _armorClass;
std::shared_ptr<CombatPanel> _combatPanel;
std::shared_ptr<TextArea> _messageLog;
std::vector<std::shared_ptr<UI::Base>> _ui;

Expand All @@ -68,6 +70,7 @@ namespace Falltergeist
void openSkilldex();
void openSaveGame();
void openLoadGame();
void endCombatTurn();

void onKeyDown(Event::Keyboard* event);
};
Expand Down