Forráskód Böngészése

add tree seblings

Kolja Strohm 2 éve
szülő
commit
b3ad4aca8c

+ 234 - 6
FactoryCraft/BasicBlock.cpp

@@ -1,4 +1,7 @@
 #include "BasicBlocks.h"
+#include "ItemEntity.h"
+#include "Game.h"
+#include "AddEntityUpdate.h"
 
 
 BasicBlock::BasicBlock(const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos)
@@ -14,6 +17,39 @@ void BasicBlock::onPostTick()
 {}
 
 
+AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos)
+	: BasicBlock(zType, zTool, pos)
+{}
+
+void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config)
+{
+	spawns.add(config);
+}
+
+void AdditionalItemSpawningBlock::onDestroy()
+{
+	for (const SpawnConfig& config : spawns)
+	{
+		if ((double)rand() / RAND_MAX < config.chance)
+		{
+			int amount = config.min + (int)((config.max - config.min) * ((double)rand() / RAND_MAX));
+			if (amount > 0)
+			{
+				ItemStack* spawnedItems = StaticRegistry<ItemType>::INSTANCE.zElement(config.itemType)->createItemStack(amount);
+				if (spawnedItems)
+				{
+					ItemEntity* itemEntity = (ItemEntity*)ItemEntityType::INSTANCE->createEntity(location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f), getDimensionId(), Game::INSTANCE->getNextEntityId());
+					itemEntity->unsaveAddItem(spawnedItems, NO_DIRECTION);
+					spawnedItems->release();
+					Game::INSTANCE->requestWorldUpdate(new AddEntityUpdate(itemEntity, getDimensionId()));
+				}
+			}
+		}
+	}
+	BasicBlock::onDestroy();
+}
+
+
 BasicBlockType::BasicBlockType(int typeId, int itemTypeId, ModelInfo model)
 	: BlockType(typeId, 0, model, 1, 100, 0),
 	itemType(itemTypeId),
@@ -130,22 +166,55 @@ Item* OakBlockItemType::createItem() const
 	return item;
 }
 
