浏览代码

add entity system

Kolja Strohm 3 年之前
父节点
当前提交
201195c511

+ 18 - 0
FactoryCraft/AddEntityUpdate.cpp

@@ -0,0 +1,18 @@
+#include "AddEntityUpdate.h"
+#include "StaticRegistry.h"
+#include "Registries.h"
+#include "Globals.h"
+#include "Entity.h"
+
+
+AddEntityUpdateType::AddEntityUpdateType()
+    : WorldUpdateType( ID )
+{}
+
+void AddEntityUpdateType::applyUpdate( Framework::StreamReader* zReader )
+{
+    int type;
+    zReader->lese( (char*)&type, 4 );
+    Entity* entity = STATIC_REGISTRY( EntityType ).zElement( type )->loadEntity( zReader );
+    currentGame->zDimensionOrCreate( entity->getCurrentDimension() )->addEntity( entity );
+}

+ 16 - 0
FactoryCraft/AddEntityUpdate.h

@@ -0,0 +1,16 @@
+#pragma once
+
+#include "WorldUpdate.h"
+
+class AddEntityUpdateType : public WorldUpdateType
+{
+    REGISTRABLE( AddEntityUpdateType )
+
+protected:
+    AddEntityUpdateType();
+
+public:
+    void applyUpdate( Framework::StreamReader* zReader ) override;
+};
+
+REGISTER( AddEntityUpdateType, WorldUpdateType )

+ 2 - 0
FactoryCraft/Area.h

@@ -23,6 +23,8 @@ enum Direction
 };
 typedef int Directions;
 
+#define ANY_DIRECTION NORTH | EAST | SOUTH | WEST | TOP | BOTTOM
+
 Framework::Vec3<int> getDirection( Directions dir );
 int getDirectionIndex( Direction dir );
 Direction getOppositeDirection( Direction dir );

+ 0 - 1
FactoryCraft/Block.h

@@ -1,7 +1,6 @@
 #pragma once
 
 #include <Model3D.h>
-#include <Either.h>
 
 #include "Inventory.h"
 #include "BlockType.h"

+ 0 - 1
FactoryCraft/Chunk.h

@@ -3,7 +3,6 @@
 #include <Punkt.h>
 #include <Model3DCollection.h>
 #include <Reader.h>
-#include <Either.h>
 #include <Model3D.h>
 #include <Array.h>
 #include <Critical.h>

+ 2 - 2
FactoryCraft/CurrentPlayer.h

@@ -1,10 +1,10 @@
 #pragma once
-#include "Entity.h"
+#include "Player.h"
 #include <Kam3D.h>
 
 class CurrentPlayer : public Framework::ReferenceCounter
 {
 private:
-    Entity* entity;
+    Player* player;
     Framework::Kam3D* kam3D;
 };

+ 30 - 0
FactoryCraft/Entity.cpp

@@ -0,0 +1,30 @@
+#include "Entity.h"
+
+
+Entity::Entity( const EntityType* zType, bool hasInventory )
+    : Model3D(), Inventory( { 0.f, 0.f, 0.f }, hasInventory ), zEntityType( zType )
+{}
+
+Entity::~Entity()
+{}
+
+bool Entity::tick( double time )
+{
+    // TODO: calculate rotation based on faceDir
+    return Model3D::tick( time );
+}
+
+int Entity::getId() const
+{
+    return id;
+}
+
+const EntityType* Entity::zType() const
+{
+    return zEntityType;
+}
+
+int Entity::getCurrentDimension() const
+{
+    return currentDimensionId;
+}

+ 30 - 1
FactoryCraft/Entity.h

@@ -1,8 +1,37 @@
 #pragma once
 
 #include <Model3D.h>
+#include "EntityType.h"
+#include "Inventory.h"
 
-class Entity : public Framework::Model3D
+class Entity : public Framework::Model3D, public Inventory
 {
+protected:
+    float maxHP;
+    float currentHP;
+    float stamina;
+    float maxStamina;
+    float hunger;
+    float maxHunger;
+    float thirst;
+    float maxThirst;
+    float targetDistanceLimit;
+    Framework::Vec3<float> speed;
+    Framework::Vec3<float> faceDir;
+    const EntityType* zEntityType;
+    int currentDimensionId;
+    float gravityMultiplier;
+    int id;
 
+public:
+    Entity( const EntityType* zType, bool hasInventory );
+    ~Entity();
+
+    virtual bool tick( double time ) override;
+
+    int getId() const;
+    const EntityType* zType() const;
+    int getCurrentDimension() const;
+
+    friend EntityType;
 };

