Bläddra i källkod

display item entities in the world

Kolja Strohm 3 år sedan
förälder
incheckning
b312518b3d

+ 13 - 0
FactoryCraft/BasicBlocks.cpp

@@ -242,4 +242,17 @@ Item* DirtBlockItemType::createItem() const
 {
     BasicBlockItem* item = new BasicBlockItem( (ItemType*)this, "Dirt" );
     return item;
+}
+
+Framework::Model3DTextur* DirtBlockItemType::getItemTextur() const
+{
+    Model3DTextur* textur = new Model3DTextur();
+    Textur* tex = currentGame->zScreen()->zGraphicsApi()->createOrGetTextur( "blocks.ltdb/dirt", 0 );
+    textur->setPolygonTextur( 0, dynamic_cast<Textur*>(tex->getThis()) );
+    textur->setPolygonTextur( 1, dynamic_cast<Textur*>(tex->getThis()) );
+    textur->setPolygonTextur( 2, dynamic_cast<Textur*>(tex->getThis()) );
+    textur->setPolygonTextur( 3, dynamic_cast<Textur*>(tex->getThis()) );
+    textur->setPolygonTextur( 4, dynamic_cast<Textur*>(tex->getThis()) );
+    textur->setPolygonTextur( 5, tex );
+    return textur;
 }

+ 1 - 0
FactoryCraft/BasicBlocks.h

@@ -48,5 +48,6 @@ protected:
 
 public:
     virtual Item* createItem() const override;
+    virtual Framework::Model3DTextur* getItemTextur() const override;
 };
 REGISTER( DirtBlockItemType, ItemType )

+ 155 - 0
FactoryCraft/Block.cpp

