add a wiggle animation for ships and planes to have sub-tile movement

This commit is contained in:
Tim Felgentreff 2022-04-02 17:56:13 +02:00
parent 4ac36b567e
commit 58c6c5967a
7 changed files with 162 additions and 1 deletions

View file

@ -125,6 +125,7 @@ set(animation_SRCS
src/animation/animation_spawnunit.cpp
src/animation/animation_unbreakable.cpp
src/animation/animation_wait.cpp
src/animation/animation_wiggle.cpp
)
source_group(animation FILES ${animation_SRCS})
@ -507,6 +508,7 @@ set(stratagus_animation_HDRS
src/include/animation/animation_spawnunit.h
src/include/animation/animation_unbreakable.h
src/include/animation/animation_wait.h
src/include/animation/animation_wiggle.h
)
set(stratagus_spell_HDRS

View file

@ -431,6 +431,11 @@ bool COrder_Build::BuildFromOutside(CUnit &unit) const
return this->BuildingUnit->CurrentAction() != UnitActionBuilt;
}
CUnit *COrder_Build::GetBuildingUnit() const
{
return this->BuildingUnit;
}
/* virtual */ void COrder_Build::UpdateUnitVariables(CUnit &unit) const
{
if (this->State == State_BuildFromOutside && this->BuildingUnit != NULL) {

View file

@ -37,6 +37,7 @@
#include "stratagus.h"
#include "action/action_spellcast.h"
#include "action/action_build.h"
#include "animation.h"
@ -61,6 +62,7 @@
#include "animation/animation_spawnunit.h"
#include "animation/animation_unbreakable.h"
#include "animation/animation_wait.h"
#include "animation/animation_wiggle.h"
#include "actions.h"
#include "iolib.h"
@ -150,6 +152,8 @@ int ParseAnimInt(const CUnit &unit, const char *parseint)
if (s[0] == 't') {
if (unit.CurrentOrder()->HasGoal()) {
goal = unit.CurrentOrder()->GetGoal();
} else if (unit.CurrentOrder()->Action == UnitActionBuild) {
goal = static_cast<const COrder_Build *>(unit.CurrentOrder())->GetBuildingUnit();
} else {
return 0;
}
@ -254,6 +258,14 @@ int ParseAnimInt(const CUnit &unit, const char *parseint)
} else if (s[0] == 'l') { //player number
return ParseAnimPlayer(unit, cur);
} else if (s[0] == 'U') { //unit itself
return UnitNumber(unit);
} else if (s[0] == 'G') { //goal
if (unit.CurrentOrder()->HasGoal()) {
return UnitNumber(*unit.CurrentOrder()->GetGoal());
} else {
return 0;
}
}
// Check if we trying to parse a number
Assert(isdigit(s[0]) || s[0] == '-');
@ -614,6 +626,8 @@ static CAnimation *ParseAnimationFrame(lua_State *l, const char *str)
anim = new CAnimation_RandomGoto;
} else if (op1 == "lua-callback") {
anim = new CAnimation_LuaCallback;
} else if (op1 == "wiggle") {
anim = new CAnimation_Wiggle;
} else {
LuaError(l, "Unknown animation: %s" _C_ op1.c_str());
}

View file

@ -0,0 +1,83 @@
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Stratagus - A free fantasy real time strategy game engine
//
/**@name animation_wiggle.cpp - The animation wiggle. */
//
// (c) Copyright 2022 by Tim Felgentreff
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//@{
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include "stratagus.h"
#include "animation/animation_wiggle.h"
#include "tile.h"
#include "pathfinder.h"
#include "unit.h"
/* virtual */ void CAnimation_Wiggle::Action(CUnit &unit, int &/*move*/, int /*scale*/) const
{
const char *xC = x.c_str();
const char *yC = y.c_str();
int x = ParseAnimInt(unit, xC);
int y = ParseAnimInt(unit, yC);
if (this->isHeading) {
x *= Heading2X[unit.Direction / NextDirection];
y *= Heading2Y[unit.Direction / NextDirection];
unit.IX += x;
unit.IY += y;
} else {
unit.IX += x;
unit.IY += y;
}
}
/* virtual */ void CAnimation_Wiggle::Init(const char *s, lua_State *)
{
const std::string str(s);
const size_t len = str.size();
size_t begin = 0;
size_t end = str.find(' ', begin);
this->x.assign(str, begin, end - begin);
begin = std::min(len, str.find_first_not_of(' ', end));
end = std::min(len, str.find(' ', begin));
this->y.assign(str, begin, end - begin);
begin = std::min(len, str.find_first_not_of(' ', end));
end = std::min(len, str.find(' ', begin));
std::string mode(str, begin, end - begin);
if (mode == "absolute") {
} else if (mode == "heading") {
this->isHeading = true;
}
}
//@}

View file

@ -63,6 +63,8 @@ public:
const CUnitType &GetUnitType() const { return *Type; }
virtual const Vec2i GetGoalPos() const;
CUnit *GetBuildingUnit() const;
private:
bool MoveToLocation(CUnit &unit);
CUnit *CheckCanBuild(CUnit &unit);

View file

@ -73,7 +73,8 @@ enum AnimationType {
AnimationSetVar,
AnimationSetPlayerVar,
AnimationDie,
AnimationLuaCallback
AnimationLuaCallback,
AnimationWiggle,
};
//Modify types

View file

@ -0,0 +1,54 @@
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Stratagus - A free fantasy real time strategy game engine
//
/**@name animation_wiggle.h - The animation wiggle headerfile. */
//
// (c) Copyright 2012 by Joris Dauphin
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
#ifndef ANIMATION_WIGGLE_H
#define ANIMATION_WIGGLE_H
//@{
#include <string>
#include "animation.h"
class CAnimation_Wiggle : public CAnimation
{
public:
CAnimation_Wiggle() : CAnimation(AnimationWiggle), isHeading(false), deltaSpeed(""), gotoLabel(NULL), notReachedLabel(NULL) {}
virtual void Action(CUnit &unit, int &move, int scale) const;
virtual void Init(const char *s, lua_State *l);
private:
std::string x;
std::string y;
bool isHeading;
};
//@}
#endif // ANIMATION_WIGGLE_H