+ 46 - 0
FactoryCraft/EntityType.cpp

@@ -0,0 +1,46 @@
+#include "EntityType.h"
+#include "Entity.h"
+
+
+EntityType::EntityType( int id )
+    : id( id )
+{}
+
+void EntityType::loadSuperEntity( Entity* zEntity, Framework::StreamReader* zReader ) const
+{
+    zEntity->loadInventory( zReader );
+    zReader->lese( (char*)&zEntity->id, 4 );
+    zReader->lese( (char*)&zEntity->maxHP, 4 );
+    zReader->lese( (char*)&zEntity->currentHP, 4 );
+    zReader->lese( (char*)&zEntity->stamina, 4 );
+    zReader->lese( (char*)&zEntity->maxStamina, 4 );
+    zReader->lese( (char*)&zEntity->hunger, 4 );
+    zReader->lese( (char*)&zEntity->maxHunger, 4 );
+    zReader->lese( (char*)&zEntity->thirst, 4 );
+    zReader->lese( (char*)&zEntity->maxThirst, 4 );
+    zReader->lese( (char*)&zEntity->speed.x, 4 );
+    zReader->lese( (char*)&zEntity->speed.y, 4 );
+    zReader->lese( (char*)&zEntity->speed.z, 4 );
+    zReader->lese( (char*)&zEntity->faceDir.x, 4 );
+    zReader->lese( (char*)&zEntity->faceDir.y, 4 );
+    zReader->lese( (char*)&zEntity->currentDimensionId, 4 );
+    zReader->lese( (char*)&zEntity->location.x, 4 );
+    zReader->lese( (char*)&zEntity->location.y, 4 );
+    zReader->lese( (char*)&zEntity->location.z, 4 );
+}
+
+void EntityType::createSuperEntity( Entity* zEntity ) const
+{}
+
+Entity* EntityType::loadEntity( Framework::StreamReader* zReader ) const
+{
+    Entity* entity = createEntity();
+    createSuperEntity( entity );
+    loadSuperEntity( entity, zReader );
+    return entity;
+}
+
+int EntityType::getId() const
+{
+    return id;
+}

+ 25 - 0
FactoryCraft/EntityType.h

@@ -0,0 +1,25 @@
+#pragma once
+
+#include <ReferenceCounter.h>
+#include <Reader.h>
+#include <Vec3.h>
+
+class Entity;
+
+class EntityType : public virtual Framework::ReferenceCounter
+{
+private:
+    const int id;
+
+protected:
+    EntityType( int id );
+
+    virtual void loadSuperEntity( Entity* zEntity, Framework::StreamReader* zReader ) const;
+    virtual void createSuperEntity( Entity* zEntity ) const;
+    virtual Entity* createEntity() const = 0;
+
+public:
+    virtual Entity* loadEntity( Framework::StreamReader* zReader ) const;
+
+    int getId() const;
+};

+ 9 - 1
FactoryCraft/FactoryCraft.vcxproj

@@ -171,6 +171,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClCompile Include="AddChunkUpdate.cpp" />
+    <ClCompile Include="AddEntityUpdate.cpp" />
     <ClCompile Include="Area.cpp" />
     <ClCompile Include="BasicBlocks.cpp" />
     <ClCompile Include="Block.cpp" />
@@ -180,12 +181,15 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClCompile Include="Chunk.cpp" />
     <ClCompile Include="Dimension.cpp" />
     <ClCompile Include="DirectConnect.cpp" />
+    <ClCompile Include="Entity.cpp" />
+    <ClCompile Include="EntityType.cpp" />
     <ClCompile Include="FactoryClient.cpp" />
     <ClCompile Include="Game.cpp" />
     <ClCompile Include="Globals.cpp" />
     <ClCompile Include="Initialisierung.cpp" />
     <ClCompile Include="Inventoty.cpp" />
     <ClCompile Include="Item.cpp" />
+    <ClCompile Include="ItemEntity.cpp" />
     <ClCompile Include="ItemSlot.cpp" />
     <ClCompile Include="ItemStack.cpp" />
     <ClCompile Include="ItemType.cpp" />
@@ -196,12 +200,14 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClCompile Include="NetworkHandler.cpp" />
     <ClCompile Include="NoBlock.cpp" />
     <ClCompile Include="PlaceBlockUpdate.cpp" />