-// Leaves Wood
-LeavesBlockType::LeavesBlockType()
-	: BasicBlockType(ID, LeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
+// Oak Leaves
+OakLeavesBlockType::OakLeavesBlockType()
+	: BasicBlockType(ID, OakLeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
 {
 	hardness = 0.1f;
 	defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
 }
 
+Block* OakLeavesBlockType::createBlock(Framework::Vec3<int> position) const
+{
+	AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(this, 0, position);
+	block->addSpawn({ 1, 1, 0.015, OakSeblingBlockItemType::ID });
+	return block;
+}
+
 
-LeavesBlockItemType::LeavesBlockItemType()
+OakLeavesBlockItemType::OakLeavesBlockItemType()
 	: BasicBlockItemType(ID, "Leaves", 0, 0, ModelInfo("itemCube", "blocks.ltdb/leaves.png", 6))
 {}
 
-Item* LeavesBlockItemType::createItem() const
+Item* OakLeavesBlockItemType::createItem() const
+{
+	BasicBlockItem* item = new BasicBlockItem(this, OakLeavesBlockType::INSTANCE, "Oak Leaves");
+	initializeItem(item, 0, 0, 0.1f, 0, 1);
+	return item;
+}
+
+// Oak Sebling
+OakSeblingBlockType::OakSeblingBlockType()
+	: BasicBlockType(ID, OakSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
+{
+	hardness = 0.1f;
+	defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
+}
+
+Block* OakSeblingBlockType::createBlock(Framework::Vec3<int> position) const
+{
+	// TODO: add sebling block
+	return BasicBlockType::createBlock(position);
+}
+
+
+OakSeblingBlockItemType::OakSeblingBlockItemType()
+	: BasicBlockItemType(ID, "Oak Sebling", 0, 0, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
+{}
+
+Item* OakSeblingBlockItemType::createItem() const
 {
-	BasicBlockItem* item = new BasicBlockItem(this, LeavesBlockType::INSTANCE, "Leaves");
+	BasicBlockItem* item = new BasicBlockItem(this, OakSeblingBlockType::INSTANCE, "Oak Sebling");
 	initializeItem(item, 0, 0, 0.1f, 0, 1);
 	return item;
 }
@@ -230,6 +299,59 @@ Item* BirchBlockItemType::createItem() const
 	return item;
 }
 
+// Birch Leaves
+BirchLeavesBlockType::BirchLeavesBlockType()
+	: BasicBlockType(ID, BirchLeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
+{
+	hardness = 0.1f;
+	defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
+}
+
+Block* BirchLeavesBlockType::createBlock(Framework::Vec3<int> position) const
+{
+	AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(this, 0, position);
+	block->addSpawn({ 1, 1, 0.03, BirchSeblingBlockItemType::ID });
+	return block;
+}
+
+
+BirchLeavesBlockItemType::BirchLeavesBlockItemType()
+	: BasicBlockItemType(ID, "Leaves", 0, 0, ModelInfo("itemCube", "blocks.ltdb/leaves.png", 6))
+{}
+
+Item* BirchLeavesBlockItemType::createItem() const
+{
+	BasicBlockItem* item = new BasicBlockItem(this, BirchLeavesBlockType::INSTANCE, "Birch Leaves");
+	initializeItem(item, 0, 0, 0.1f, 0, 1);
+	return item;
+}
+
+// Birch Sebling
+BirchSeblingBlockType::BirchSeblingBlockType()
+	: BasicBlockType(ID, BirchSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
+{
+	hardness = 0.1f;
+	defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
+}
+
+Block* BirchSeblingBlockType::createBlock(Framework::Vec3<int> position) const
+{
+	// TODO: add sebling block
+	return BasicBlockType::createBlock(position);
+}
+
+
+BirchSeblingBlockItemType::BirchSeblingBlockItemType()
+	: BasicBlockItemType(ID, "Birch Sebling", 0, 0, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
+{}
+
+Item* BirchSeblingBlockItemType::createItem() const
+{
+	BasicBlockItem* item = new BasicBlockItem(this, BirchSeblingBlockType::INSTANCE, "Birch Sebling");
+	initializeItem(item, 0, 0, 0.1f, 0, 1);
+	return item;
+}
+
 // Beech Wood
 BeechBlockType::BeechBlockType()
 	: BasicBlockType(ID, BeechBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/beech.png", 6))
@@ -250,6 +372,59 @@ Item* BeechBlockItemType::createItem() const
 	return item;
 }
 
+// Beech Leaves
+BeechLeavesBlockType::BeechLeavesBlockType()
+	: BasicBlockType(ID, BeechLeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
+{
+	hardness = 0.1f;
+	defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
+}
+
+Block* BeechLeavesBlockType::createBlock(Framework::Vec3<int> position) const
+{
+	AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(this, 0, position);
+	block->addSpawn({ 1, 1, 0.02, BeechSeblingBlockItemType::ID });
+	return block;
+}
+
+
+BeechLeavesBlockItemType::BeechLeavesBlockItemType()
+	: BasicBlockItemType(ID, "Leaves", 0, 0, ModelInfo("itemCube", "blocks.ltdb/leaves.png", 6))
+{}
+
+Item* BeechLeavesBlockItemType::createItem() const
+{
+	BasicBlockItem* item = new BasicBlockItem(this, BeechLeavesBlockType::INSTANCE, "Beech Leaves");
+	initializeItem(item, 0, 0, 0.1f, 0, 1);
+	return item;
+}
+
+// Beech Sebling
+BeechSeblingBlockType::BeechSeblingBlockType()
+	: BasicBlockType(ID, BeechSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
+{
+	hardness = 0.1f;
+	defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
+}
+
+Block* BeechSeblingBlockType::createBlock(Framework::Vec3<int> position) const
+{
+	// TODO: add sebling block
+	return BasicBlockType::createBlock(position);
+}
+
+
+BeechSeblingBlockItemType::BeechSeblingBlockItemType()
+	: BasicBlockItemType(ID, "Beech Sebling", 0, 0, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
+{}
+
+Item* BeechSeblingBlockItemType::createItem() const
+{
+	BasicBlockItem* item = new BasicBlockItem(this, BeechSeblingBlockType::INSTANCE, "Beech Sebling");
+	initializeItem(item, 0, 0, 0.1f, 0, 1);
+	return item;
+}
+
 // Basalt
 BasaltBlockType::BasaltBlockType()
 	: BasicBlockType(ID, BasaltBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/basalt.png", 6))
@@ -288,4 +463,57 @@ Item* PineBlockItemType::createItem() const
 	BasicBlockItem* item = new BasicBlockItem(this, PineBlockType::INSTANCE, "Pine");
 	initializeItem(item, 0, 0, 1.4f, 0, 1);
 	return item;
+}
+
+// Pine Leaves
+PineLeavesBlockType::PineLeavesBlockType()
+	: BasicBlockType(ID, PineLeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
+{
+	hardness = 0.1f;
+	defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
+}
+
+
+PineLeavesBlockItemType::PineLeavesBlockItemType()
+	: BasicBlockItemType(ID, "Leaves", 0, 0, ModelInfo("itemCube", "blocks.ltdb/leaves.png", 6))
+{}
+
+Item* PineLeavesBlockItemType::createItem() const
+{
+	BasicBlockItem* item = new BasicBlockItem(this, PineLeavesBlockType::INSTANCE, "Pine Leaves");
+	initializeItem(item, 0, 0, 0.1f, 0, 1);
+	return item;
+}
+
+Block* PineLeavesBlockType::createBlock(Framework::Vec3<int> position) const
+{
+	AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(this, 0, position);
+	block->addSpawn({ 1, 1, 0.025, PineSeblingBlockItemType::ID });
+	return block;
+}
+
+// Pine Sebling
+PineSeblingBlockType::PineSeblingBlockType()
+	: BasicBlockType(ID, PineSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
+{
+	hardness = 0.1f;
+	defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
+}
+
+Block* PineSeblingBlockType::createBlock(Framework::Vec3<int> position) const
+{
+	// TODO: add sebling block
+	return BasicBlockType::createBlock(position);
+}
+
+
+PineSeblingBlockItemType::PineSeblingBlockItemType()
+	: BasicBlockItemType(ID, "Pine Sebling", 0, 0, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
+{}
+
+Item* PineSeblingBlockItemType::createItem() const
+{
+	BasicBlockItem* item = new BasicBlockItem(this, PineSeblingBlockType::INSTANCE, "Pine Sebling");
+	initializeItem(item, 0, 0, 0.1f, 0, 1);
+	return item;
 }

+ 207 - 10
FactoryCraft/BasicBlocks.h

@@ -18,6 +18,25 @@ public:
 	friend BasicBlockType;
 };
 
+struct SpawnConfig
+{
+	int min;
+	int max;
+	double chance;
+	int itemType;
+};
+
+class AdditionalItemSpawningBlock : public BasicBlock
+{
+private:
+	Framework::Array< SpawnConfig> spawns;
+
+public:
+	AdditionalItemSpawningBlock(const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos);
+	void addSpawn(SpawnConfig config);
+	virtual void onDestroy() override;
+};
+
 class BasicBlockType : public BlockType
 {
 private:
@@ -127,27 +146,55 @@ protected:
 };
 REGISTER(OakBlockType, BlockType)
 
-// Leaves
-class LeavesBlockItemType : public BasicBlockItemType
+// Oak Leaves
+class OakLeavesBlockItemType : public BasicBlockItemType
+{
+	REGISTRABLE(OakLeavesBlockItemType)
+
+protected:
+	OakLeavesBlockItemType();
+
+public:
+	virtual Item* createItem() const override;
+};
+REGISTER(OakLeavesBlockItemType, ItemType)
+
+class OakLeavesBlockType : public BasicBlockType
+{
+	REGISTRABLE(OakLeavesBlockType)
+
+protected:
+	OakLeavesBlockType();
+
+public:
+	virtual Block* createBlock(Framework::Vec3<int> position) const override;
+};
+REGISTER(OakLeavesBlockType, BlockType)
+
+// Oak Sebling
+class OakSeblingBlockItemType : public BasicBlockItemType
 {
-	REGISTRABLE(LeavesBlockItemType)
+	REGISTRABLE(OakSeblingBlockItemType)
 
 protected:
-	LeavesBlockItemType();
+	OakSeblingBlockItemType();
 
 public:
 	virtual Item* createItem() const override;
 };
-REGISTER(LeavesBlockItemType, ItemType)
+REGISTER(OakSeblingBlockItemType, ItemType)
 
-class LeavesBlockType : public BasicBlockType
+class OakSeblingBlockType : public BasicBlockType
 {
-	REGISTRABLE(LeavesBlockType)
+	REGISTRABLE(OakSeblingBlockType)
 
 protected:
-	LeavesBlockType();
+	OakSeblingBlockType();
+
+public:
+	virtual Block* createBlock(Framework::Vec3<int> position) const override;
 };
-REGISTER(LeavesBlockType, BlockType)
+REGISTER(OakSeblingBlockType, BlockType)
 
 // Gravel
 class GravelBlockItemType : public BasicBlockItemType
@@ -237,6 +284,56 @@ protected:
 };
 REGISTER(BirchBlockType, BlockType)
 
+// Birch Leaves
+class BirchLeavesBlockItemType : public BasicBlockItemType
+{
+	REGISTRABLE(BirchLeavesBlockItemType)
+
+protected:
+	BirchLeavesBlockItemType();
+
+public:
+	virtual Item* createItem() const override;
+};
+REGISTER(BirchLeavesBlockItemType, ItemType)
+
+class BirchLeavesBlockType : public BasicBlockType
+{
+	REGISTRABLE(BirchLeavesBlockType)
+
+protected:
+	BirchLeavesBlockType();
+
+public:
+	virtual Block* createBlock(Framework::Vec3<int> position) const override;
+};
+REGISTER(BirchLeavesBlockType, BlockType)
+
+// Birch Sebling
+class BirchSeblingBlockItemType : public BasicBlockItemType
+{
+	REGISTRABLE(BirchSeblingBlockItemType)
+
+protected:
+	BirchSeblingBlockItemType();
+
+public:
+	virtual Item* createItem() const override;
+};
+REGISTER(BirchSeblingBlockItemType, ItemType)
+
+class BirchSeblingBlockType : public BasicBlockType
+{
+	REGISTRABLE(BirchSeblingBlockType)
+
+protected:
+	BirchSeblingBlockType();
+
+public:
+	virtual Block* createBlock(Framework::Vec3<int> position) const override;
+};
+REGISTER(BirchSeblingBlockType, BlockType)
+
 // Beech Wood
 class BeechBlockItemType : public BasicBlockItemType
 {
@@ -259,6 +356,56 @@ protected:
 };
 REGISTER(BeechBlockType, BlockType)
 
+// Beech Leaves
+class BeechLeavesBlockItemType : public BasicBlockItemType
+{
+	REGISTRABLE(BeechLeavesBlockItemType)
+
+protected:
+	BeechLeavesBlockItemType();
+
+public:
+	virtual Item* createItem() const override;
+};
+REGISTER(BeechLeavesBlockItemType, ItemType)
+
+class BeechLeavesBlockType : public BasicBlockType
+{
+	REGISTRABLE(BeechLeavesBlockType)
+
+protected:
+	BeechLeavesBlockType();
+
+public:
+	virtual Block* createBlock(Framework::Vec3<int> position) const override;
+};
+REGISTER(BeechLeavesBlockType, BlockType)
+
+// Beech Sebling
+class BeechSeblingBlockItemType : public BasicBlockItemType
+{
+	REGISTRABLE(BeechSeblingBlockItemType)
+
+protected:
+	BeechSeblingBlockItemType();
+
+public:
+	virtual Item* createItem() const override;
+};
+REGISTER(BeechSeblingBlockItemType, ItemType)
+
+class BeechSeblingBlockType : public BasicBlockType
+{
+	REGISTRABLE(BeechSeblingBlockType)
+
+protected:
+	BeechSeblingBlockType();
+
+public:
+	virtual Block* createBlock(Framework::Vec3<int> position) const override;
+};
+REGISTER(BeechSeblingBlockType, BlockType)
+
 // Basalt
 class BasaltBlockItemType : public BasicBlockItemType
 {
@@ -301,4 +448,54 @@ class PineBlockType : public BasicBlockType
 protected:
 	PineBlockType();
 };
-REGISTER(PineBlockType, BlockType)
+REGISTER(PineBlockType, BlockType)
+
+// Pine Leaves
+class PineLeavesBlockItemType : public BasicBlockItemType
+{
+	REGISTRABLE(PineLeavesBlockItemType)
+
+protected:
+	PineLeavesBlockItemType();
+
+public:
+	virtual Item* createItem() const override;
+};
+REGISTER(PineLeavesBlockItemType, ItemType)
+
+class PineLeavesBlockType : public BasicBlockType
+{
+	REGISTRABLE(PineLeavesBlockType)
+
+protected:
+	PineLeavesBlockType();
+
+public:
+	virtual Block* createBlock(Framework::Vec3<int> position) const override;
+};
+REGISTER(PineLeavesBlockType, BlockType)
+
+// Pine Sebling
+class PineSeblingBlockItemType : public BasicBlockItemType
+{
+	REGISTRABLE(PineSeblingBlockItemType)
+
+protected:
+	PineSeblingBlockItemType();
+
+public:
+	virtual Item* createItem() const override;
+};
+REGISTER(PineSeblingBlockItemType, ItemType)
+
+class PineSeblingBlockType : public BasicBlockType
+{
+	REGISTRABLE(PineSeblingBlockType)
+
+protected:
+	PineSeblingBlockType();
+
+public:
+	virtual Block* createBlock(Framework::Vec3<int> position) const override;
+};
+REGISTER(PineSeblingBlockType, BlockType)

+ 2 - 2
FactoryCraft/CraftingStorage.cpp

@@ -171,7 +171,7 @@ bool BasicShapedCrafter::consume(Framework::RCArray<ItemFilter>& filters, Framew
 					if (broken)
 					{
 						ItemStack* brokenStack = new ItemStack(broken, 1);
-						zInventory->addItems(brokenStack, INSIDE);
+						zInventory->unsaveAddItem(brokenStack, INSIDE);
 						// TODO: if brokenStack is not empty spawn an item entity
 						brokenStack->release();
 					}
@@ -204,7 +204,7 @@ bool BasicShapedCrafter::consume(Framework::RCArray<ItemFilter>& filters, Framew
 
 void BasicShapedCrafter::addCraftingResult(ItemStack* stack)
 {
-	zInventory->addItems(stack, NO_DIRECTION);
+	zInventory->unsaveAddItem(stack, NO_DIRECTION);
 }
 
 void BasicShapedCrafter::applyCurrentRecipie()

+ 4 - 4
FactoryCraft/GrasslandBiom.cpp

@@ -10,10 +10,10 @@
 GrasslandBiom::GrasslandBiom()
 	: BiomGenerator()
 {
-	addTemplateGenerator(new TreeTemplate(0.02f, BirchBlockType::INSTANCE, LeavesBlockType::INSTANCE, 8, 15));
-	addTemplateGenerator(new TreeTemplate(0.01f, BeechBlockType::INSTANCE, LeavesBlockType::INSTANCE, 8, 13));
-	addTemplateGenerator(new TreeTemplate(0.005f, OakBlockType::INSTANCE, LeavesBlockType::INSTANCE, 10, 15));
-	addTemplateGenerator(new TreeTemplate(0.0025f, PineBlockType::INSTANCE, LeavesBlockType::INSTANCE, 15, 24));
+	addTemplateGenerator(new TreeTemplate(0.02f, BirchBlockType::INSTANCE, BirchLeavesBlockType::INSTANCE, 8, 15));
+	addTemplateGenerator(new TreeTemplate(0.01f, BeechBlockType::INSTANCE, BeechLeavesBlockType::INSTANCE, 8, 13));
+	addTemplateGenerator(new TreeTemplate(0.005f, OakBlockType::INSTANCE, OakLeavesBlockType::INSTANCE, 10, 15));
+	addTemplateGenerator(new TreeTemplate(0.0025f, PineBlockType::INSTANCE, PineLeavesBlockType::INSTANCE, 15, 24));
 	heightNoise = 0;
 }
 

+ 1 - 1
FactoryCraft/Inventory.h

@@ -60,6 +60,7 @@ protected:
 	virtual void saveInventory(Framework::StreamWriter* zWriter);
 	void removeObserver(Entity* zSource, Framework::Text id);
 	void addObserver(Entity* zSource, Framework::Text id);
+	virtual void addItems(ItemStack* zItems, Direction dir);
 
 public:
 	Inventory(const Framework::Vec3<float> location, bool hasInventory);
@@ -69,7 +70,6 @@ public:
 	void addSlot(ItemSlot* slot);
 	void localTransaction(Framework::Array< ItemSlot* >* zSourceSlots, Framework::Array<ItemSlot*>* zTargetSlots, ItemFilter* zFilter, int count, Direction outDir, Direction inDir);
 	ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
-	virtual void addItems(ItemStack* zItems, Direction dir);
 	virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
 	InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
 	void unsaveAddItem(ItemStack* zStack, Direction dir);