@@ -74,4 +74,159 @@ void BasicBlockItemType::loadSuperItem( Item* zItem, Framework::StreamReader* zR
     zReader->lese( (char*)&item->hardness, 4 );
     zReader->lese( (char*)&item->toolId, 4 );
     zReader->lese( (char*)&item->speedModifier, 4 );
+}
+
+Framework::Model3DData* BasicBlockItemType::getItemModel() const
+{
+    if( window->zBildschirm()->zGraphicsApi()->hasModel( "itemCube" ) )
+        return window->zBildschirm()->zGraphicsApi()->getModel( "itemCube" );
+    else
+    {
+        Framework::Model3DData* data = window->zBildschirm()->zGraphicsApi()->createModel( "itemCube" );
+        data->setAmbientFactor( 0.8f );
+        data->setDiffusFactor( 0.1f );
+        data->setSpecularFactor( 0.1f );
+        float size = 0.2f;
+        float left, right, top, bottom;
+        // Calculate the screen coordinates of the left side of the bitmap.
+        left = (float)((size / 2.0) * -1);
+        // Calculate the screen coordinates of the right side of the bitmap.
+        right = left + (float)size;
+        // Calculate the screen coordinates of the top of the bitmap.
+        top = (float)(size / 2.0);
+        // Calculate the screen coordinates of the bottom of the bitmap.
+        bottom = top - (float)size;
+        float front = -size / 2;
+        float back = front + size;
+
+        Vertex3D* vertecies = new Vertex3D[ 24 ];
+        for( int i = 0; i < 24; i++ )
+            vertecies[ i ].knochenId = 0;
+        vertecies[ 0 ].pos = Vec3<float >( left, top, front );
+        vertecies[ 0 ].tPos = Vec2< float >( 0.f, 0.f );
+        vertecies[ 1 ].pos = Vec3<float >( right, top, front );
+        vertecies[ 1 ].tPos = Vec2< float >( 1.f, 0.f );
+        vertecies[ 2 ].pos = Vec3<float >( left, bottom, front );
+        vertecies[ 2 ].tPos = Vec2< float >( 0.f, 1.f );
+        vertecies[ 3 ].pos = Vec3<float >( right, bottom, front );
+        vertecies[ 3 ].tPos = Vec2< float >( 1.f, 1.f );
+        vertecies[ 4 ].pos = Vec3<float >( left, top, back );
+        vertecies[ 4 ].tPos = Vec2< float >( 0.0f, 0.0f );
+        vertecies[ 5 ].pos = Vec3<float >( right, top, back );
+        vertecies[ 5 ].tPos = Vec2< float >( 1.0f, 0.0f );
+        vertecies[ 6 ].pos = Vec3<float >( left, bottom, back );
+        vertecies[ 6 ].tPos = Vec2< float >( 0.0f, 1.0f );
+        vertecies[ 7 ].pos = Vec3<float >( right, bottom, back );
+        vertecies[ 7 ].tPos = Vec2< float >( 1.0f, 1.0f );
+
+        vertecies[ 8 ].pos = Vec3<float >( left, top, front );
+        vertecies[ 8 ].tPos = Vec2< float >( 1.f, 0.f );
+        vertecies[ 9 ].pos = Vec3<float >( right, top, front );
+        vertecies[ 9 ].tPos = Vec2< float >( 0.f, 0.f );
+        vertecies[ 10 ].pos = Vec3<float >( left, bottom, front );
+        vertecies[ 10 ].tPos = Vec2< float >( 1.f, 1.f );
+        vertecies[ 11 ].pos = Vec3<float >( right, bottom, front );
+        vertecies[ 11 ].tPos = Vec2< float >( 0.f, 1.f );
+        vertecies[ 12 ].pos = Vec3<float >( left, top, back );
+        vertecies[ 12 ].tPos = Vec2< float >( 0.0f, 0.0f );
+        vertecies[ 13 ].pos = Vec3<float >( right, top, back );
+        vertecies[ 13 ].tPos = Vec2< float >( 1.0f, 0.0f );
+        vertecies[ 14 ].pos = Vec3<float >( left, bottom, back );
+        vertecies[ 14 ].tPos = Vec2< float >( 0.0f, 1.0f );
+        vertecies[ 15 ].pos = Vec3<float >( right, bottom, back );
+        vertecies[ 15 ].tPos = Vec2< float >( 1.0f, 1.0f );
+
+        vertecies[ 16 ].pos = Vec3<float >( left, top, front );
+        vertecies[ 16 ].tPos = Vec2< float >( 0.f, 1.f );
+        vertecies[ 17 ].pos = Vec3<float >( right, top, front );
+        vertecies[ 17 ].tPos = Vec2< float >( 1.f, 1.f );
+        vertecies[ 18 ].pos = Vec3<float >( left, bottom, front );
+        vertecies[ 18 ].tPos = Vec2< float >( 0.f, 0.f );
+        vertecies[ 19 ].pos = Vec3<float >( right, bottom, front );
+        vertecies[ 19 ].tPos = Vec2< float >( 1.f, 0.f );
+        vertecies[ 20 ].pos = Vec3<float >( left, top, back );
+        vertecies[ 20 ].tPos = Vec2< float >( 0.0f, 0.0f );
+        vertecies[ 21 ].pos = Vec3<float >( right, top, back );
+        vertecies[ 21 ].tPos = Vec2< float >( 1.0f, 0.0f );
+        vertecies[ 22 ].pos = Vec3<float >( left, bottom, back );
+        vertecies[ 22 ].tPos = Vec2< float >( 0.0f, 1.0f );
+        vertecies[ 23 ].pos = Vec3<float >( right, bottom, back );
+        vertecies[ 23 ].tPos = Vec2< float >( 1.0f, 1.0f );
+
+        data->setVertecies( vertecies, 24 );
+        // the order of the polygons has to be NORTH, EAST, SOUTH, WEST, TOP, BOTTOM according to the Area definition
+        // down side
+        Polygon3D* p = new Polygon3D();
+        p->indexAnz = 6;
+        p->indexList = new int[ p->indexAnz ];
+        p->indexList[ 0 ] = 6 + 16;
+        p->indexList[ 1 ] = 2 + 16;
+        p->indexList[ 2 ] = 3 + 16;
+        p->indexList[ 3 ] = 6 + 16;
+        p->indexList[ 4 ] = 3 + 16;
+        p->indexList[ 5 ] = 7 + 16;
+        data->addPolygon( p );
+        // right side
+        p = new Polygon3D();
+        p->indexAnz = 6;
+        p->indexList = new int[ p->indexAnz ];
+        p->indexList[ 0 ] = 1 + 8;
+        p->indexList[ 1 ] = 7 + 8;
+        p->indexList[ 2 ] = 3 + 8;
+        p->indexList[ 3 ] = 1 + 8;
+        p->indexList[ 4 ] = 5 + 8;
+        p->indexList[ 5 ] = 7 + 8;
+        data->addPolygon( p );
+        // top side
+        p = new Polygon3D();
+        p->indexAnz = 6;
+        p->indexList = new int[ p->indexAnz ];
+        p->indexList[ 0 ] = 4 + 16;
+        p->indexList[ 1 ] = 1 + 16;
+        p->indexList[ 2 ] = 0 + 16;
+        p->indexList[ 3 ] = 4 + 16;
+        p->indexList[ 4 ] = 5 + 16;
+        p->indexList[ 5 ] = 1 + 16;
+        data->addPolygon( p );
+        // left side
+        p = new Polygon3D();
+        p->indexAnz = 6;
+        p->indexList = new int[ p->indexAnz ];
+        p->indexList[ 0 ] = 0 + 8;
+        p->indexList[ 1 ] = 2 + 8;
+        p->indexList[ 2 ] = 6 + 8;
+        p->indexList[ 3 ] = 0 + 8;
+        p->indexList[ 4 ] = 6 + 8;
+        p->indexList[ 5 ] = 4 + 8;
+        data->addPolygon( p );
+        // back side
+        p = new Polygon3D();
+        p->indexAnz = 6;
+        p->indexList = new int[ p->indexAnz ];
+        p->indexList[ 0 ] = 4;
+        p->indexList[ 1 ] = 6;
+        p->indexList[ 2 ] = 7;
+        p->indexList[ 3 ] = 4;
+        p->indexList[ 4 ] = 7;
+        p->indexList[ 5 ] = 5;
+        data->addPolygon( p );
+        // front side
+        p = new Polygon3D();
+        p->indexAnz = 6;
+        p->indexList = new int[ p->indexAnz ];
+        p->indexList[ 0 ] = 0;
+        p->indexList[ 1 ] = 3;
+        p->indexList[ 2 ] = 2;
+        p->indexList[ 3 ] = 0;
+        p->indexList[ 4 ] = 1;
+        p->indexList[ 5 ] = 3;
+        data->addPolygon( p );
+        data->calculateNormals();
+        return data;
+    }
+}
+
+Framework::Model3DTextur* BasicBlockItemType::getItemTextur() const
+{
+    return 0;
 }