+    <ClCompile Include="Player.cpp" />
     <ClCompile Include="PlayerKam.cpp" />
     <ClCompile Include="StaticInitializerOrder.cpp" />
     <ClCompile Include="World.cpp" />
     <ClCompile Include="WorldUpdate.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="AddEntityUpdate.h" />
     <ClInclude Include="Area.h" />
     <ClInclude Include="BasicBlocks.h" />
     <ClInclude Include="Block.h" />
@@ -210,10 +216,10 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClInclude Include="BlockType.h" />
     <ClInclude Include="Chunk.h" />
     <ClInclude Include="Constants.h" />
-    <ClInclude Include="CurrentPlayer.h" />
     <ClInclude Include="Dimension.h" />
     <ClInclude Include="DirectConnect.h" />
     <ClInclude Include="Entity.h" />
+    <ClInclude Include="EntityType.h" />
     <ClInclude Include="FactoryClient.h" />
     <ClInclude Include="Game.h" />
     <ClInclude Include="Globals.h" />
@@ -221,6 +227,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClInclude Include="Initialisierung.h" />
     <ClInclude Include="Inventory.h" />
     <ClInclude Include="Item.h" />
+    <ClInclude Include="ItemEntity.h" />
     <ClInclude Include="ItemSlot.h" />
     <ClInclude Include="ItemStack.h" />
     <ClInclude Include="ItemType.h" />
@@ -230,6 +237,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClInclude Include="NetworkHandler.h" />
     <ClInclude Include="NoBlock.h" />
     <ClInclude Include="PlaceBlockUpdate.h" />
+    <ClInclude Include="Player.h" />
     <ClInclude Include="PlayerKam.h" />
     <ClInclude Include="Registries.h" />
     <ClInclude Include="StaticRegistry.h" />

+ 33 - 6
FactoryCraft/FactoryCraft.vcxproj.filters

@@ -37,6 +37,9 @@
       <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
       <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
     </Filter>
+    <Filter Include="Headerdateien\Entity">
+      <UniqueIdentifier>{2a45d30e-df5b-4886-8ae7-d42ff2b3a13e}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Main.cpp">
@@ -129,14 +132,23 @@
     <ClCompile Include="BlockRemovedUpdate.cpp">
       <Filter>world\update</Filter>
     </ClCompile>
+    <ClCompile Include="EntityType.cpp">
+      <Filter>Headerdateien\Entity</Filter>
+    </ClCompile>
+    <ClCompile Include="Entity.cpp">
+      <Filter>Headerdateien\Entity</Filter>
+    </ClCompile>
+    <ClCompile Include="Player.cpp">
+      <Filter>Headerdateien\Entity</Filter>
+    </ClCompile>
+    <ClCompile Include="AddEntityUpdate.cpp">
+      <Filter>world\update</Filter>
+    </ClCompile>
+    <ClCompile Include="ItemEntity.cpp">
+      <Filter>Headerdateien\Entity</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
-    <ClInclude Include="Entity.h">
-      <Filter>Headerdateien</Filter>
-    </ClInclude>
-    <ClInclude Include="CurrentPlayer.h">
-      <Filter>Headerdateien</Filter>
-    </ClInclude>
     <ClInclude Include="Area.h">
       <Filter>world</Filter>
     </ClInclude>
@@ -230,5 +242,20 @@
     <ClInclude Include="BlockRemovedUpdate.h">
       <Filter>world\update</Filter>
     </ClInclude>
+    <ClInclude Include="Entity.h">
+      <Filter>Headerdateien\Entity</Filter>
+    </ClInclude>
+    <ClInclude Include="EntityType.h">
+      <Filter>Headerdateien\Entity</Filter>
+    </ClInclude>
+    <ClInclude Include="Player.h">
+      <Filter>Headerdateien\Entity</Filter>
+    </ClInclude>
+    <ClInclude Include="AddEntityUpdate.h">
+      <Filter>world\update</Filter>
+    </ClInclude>
+    <ClInclude Include="ItemEntity.h">
+      <Filter>Headerdateien\Entity</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>

+ 2 - 0
FactoryCraft/Inventory.h

@@ -19,6 +19,8 @@ private:
 protected:
     Framework::Vec3<float> location;
     virtual void loadInventory( Framework::StreamReader* zReader );
