From: Daniel Karbach Date: Sun, 7 Oct 2012 19:02:25 +0000 (+0200) Subject: store complete entity in hero X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=d2d8ff1fd5f55e8b43d48ae5e75c216492e2f032;p=l2e.git store complete entity in hero --- diff --git a/src/common/Hero.cpp b/src/common/Hero.cpp index 98b92ca..1d276e5 100644 --- a/src/common/Hero.cpp +++ b/src/common/Hero.cpp @@ -35,9 +35,7 @@ Hero::Hero() , battleSprite(0) , meleeAnimation(0) , attackAnimation(0) -, spellAnimation(0) - -, mapSprite(0) { +, spellAnimation(0) { } @@ -61,6 +59,7 @@ void Hero::CreateTypeDescription() { Hero h; int animationId(TypeDescription::GetTypeId("Animation")); + int entityId(TypeDescription::GetTypeId("Entity")); int numberId(TypeDescription::GetTypeId("Number")); int spriteId(TypeDescription::GetTypeId("Sprite")); int statsId(TypeDescription::GetTypeId("Stats")); @@ -87,7 +86,7 @@ void Hero::CreateTypeDescription() { td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), animationId, true)); td.AddField("meleeAnimation", FieldDescription(((char *)&h.meleeAnimation) - ((char *)&h), animationId, true)); - td.AddField("mapSprite", FieldDescription(((char *)&h.mapSprite) - ((char *)&h), spriteId, true)); + td.AddField("mapEntity", FieldDescription(((char *)&h.mapEntity) - ((char *)&h), entityId, false)); } void Hero::Construct(void *data) { diff --git a/src/common/Hero.h b/src/common/Hero.h index d78d149..caa8f4d 100644 --- a/src/common/Hero.h +++ b/src/common/Hero.h @@ -11,6 +11,7 @@ #include "fwd.h" #include "Stats.h" #include "../graphics/fwd.h" +#include "../map/Entity.h" #include @@ -72,7 +73,8 @@ public: graphics::Animation *AttackAnimation() { return attackAnimation; } graphics::Animation *SpellAnimation() { return spellAnimation; } - graphics::Sprite *MapSprite() { return mapSprite; } + map::Entity &MapEntity() { return mapEntity; } + const map::Entity &MapEntity() const { return mapEntity; } static void CreateTypeDescription(); static void Construct(void *); @@ -114,7 +116,7 @@ private: graphics::Animation *attackAnimation; graphics::Animation *spellAnimation; - graphics::Sprite *mapSprite; + map::Entity mapEntity; }; diff --git a/src/main.cpp b/src/main.cpp index ff49b66..5be8305 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -127,6 +127,7 @@ int main(int argc, char **argv) { Sprite::CreateTypeDescription(); Stats::CreateTypeDescription(); common::TargetingMode::CreateTypeDescription(); + Entity::CreateTypeDescription(); Arguments args; args.Read(argc, argv); @@ -509,35 +510,19 @@ int main(int argc, char **argv) { triggers2[0].map = &map1; triggers2[0].target = Vector(8, 3); - SimpleAnimation mapMaximAnimation(gameState.heroes[0].MapSprite(), (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true); - Entity mapMaxim; - mapMaxim.SetAnimation(&mapMaximAnimation); - mapMaxim.Position() = Vector(64, 128); - mapMaxim.SpriteOffset() = Vector(0, -32); - - SimpleAnimation mapSelanAnimation(gameState.heroes[1].MapSprite(), (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true); - Entity mapSelan; - mapSelan.SetAnimation(&mapSelanAnimation); - mapSelan.Position() = Vector(64, 128); - mapSelan.SpriteOffset() = Vector(0, -32); - mapSelan.SetFlags(Entity::FLAG_NONBLOCKING); - mapMaxim.AddFollower(&mapSelan); - - SimpleAnimation mapGuyAnimation(gameState.heroes[2].MapSprite(), (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true); - Entity mapGuy; - mapGuy.SetAnimation(&mapGuyAnimation); - mapGuy.Position() = Vector(64, 128); - mapGuy.SpriteOffset() = Vector(0, -32); - mapGuy.SetFlags(Entity::FLAG_NONBLOCKING); - mapSelan.AddFollower(&mapGuy); - - SimpleAnimation mapDekarAnimation(gameState.heroes[3].MapSprite(), (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true); - Entity mapDekar; - mapDekar.SetAnimation(&mapDekarAnimation); - mapDekar.Position() = Vector(64, 128); - mapDekar.SpriteOffset() = Vector(0, -32); - mapDekar.SetFlags(Entity::FLAG_NONBLOCKING); - mapGuy.AddFollower(&mapDekar); + gameState.heroes[0].MapEntity().Position() = Vector(64, 128); + + gameState.heroes[1].MapEntity().Position() = Vector(64, 128); + gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING); + gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity()); + + gameState.heroes[2].MapEntity().Position() = Vector(64, 128); + gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING); + gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity()); + + gameState.heroes[3].MapEntity().Position() = Vector(64, 128); + gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING); + gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity()); SDL_Surface *mapMonsterImg(IMG_Load("test-data/monster-map.png")); Sprite mapMonsterSprite(mapMonsterImg, 32, 32); @@ -566,7 +551,7 @@ int main(int argc, char **argv) { } else { MapState *mapState(new MapState(&map1)); - mapState->ControlEntity(&mapMaxim); + mapState->ControlEntity(&gameState.heroes[0].MapEntity()); mapState->SetWalkingSpeed(walkSpeed); mapMonster.StartAnimation(*mapState); diff --git a/src/map/Entity.cpp b/src/map/Entity.cpp index 610e585..e1f970b 100644 --- a/src/map/Entity.cpp +++ b/src/map/Entity.cpp @@ -7,7 +7,11 @@ #include "Entity.h" +#include "../loader/TypeDescription.h" + using geometry::Vector; +using loader::FieldDescription; +using loader::TypeDescription; namespace map { @@ -109,4 +113,29 @@ void Entity::Render(SDL_Surface *dest, const Vector &offset) const { } } + +void Entity::CreateTypeDescription() { + Entity e; + + int animationId(TypeDescription::GetTypeId("Animation")); + int vectorId(TypeDescription::GetTypeId("Vector")); + + TypeDescription &td(TypeDescription::CreateOrGet("Entity")); + td.SetConstructor(&Construct); + td.SetLoader(&Load); + td.SetSize(sizeof(Entity)); + + td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), animationId, true)); + td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), vectorId, false)); +} + +void Entity::Construct(void *data) { + new (data) Entity; +} + +void Entity::Load(void *data) { + Entity *entity(reinterpret_cast(data)); + entity->runner.ChangeAnimation(entity->animation); +} + } diff --git a/src/map/Entity.h b/src/map/Entity.h index 872ad0c..3ddc9cc 100644 --- a/src/map/Entity.h +++ b/src/map/Entity.h @@ -41,8 +41,8 @@ public: geometry::Vector &Velocity() { return velocity; } const geometry::Vector &Velocity() const { return velocity; } - geometry::Vector &SpriteOffset() { return spriteOffset; } - const geometry::Vector &SpriteOffset() const { return spriteOffset; } + geometry::Vector &SpriteOffset() { return spriteOffset; } + const geometry::Vector &SpriteOffset() const { return spriteOffset; } void SetAnimation(const graphics::Animation *a); void StartAnimation(app::Application &ctrl); @@ -72,6 +72,10 @@ public: void Render(SDL_Surface *, const geometry::Vector &offset) const; + static void CreateTypeDescription(); + static void Construct(void *); + static void Load(void *); + private: void UpdateVelocity(); @@ -79,7 +83,7 @@ private: Entity *follower; const graphics::Animation *animation; graphics::AnimationRunner runner; - geometry::Vector spriteOffset; + geometry::Vector spriteOffset; geometry::Vector position; geometry::Vector velocity; Orientation orientation; diff --git a/test-data/test.l2s b/test-data/test.l2s index 2576b3c..4a4bb05 100644 --- a/test-data/test.l2s +++ b/test-data/test.l2s @@ -70,15 +70,10 @@ Sprite maximSprite { image: :"maxim.png", size: <64,64> } -Sprite maximMapSprite { - image: :"maxim-map.png", - size: <32,64> -} export Hero maxim { name: "Maxim", level: 1, battleSprite: maximSprite, - mapSprite: maximMapSprite, maxHealth: 33, health: 33, maxMana: 20, @@ -146,6 +141,17 @@ export Hero maxim { }, frametime: twoFramesTime, framecount: 4 + }, + mapEntity: Entity { + animation: SimpleAnimation{ + sprite: Sprite { + image: :"maxim-map.png", + size: <32,64> + }, + frametime: 120, + framecount: 2 + }, + spriteOffset: <0,-32> } } @@ -153,15 +159,10 @@ Sprite selanSprite { image: :"selan.png", size: <64,64> } -Sprite selanMapSprite { - image: :"selan-map.png", - size: <32,64> -} export Hero selan { name: "Selan", level: 1, battleSprite: selanSprite, - mapSprite: selanMapSprite, maxHealth: 28, health: 28, maxMana: 23, @@ -223,6 +224,17 @@ export Hero selan { }, frametime: twoFramesTime, framecount: 4 + }, + mapEntity: Entity { + animation: SimpleAnimation{ + sprite: Sprite { + image: :"selan-map.png", + size: <32,64> + }, + frametime: 120, + framecount: 2 + }, + spriteOffset: <0,-32> } } @@ -230,15 +242,10 @@ Sprite guySprite { image: :"guy.png", size: <64,64> } -Sprite guyMapSprite { - image: :"guy-map.png", - size: <32,64> -} export Hero guy { name: "Guy", level: 1, battleSprite: guySprite, - mapSprite: guyMapSprite, maxHealth: 38, health: 38, maxMana: 0, @@ -282,6 +289,17 @@ export Hero guy { }, frametime: fourFramesTime, framecount: 4 + }, + mapEntity: Entity { + animation: SimpleAnimation{ + sprite: Sprite { + image: :"guy-map.png", + size: <32,64> + }, + frametime: 120, + framecount: 2 + }, + spriteOffset: <0,-32> } } @@ -289,15 +307,10 @@ Sprite dekarSprite { image: :"dekar.png", size: <64,64> } -Sprite dekarMapSprite { - image: :"dekar-map.png", - size: <32,64> -} export Hero dekar { name: "Dekar", level: 1, battleSprite: dekarSprite, - mapSprite: dekarMapSprite, maxHealth: 38, health: 38, maxMana: 0, @@ -360,6 +373,17 @@ export Hero dekar { }, frametime: twoFramesTime, framecount: 4 + }, + mapEntity: Entity { + animation: SimpleAnimation{ + sprite: Sprite { + image: :"dekar-map.png", + size: <32,64> + }, + frametime: 120, + framecount: 2 + }, + spriteOffset: <0,-32> } }