+ 2 - 0
FactoryCraft/Block.h

@@ -62,4 +62,6 @@ class BasicBlockItemType : public ItemType
 protected:
     BasicBlockItemType( int id );
     virtual void loadSuperItem( Item* zItem, Framework::StreamReader* zReader ) const override;
+    virtual Framework::Model3DData* getItemModel() const override;
+    virtual Framework::Model3DTextur* getItemTextur() const override;
 };

+ 1 - 0
FactoryCraft/Entity.cpp

@@ -10,6 +10,7 @@ Entity::~Entity()
 
 bool Entity::tick( double time )
 {
+    setPosition( location );
     // TODO: calculate rotation based on faceDir
     return Model3D::tick( time );
 }

+ 4 - 1
FactoryCraft/EntityType.cpp

@@ -1,10 +1,13 @@
 #include "EntityType.h"
 #include "Entity.h"
+#include "Registries.h"
 
 
 EntityType::EntityType( int id )
     : id( id )
-{}
+{
+    STATIC_REGISTRY( EntityType ).registerT( this, id );
+}
 
 void EntityType::loadSuperEntity( Entity* zEntity, Framework::StreamReader* zReader ) const
 {

+ 1 - 1
FactoryCraft/Inventoty.cpp

@@ -68,5 +68,5 @@ void Inventory::addSlot( ItemSlot* slot )
             break;
         index++;
     }
-    pullSlotsOrder->add( slot, index );
+    pushSlotsOrder->add( slot, index );
 }

+ 14 - 1
FactoryCraft/ItemEntity.cpp

@@ -1,14 +1,27 @@
 #include "ItemEntity.h"
+#include "ItemSlot.h"
+#include "Globals.h"
 
 
 ItemEntity::ItemEntity()
     : Entity( ItemEntityType::INSTANCE, 1 )
 {
-    ItemSlot* slot = new ItemSlot( INT_MAX, 0, 0, 0, ANY_DIRECTION, 0 );
+    slot = new ItemSlot( INT_MAX, 0, 0, 0, ANY_DIRECTION, 0 );
     addSlot( slot );
+    currentGame->setVisibility( this, 1 );
     // TODO: initialize model3d
 }
 
+bool ItemEntity::tick( double time )
+{
+    if( !model && slot->zStack() )
+    {
+        setModelDaten( slot->zStack()->zItem()->zItemType()->getItemModel() );
+        textur = slot->zStack()->zItem()->zItemType()->getItemTextur();
+    }
+    return Entity::tick( time );
+}
+
 
 ItemEntityType::ItemEntityType()
     : EntityType( ID )