+    void addSlot( ItemSlot* slot );
+
 
 public:
     Inventory( const Framework::Vec3<float> location, bool hasInventory );

+ 22 - 0
FactoryCraft/Inventoty.cpp

@@ -47,4 +47,26 @@ void Inventory::loadInventory( Framework::StreamReader* zReader )
             }
         }
     }
+}
+
+void Inventory::addSlot( ItemSlot* slot )
+{
+    int pullPrio = slot->getPullPriority();
+    int pushPrio = slot->getPushPriority();
+    int index = 0;
+    for( auto stack : *pullSlotsOrder )
+    {
+        if( stack->getPullPriority() > pullPrio )
+            break;
+        index++;
+    }
+    pullSlotsOrder->add( dynamic_cast<ItemSlot*>(slot->getThis()), index );
+    index = 0;
+    for( auto stack : *pushSlotsOrder )
+    {
+        if( stack->getPushPriority() > pushPrio )
+            break;
+        index++;
+    }
+    pullSlotsOrder->add( slot, index );
 }

+ 20 - 0
FactoryCraft/ItemEntity.cpp

@@ -0,0 +1,20 @@
+#include "ItemEntity.h"
+
+
+ItemEntity::ItemEntity()
+    : Entity( ItemEntityType::INSTANCE, 1 )
+{
+    ItemSlot* slot = new ItemSlot( INT_MAX, 0, 0, 0, ANY_DIRECTION, 0 );
+    addSlot( slot );
+    // TODO: initialize model3d
+}
+
+
+ItemEntityType::ItemEntityType()
+    : EntityType( ID )
+{}
+
+Entity* ItemEntityType::createEntity() const
+{
+    return new ItemEntity();
+}

+ 23 - 0
FactoryCraft/ItemEntity.h

@@ -0,0 +1,23 @@
+#pragma once
+
+#include "Entity.h"
+#include "StaticRegistry.h"
+
+
+class ItemEntity : public Entity
+{
+public:
+    ItemEntity();
+};
+
+class ItemEntityType : public EntityType
+{
+    REGISTRABLE( ItemEntityType )
+
+protected:
+    virtual Entity* createEntity() const override;
+
+public:
+    ItemEntityType();
+};
+REGISTER( ItemEntityType, EntityType )

+ 10 - 0
FactoryCraft/ItemSlot.cpp

@@ -21,4 +21,14 @@ ItemSlot::~ItemSlot()
 void ItemSlot::setItems( ItemStack* item )
 {
     this->items = items;
+}
+
+int ItemSlot::getPullPriority() const
+{
+    return pullPriority;
+}
+
+int ItemSlot::getPushPriority() const
+{
+    return pushPriority;
 }

+ 2 - 0
FactoryCraft/ItemSlot.h

@@ -19,4 +19,6 @@ public:
     ~ItemSlot();
 
     void setItems( ItemStack* item );
+    int getPullPriority() const;
+    int getPushPriority() const;
 };

+ 30 - 0
FactoryCraft/Player.cpp

@@ -0,0 +1,30 @@
+#include "Player.h"
+#include "Area.h"
+
+Player::Player()
+    : Entity( PlayerEntityType::INSTANCE, 1 )
+{
+    for( int i = 0; i < 9; i++ )
+    {
+        ItemSlot* slot = new ItemSlot( 50, 0, i, 0, ANY_DIRECTION, 0 );
+        addSlot( slot );
+    }
+}
+
+Player::~Player()
+{}
+
+
+PlayerEntityType::PlayerEntityType()
+    : EntityType( ID )
+{}
+
+void PlayerEntityType::loadSuperEntity( Entity* zEntity, Framework::StreamReader* zReader ) const
+{
+    EntityType::loadSuperEntity( zEntity, zReader );
+}
+
+Entity* PlayerEntityType::createEntity() const
+{
+    return new Player();
+}

+ 29 - 0
FactoryCraft/Player.h

@@ -0,0 +1,29 @@
+#pragma once
+
+#include <Text.h>
+#include "Entity.h"
+#include "StaticRegistry.h"
+
+class Player : public Entity
+{
+private:
+    Framework::Text name;
+
+public:
+    Player();
+    ~Player();
+};
+
+class PlayerEntityType : public EntityType
+{
+    REGISTRABLE( PlayerEntityType )
+
+protected:
+    virtual void loadSuperEntity( Entity* zEntity, Framework::StreamReader* zReader ) const override;
+    virtual Entity* createEntity() const override;
+
+public:
+    PlayerEntityType();
+};
+
+REGISTER( PlayerEntityType, EntityType )

