diff --git a/data/sprites/humans.sprite b/data/sprites/humans.sprite index 0c62d79..ff1fe02 100644 --- a/data/sprites/humans.sprite +++ b/data/sprites/humans.sprite @@ -105,6 +105,54 @@ "frame_time" : 100, "originx" : 32, "originy" : 50 +}, + +"male_spellcast_N" : { + "texture" : "male_spellcast", + "topx" : 0, + "topy" : 0, + "height" : 64, + "width" : 64, + "number_of_frames" : 7, + "frame_time" : 100, + "originx" : 32, + "originy" : 50 +}, + +"male_spellcast_W" : { + "texture" : "male_spellcast", + "topx" : 0, + "topy" : 64, + "height" : 64, + "width" : 64, + "number_of_frames" : 7, + "frame_time" : 100, + "originx" : 32, + "originy" : 50 +}, + +"male_spellcast_S" : { + "texture" : "male_spellcast", + "topx" : 0, + "topy" : 128, + "height" : 64, + "width" : 64, + "number_of_frames" : 7, + "frame_time" : 100, + "originx" : 32, + "originy" : 50 +}, + +"male_spellcast_E" : { + "texture" : "male_spellcast", + "topx" : 0, + "topy" : 192, + "height" : 64, + "width" : 64, + "number_of_frames" : 7, + "frame_time" : 100, + "originx" : 32, + "originy" : 50 } -} \ No newline at end of file +} diff --git a/data/textures/male_spellcast.png b/data/textures/male_spellcast.png new file mode 100644 index 0000000..61f0548 Binary files /dev/null and b/data/textures/male_spellcast.png differ diff --git a/src/sago/SagoSprite.cpp b/src/sago/SagoSprite.cpp index 2ddcd14..a4070a7 100644 --- a/src/sago/SagoSprite.cpp +++ b/src/sago/SagoSprite.cpp @@ -95,6 +95,12 @@ void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, cons SDL_RenderCopy(target, data->tex.get(), &rect, &pos); } +void SagoSprite::DrawProgressive(SDL_Renderer* target, float progress, int x, int y) const { + Sint32 frameNumber = progress*data->aniFrames; + Sint32 frameTime = frameNumber*data->aniFrameTime; + Draw(target, frameTime, x, y); +} + void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const { SDL_Rect rect = data->imgCord; rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames); diff --git a/src/sago/SagoSprite.hpp b/src/sago/SagoSprite.hpp index 6a19c3f..c66fd87 100644 --- a/src/sago/SagoSprite.hpp +++ b/src/sago/SagoSprite.hpp @@ -58,7 +58,14 @@ public: * @param y Place to draw the sprite * @param bounds A recagular area that we must not draw outside. */ - void DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const; + void DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const;/** + * Draws the sprite to a given render window + * @param target The render window to draw on + * @param progress A float with value from 0.0f to 1.0f. Tells how far in the animation that we got + * @param x Place to draw the sprite + * @param y Place to draw the sprite + */ + void DrawProgressive(SDL_Renderer* target, float progress, int x, int y) const; void DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const; /** * Set a different origin. Normally it is the top left cornor. But in some cases you might want to center the origin or tranform it for other reasons diff --git a/src/saland.cpp b/src/saland.cpp index 1874459..1f67788 100644 --- a/src/saland.cpp +++ b/src/saland.cpp @@ -44,6 +44,8 @@ class TitleScreen : public sago::GameStateInterface { 0, 0, 255, 255); const sago::SagoSprite& s = globalData.spriteHolder->GetSprite("male_walkcycle_E"); s.Draw(globalData.screen, SDL_GetTicks(), 100, 100); + const sago::SagoSprite& sc = globalData.spriteHolder->GetSprite("male_spellcast_E"); + sc.Draw(globalData.screen, SDL_GetTicks(), 200, 100); } virtual void ProcessInput(const SDL_Event& event, bool &processed) override { diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index fcb91c9..f341761 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -65,16 +65,28 @@ bool Game::IsActive() { static void DrawHumanEntity(SDL_Renderer* target, sago::SagoSpriteHolder* sHolder, const Human *entity, float time, int offsetX, int offsetY, bool drawCollision) { std::string animation = "standing"; + bool relativeAnimation = false; + float relativeAnimationState = 0.0f; if (entity->moving) { animation = "walkcycle"; } + if (entity->castTimeRemaining) { + animation = "spellcast"; + relativeAnimation = true; + relativeAnimationState = 1.0f-(entity->castTimeRemaining/entity->castTime); + } /*if (drawCollision) { sf::CircleShape circle(entity->Radius); circle.setPosition(entity->X-offsetX-16, entity->Y-offsetY-16); target.draw(circle); }*/ const sago::SagoSprite &mySprite = sHolder->GetSprite(entity->race + "_"+animation+"_"+std::string(1,entity->direction)); - mySprite.Draw(target, time, std::round(entity->X)-offsetX, std::round(entity->Y)-offsetY); + if (relativeAnimation) { + mySprite.DrawProgressive(target, relativeAnimationState, std::round(entity->X)-offsetX, std::round(entity->Y)-offsetY); + } + else { + mySprite.Draw(target, time, std::round(entity->X)-offsetX, std::round(entity->Y)-offsetY); + } /*const sago::SagoSprite &myHair = sHolder->GetSprite(entity->race + "_"+animation+"_hair_1_"+string(1,entity->direction)); myHair.Draw(target, time, entity->X-offsetX, entity->Y-offsetY);*/ } @@ -97,6 +109,14 @@ void Game::Draw(SDL_Renderer* target) { } void Game::ProcessInput(const SDL_Event& event, bool& processed) { + if ( event.type == SDL_KEYDOWN ) { + if (event.key.keysym.sym == SDLK_SPACE) { + if (data->human->castTimeRemaining == 0) { + data->human->castTimeRemaining = data->human->castTime; + } + processed = true; + } + } } static bool MoveEntity( Placeable* entity, float destX, float destY ) { @@ -140,6 +160,18 @@ static void MoveHumanEntity (Creature *entity, float directionX, float direction MoveEntity (entity, entity->X + deltaX*(fDeltaTime/speed), entity->Y + deltaY*(fDeltaTime/speed)); } +static void UpdateHuman(Human *entity, float fDeltaTime) { + if (entity->castTimeRemaining > 0) { + entity->castTimeRemaining -= fDeltaTime; + } + if (entity->castTimeRemaining < 0) { + entity->castTimeRemaining = 0; + } + if (entity->castTimeRemaining == 0) { + MoveHumanEntity(entity, entity->moveX, entity->moveY, fDeltaTime); + } +} + void Game::Update() { Uint32 nowTime = SDL_GetTicks(); Uint32 deltaTime = nowTime - data->lastUpdate; @@ -158,7 +190,9 @@ void Game::Update() { if (state[SDL_SCANCODE_LEFT]) { deltaX -= 1.0f; } - MoveHumanEntity(data->human.get(), deltaX, deltaY, deltaTime); + data->human->moveX = deltaX; + data->human->moveY = deltaY; + UpdateHuman(data->human.get(), deltaTime); data->center_x = round(data->human->X); data->center_y = round(data->human->Y); data->lastUpdate = nowTime; diff --git a/src/saland/model/placeables.hpp b/src/saland/model/placeables.hpp index 3068991..5489e5e 100644 --- a/src/saland/model/placeables.hpp +++ b/src/saland/model/placeables.hpp @@ -29,6 +29,10 @@ public: class Human : public Creature { public: std::string race = "male"; + float castTimeRemaining = 0; //If non-zero then we are casting a spell + float castTime = 400; //Number of milliseconds it will take to complete the cast + float moveX = 0.0; + float moveY = 0.0; }; #endif /* PLACEABLES_HPP */