Grog-Knight

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

commit 8e410caf1c2c6467cc7c2b8eb6602fce03002c34
parent 8a0fcd9c53b270c71a415b6365bf9426539c4bba
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 18 Feb 2015 05:40:31 -0500

Add(Sprite Animation): Add a animation handling (WIP)

Diffstat:
MConfig/input_bindings.ini | 3+++
AResources/Images/Hero/hero_000.png | 0
AResources/Images/Hero/hero_001.png | 0
AResources/Images/Hero/hero_002.png | 0
AResources/Images/Hero/hero_003.png | 0
AResources/Images/Hero/hero_004.png | 0
AResources/Images/Hero/hero_005.png | 0
AResources/Images/Hero/hero_006.png | 0
AResources/Images/Hero/hero_007.png | 0
AResources/Images/Hero/hero_008.png | 0
AResources/Images/Hero/hero_009.png | 0
AResources/Images/Luffy.gif | 0
AResources/Images/kappa.png | 0
AResources/Images/sprite.png | 0
AResources/Images/test_000.png | 0
MSources/inc/Hero.hpp | 3+++
MSources/src/Elements.cpp | 3+++
MSources/src/Hero.cpp | 45+++++++++++++++++++++++++++++++++++++++++----
MSources/src/main.cpp | 1+
19 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/Config/input_bindings.ini b/Config/input_bindings.ini @@ -1 +1,4 @@ ; This is the key-bindings file. +D= +forwardPress +D= -forwardRealeased +SPACE= Jump diff --git a/Resources/Images/Hero/hero_000.png b/Resources/Images/Hero/hero_000.png Binary files differ. diff --git a/Resources/Images/Hero/hero_001.png b/Resources/Images/Hero/hero_001.png Binary files differ. diff --git a/Resources/Images/Hero/hero_002.png b/Resources/Images/Hero/hero_002.png Binary files differ. diff --git a/Resources/Images/Hero/hero_003.png b/Resources/Images/Hero/hero_003.png Binary files differ. diff --git a/Resources/Images/Hero/hero_004.png b/Resources/Images/Hero/hero_004.png Binary files differ. diff --git a/Resources/Images/Hero/hero_005.png b/Resources/Images/Hero/hero_005.png Binary files differ. diff --git a/Resources/Images/Hero/hero_006.png b/Resources/Images/Hero/hero_006.png Binary files differ. diff --git a/Resources/Images/Hero/hero_007.png b/Resources/Images/Hero/hero_007.png Binary files differ. diff --git a/Resources/Images/Hero/hero_008.png b/Resources/Images/Hero/hero_008.png Binary files differ. diff --git a/Resources/Images/Hero/hero_009.png b/Resources/Images/Hero/hero_009.png Binary files differ. diff --git a/Resources/Images/Luffy.gif b/Resources/Images/Luffy.gif Binary files differ. diff --git a/Resources/Images/kappa.png b/Resources/Images/kappa.png Binary files differ. diff --git a/Resources/Images/sprite.png b/Resources/Images/sprite.png Binary files differ. diff --git a/Resources/Images/test_000.png b/Resources/Images/test_000.png Binary files differ. diff --git a/Sources/inc/Hero.hpp b/Sources/inc/Hero.hpp @@ -34,6 +34,9 @@ class Hero : public Elements { ~Hero(); virtual void callback(Elements * elem); + virtual void AnimCallback(String name); + void ReceiveMessage(Message *m); + void init(); }; #endif diff --git a/Sources/src/Elements.cpp b/Sources/src/Elements.cpp @@ -93,6 +93,9 @@ void Elements::display(void) { this->Tag(this->getAttribute("type")); if (this->getAttribute("sprite") != "") this->SetSprite(this->getAttribute("sprite")); + else if (this->getAttribute("spritesFrame") != "") { + this->LoadSpriteFrames(this->getAttribute("spritesFrame")); + } else this->SetColor(0, 0, 0, 0); this->SetSize(1.0f); diff --git a/Sources/src/Hero.cpp b/Sources/src/Hero.cpp @@ -29,13 +29,18 @@ * Basic constructor */ Hero::Hero(void) { - this->addAttribute("sprite", "Resources/Images/hero.png"); + //this->addAttribute("sprite", "Resources/Images/test.png"); this->addAttribute("physic", "1"); this->addAttribute("type", "Hero"); this->SetDensity(1.0f); this->SetFriction(1.0f); this->SetRestitution(0.0f); - //this->SetFixedRotation(true); + this->SetFixedRotation(true); + this->addAttribute("spritesFrame", "Resources/Images/Hero/hero_000.png"); + //theSwitchboard.SubscribeTo(this, "callbackEndRun"); + theSwitchboard.SubscribeTo(this, "forwardPress"); + theSwitchboard.SubscribeTo(this, "forwardRealeased"); + theSwitchboard.SubscribeTo(this, "Jump"); } /** @@ -51,9 +56,41 @@ Hero::~Hero(void) { */ void Hero::callback(Elements * elem) { if (elem->getAttribute("type") == "ground") { - std::cout << "Hero got a collision with " << elem->getAttribute("type") << ", " << elem->getId() << ", << " << this->getId() << std::endl; + //this->PlaySpriteAnimation(0.5f, SAT_OneShot, 0, 2, "laul"); + } +} + +/** + * Callback for end of animation + * @param: name (String) + */ +void Hero::AnimCallback(String name) { + std::cout << name << std::endl; + if (name == "base") { + this->PlaySpriteAnimation(0.2f, SAT_Loop, 0, 3, "base"); + } else if (name == "run") { + this->PlaySpriteAnimation(0.1f, SAT_OneShot, 4, 9, "run"); + } +} + +/** + * Receive broadcasts message + * @param: (Message *) + */ +void Hero::ReceiveMessage(Message *m) { + std::cout << m->GetMessageName() << std::endl; + if (m->GetMessageName() == "forwardPress") { this->ApplyLinearImpulse(Vector2(4, 0), Vector2(1, 1)); - } else { + if (this->GetSpriteFrame() <= 3) + this->PlaySpriteAnimation(0.1f, SAT_Loop, 4, 9, "run"); + } else if (m->GetMessageName() == "forwardRealeased") { this->GetBody()->SetLinearVelocity(b2Vec2(0, 0)); + this->PlaySpriteAnimation(0.1f, SAT_OneShot, 0, 3, "base"); + } else if (m->GetMessageName() == "Jump") { + this->ApplyLinearImpulse(Vector2(0, 6), Vector2(1, 1)); } } + +void Hero::init(void) { + this->PlaySpriteAnimation(0.2f, SAT_OneShot, 0, 3, "base"); +} diff --git a/Sources/src/main.cpp b/Sources/src/main.cpp @@ -49,6 +49,7 @@ int main(int ac, char **av) { Hero *hero = new Hero(); theCamera.LockTo(hero); game->displayHero(*(hero)); + hero->init(); //theWorld.SetSideBlockers(true, 0.7f); Game::listElement();