+ 3 - 1
FactoryCraft/Registries.h

@@ -4,6 +4,7 @@
 #include "WorldUpdate.h"
 #include "BlockType.h"
 #include "ItemType.h"
+#include "EntityType.h"
 
 #ifndef _REGISTRY
 #define _REGISTRY extern
@@ -13,4 +14,5 @@
 
 _REGISTRY StaticRegistry<WorldUpdateType> WorldUpdateTypeRegistry;
 _REGISTRY StaticRegistry<BlockType> BlockTypeRegistry;
-_REGISTRY StaticRegistry<ItemType> ItemTypeRegistry;
+_REGISTRY StaticRegistry<ItemType> ItemTypeRegistry;
+_REGISTRY StaticRegistry<EntityType> EntityTypeRegistry;

+ 4 - 2
FactoryCraft/StaticInitializerOrder.cpp

@@ -18,9 +18,11 @@ const c *c::INSTANCE = new c();
 #include "NoBlock.h" // must be first
 #include "BasicBlocks.h"
 // entities
-//#include "Player.h"
+#include "Player.h"
+#include "ItemEntity.h"
 // world updates
 #include "AddChunkUpdate.h"
 #include "PlaceBlockUpdate.h"
 #include "BlockChangedUpdate.h"
-#include "BlockRemovedUpdate.h"
+#include "BlockRemovedUpdate.h"
+#include "AddEntityUpdate.h"

+ 20 - 7
FactoryCraft/World.cpp

@@ -80,9 +80,9 @@ void World::update( bool background )
             {
                 if( entityTarget == -1 )
                 {
-                    auto block = zBlockAt( target, dimensions->z( 0 )->getDimensionId() );
-                    if( block.isA() && block.getA() )
-                        block.getA()->setAmbientFactor( block.getA()->getAmbientFactor() - 0.2f );
+                    Block* block = zBlockAt( target, dimensions->z( 0 )->getDimensionId() );
+                    if( block )
+                        block->setAmbientFactor( block->getAmbientFactor() - 0.2f );
                 }
             }
             if( b == 1 )
@@ -105,9 +105,9 @@ void World::update( bool background )
             {
                 if( entityTarget == -1 )
                 {
-                    auto block = zBlockAt( target, dimensions->z( 0 )->getDimensionId() );
-                    if( block.isA() && block.getA() )
-                        block.getA()->setAmbientFactor( block.getA()->getAmbientFactor() + 0.2f );
+                    Block* block = zBlockAt( target, dimensions->z( 0 )->getDimensionId() );
+                    if( block )
+                        block->setAmbientFactor( block->getAmbientFactor() + 0.2f );
                 }
             }
             ((Game*)(Menu*)menuRegister->get( "game" ))->updatePosition( pos, hasTarget, target );
@@ -160,7 +160,7 @@ void World::thread()
     }
 }
 
-Framework::Either<Block*, int> World::zBlockAt( Framework::Vec3<int> location, int dimension ) const
+Block* World::zBlockAt( Framework::Vec3<int> location, int dimension ) const
 {
     Dimension* dim = zDimension( dimension );
     if( dim )
@@ -178,6 +178,19 @@ Dimension* World::zDimension( int id ) const
     return 0;
 }
 
+Dimension* World::zDimensionOrCreate( int id )
+{
+    zScreenPtr->lock();
+    Dimension* d = zDimension( id );
+    if( !d )
+    {
+        d = new Dimension( id );
+        dimensions->add( d );
+    }
+    zScreenPtr->unlock();
+    return d;
+}
+
 void World::setVisibility( Chunk* zChunk, bool visible )
 {
     renderedWorld->lock();

+ 2 - 1
FactoryCraft/World.h

@@ -29,8 +29,9 @@ public:
     void setChunk( Chunk* chunk, int dimensionId );
     void thread() override;
 
-    Framework::Either<Block*, int> zBlockAt( Framework::Vec3<int> location, int dimension ) const;
+    Block* zBlockAt( Framework::Vec3<int> location, int dimension ) const;
     Dimension* zDimension( int id ) const;
+    Dimension* zDimensionOrCreate( int id );
     void setVisibility( Chunk* zChunk, bool visible );
     Framework::Bildschirm3D* zScreen() const;
     Framework::Punkt getChunkCenter( int x, int y ) const;