+ 4 - 0
FactoryCraft/ItemEntity.h

@@ -3,11 +3,15 @@
 #include "Entity.h"
 #include "StaticRegistry.h"
 
+class ItemSlot;
 
 class ItemEntity : public Entity
 {
+    ItemSlot* slot;
 public:
     ItemEntity();
+
+    virtual bool tick( double time ) override;
 };
 
 class ItemEntityType : public EntityType

+ 6 - 1
FactoryCraft/ItemSlot.cpp

@@ -18,7 +18,7 @@ ItemSlot::~ItemSlot()
         items->release();
 }
 
-void ItemSlot::setItems( ItemStack* item )
+void ItemSlot::setItems( ItemStack* items )
 {
     this->items = items;
 }
@@ -31,4 +31,9 @@ int ItemSlot::getPullPriority() const
 int ItemSlot::getPushPriority() const
 {
     return pushPriority;
+}
+
+ItemStack* ItemSlot::zStack() const
+{
+    return items;
 }

+ 1 - 0
FactoryCraft/ItemSlot.h

@@ -21,4 +21,5 @@ public:
     void setItems( ItemStack* item );
     int getPullPriority() const;
     int getPushPriority() const;
+    ItemStack* zStack() const;
 };

+ 5 - 0
FactoryCraft/ItemStack.cpp

@@ -27,4 +27,9 @@ int ItemStack::getSize() const
 int ItemStack::getMaxSize() const
 {
     return maxSize;
+}
+
+Item* ItemStack::zItem() const
+{
+    return item;
 }

+ 1 - 0
FactoryCraft/ItemStack.h

@@ -16,4 +16,5 @@ public:
 
     int getSize() const;
     int getMaxSize() const;
+    Item* zItem() const;
 };

+ 4 - 0
FactoryCraft/ItemType.h

@@ -4,6 +4,8 @@
 #include <JSON.h>
 #include <Trie.h>
 #include <Writer.h>
+#include <Model3D.h>
+#include <Textur.h>
 
 class Item;
 class ItemStack;
@@ -26,4 +28,6 @@ public:
     virtual Item* createItem() const = 0;
     virtual ItemStack* createItemStack( int size ) const;
     virtual Item* loadItem( Framework::StreamReader* zReader ) const;
+    virtual Framework::Model3DData* getItemModel() const = 0;
+    virtual Framework::Model3DTextur* getItemTextur() const = 0;
 };

+ 1 - 1
FactoryCraft/Main.cpp

@@ -36,7 +36,7 @@ int KSGStart Framework::Start( Framework::Startparam p )
     WNDCLASS wc = Framework::F_Normal( p.hinst );
     wc.lpszClassName = "Factory Craft";
     window.erstellen( WS_POPUPWINDOW, wc );
-    Monitor m = Framework::getMonitor( 1 );
+    Monitor m = Framework::getMonitor( 0 );
     window.setBounds( Punkt( m.x, m.y ), Punkt( m.breite, m.height ) );
     window.setAnzeigeModus( SW_SHOWNORMAL );
     window.setVSchließAktion( [&window]( void* p, void* f ) {

+ 10 - 0
FactoryCraft/World.cpp

@@ -201,6 +201,16 @@ void World::setVisibility( Chunk* zChunk, bool visible )
     renderedWorld->unlock();
 }
 
+void World::setVisibility( Entity* zEntity, bool visible )
+{
+    renderedWorld->lock();
+    if( visible )
+        renderedWorld->addZeichnung( dynamic_cast<Framework::Model3D*>(zEntity->getThis()) );
+    else
+        renderedWorld->removeZeichnung( zEntity );
+    renderedWorld->unlock();
+}
+
 Framework::Bildschirm3D* World::zScreen() const
 {
     return zScreenPtr;

+ 1 - 0
FactoryCraft/World.h

@@ -33,6 +33,7 @@ public:
     Dimension* zDimension( int id ) const;
     Dimension* zDimensionOrCreate( int id );
     void setVisibility( Chunk* zChunk, bool visible );
+    void setVisibility( Entity* zEntity, bool visible );
     Framework::Bildschirm3D* zScreen() const;
     Framework::Punkt getChunkCenter( int x, int y ) const;
 };