123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- #include "LightSources.h"
- #include "Game.h"
- LightSourceItem::LightSourceItem(
- int itemType, int blockType, Framework::Text name)
- : BasicBlockItem(itemType, blockType, name, 0),
- color(0XFFFFFFFF)
- {}
- bool LightSourceItem::canBeStackedWith(const Item* zItem) const
- {
- const LightSourceItem* other = dynamic_cast<const LightSourceItem*>(zItem);
- return BasicBlockItem::canBeStackedWith(zItem) && other->color == color;
- }
- LightSourceItemType::LightSourceItemType(Framework::Text name,
- ModelInfo* model,
- Framework::Text blockTypeName,
- int maxStackSize,
- Framework::RCArray<Framework::Text> groups)
- : BasicBlockItemType(name,
- model,
- true,
- true,
- 0.f,
- 1.f,
- blockTypeName,
- 0,
- maxStackSize,
- groups)
- {}
- void LightSourceItemType::loadSuperItem(
- Item* zItem, Framework::StreamReader* zReader) const
- {
- LightSourceItem* item = dynamic_cast<LightSourceItem*>(zItem);
- zReader->lese((char*)&item->color, 4);
- BasicBlockItemType::loadSuperItem(zItem, zReader);
- }
- void LightSourceItemType::saveSuperItem(
- const Item* zItem, Framework::StreamWriter* zWriter) const
- {
- const LightSourceItem* item = dynamic_cast<const LightSourceItem*>(zItem);
- zWriter->schreibe((char*)&item->color, 4);
- BasicBlockItemType::saveSuperItem(zItem, zWriter);
- }
- Item* LightSourceItemType::createItem() const
- {
- LightSourceItem* item = new LightSourceItem(id, getBlockTypeId(), name);
- item->color = color;
- return item;
- }
- LightSourceItemType* LightSourceItemType::setColor(int color)
- {
- this->color = color;
- return this;
- }
- BasicLightSource::BasicLightSource(
- int typeId, Framework::Vec3<int> pos, int dimensionId)
- : Block(typeId, pos, dimensionId, false)
- {}
- void BasicLightSource::setLight(int light)
- {
- lightEmisionColor[0] = (unsigned char)((light >> 16) & 0xFF);
- lightEmisionColor[1] = (unsigned char)((light >> 8) & 0xFF);
- lightEmisionColor[2] = (unsigned char)(light & 0xFF);
- }
- bool BasicLightSource::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
- {
- return 0;
- }
- void BasicLightSource::onPostTick() {}
- BasicLightSourceBlockType::BasicLightSourceBlockType()
- : BlockType(),
- itemTypeName(),
- itemTypeId(0),
- transparent(1),
- passable(1),
- interactable(1),
- lightColor(0XFFFFFFFF)
- {}
- void BasicLightSourceBlockType::createSuperBlock(
- Block* zBlock, Item* zItem) const
- {
- BlockType::createSuperBlock(zBlock, zItem);
- BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
- block->transparent = transparent;
- block->passable = passable;
- block->hp = (float)getInitialMaxHP();
- block->maxHP = (float)getInitialMaxHP();
- block->hardness = getHardness();
- block->speedModifier = 1;
- block->interactable = interactable;
- block->setLight(lightColor);
- if (zItem)
- {
- dynamic_cast<BasicLightSource*>(zBlock)->setLight(
- dynamic_cast<LightSourceItem*>(zItem)->color);
- }
- }
- void BasicLightSourceBlockType::loadSuperBlock(
- Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
- {
- BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
- BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
- if (!block)
- throw "BasicLightSourceBlockType::loadSuperBlock was called with a "
- "block witch is not an instance of BasicLightSource";
- zReader->lese((char*)&block->lightEmisionColor, 4);
- }
- void BasicLightSourceBlockType::saveSuperBlock(
- Block* zBlock, Framework::StreamWriter* zWriter) const
- {
- BlockType::saveSuperBlock(zBlock, zWriter);
- BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
- if (!block)
- throw "BasicLightSourceBlockType::saveSuperBlock was called with a "
- "block witch is not an instance of BasicLightSource";
- zWriter->schreibe((char*)&block->lightEmisionColor, 4);
- }
- bool BasicLightSourceBlockType::initialize(Game* zGame)
- {
- if (itemTypeName.getLength())
- {
- itemTypeId = zGame->getItemTypeId(itemTypeName);
- }
- else
- {
- itemTypeId = 0;
- }
- return itemTypeId >= 0 && BlockType::initialize(zGame);
- }
- Block* BasicLightSourceBlockType::createBlock(
- Framework::Vec3<int> position, int dimensionId) const
- {
- return new BasicLightSource(getId(), position, dimensionId);
- }
- Item* BasicLightSourceBlockType::createItem() const
- {
- return Game::INSTANCE->zItemType(itemTypeId)->createItem();
- }
- ItemType* BasicLightSourceBlockType::createItemType() const
- {
- return (new LightSourceItemType(getItemTypeName(),
- new ModelInfo(zModel()->getModelPath(),
- zModel()->getTexturePaths(),
- zModel()->isTransparent(),
- zModel()->getSize() / 2.f),
- getName(),
- 50,
- getGroupNames()))
- ->setColor(0x00F69A54);
- }
- void BasicLightSourceBlockType::setItemTypeName(Framework::Text itemTypeName)
- {
- this->itemTypeName = itemTypeName;
- }
- Framework::Text BasicLightSourceBlockType::getItemTypeName() const
- {
- return itemTypeName;
- }
- void BasicLightSourceBlockType::setTransparent(bool transparent)
- {
- this->transparent = transparent;
- }
- bool BasicLightSourceBlockType::isTransparent() const
- {
- return transparent;
- }
- void BasicLightSourceBlockType::setPassable(bool passable)
- {
- this->passable = passable;
- }
- bool BasicLightSourceBlockType::isPassable() const
- {
- return passable;
- }
- void BasicLightSourceBlockType::setInteractable(bool interactable)
- {
- this->interactable = interactable;
- }
- bool BasicLightSourceBlockType::isInteractable() const
- {
- return interactable;
- }
- void BasicLightSourceBlockType::setLightColor(int lightColor)
- {
- this->lightColor = lightColor;
- }
- int BasicLightSourceBlockType::getLightColor() const
- {
- return lightColor;
- }
- BasicLightSourceBlockTypeFactory::BasicLightSourceBlockTypeFactory()
- : BlockTypeFactoryBase()
- {}
- BasicLightSourceBlockType* BasicLightSourceBlockTypeFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new BasicLightSourceBlockType();
- }
- void BasicLightSourceBlockTypeFactory::fromJson(
- BasicLightSourceBlockType* zResult,
- Framework::JSON::JSONObject* zJson) const
- {
- zResult->setItemTypeName(zJson->zValue("itemType")->asString()->getString());
- zResult->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
- zResult->setPassable(zJson->zValue("passable")->asBool()->getBool());
- zResult->setInteractable(
- zJson->zValue("interactable")->asBool()->getBool());
- zResult->setLightColor(
- (int)zJson->zValue("lightColor")->asString()->getString());
- BlockTypeFactoryBase::fromJson(zResult, zJson);
- }
- void BasicLightSourceBlockTypeFactory::toJson(
- BasicLightSourceBlockType* zObject,
- Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue("itemType",
- new Framework::JSON::JSONString(zObject->getItemTypeName()));
- zResult->addValue(
- "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
- zResult->addValue(
- "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
- zResult->addValue("interactable",
- new Framework::JSON::JSONBool(zObject->isInteractable()));
- zResult->addValue("lightColor",
- new Framework::JSON::JSONString(zObject->getLightColor()));
- BlockTypeFactoryBase::toJson(zObject, zResult);
- }
- JSONObjectValidationBuilder* BasicLightSourceBlockTypeFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return BlockTypeFactoryBase::addToValidator(
- builder->withRequiredString("itemType")
- ->finishString()
- ->withRequiredBool("transparent")
- ->withDefault(true)
- ->finishBool()
- ->withRequiredBool("passable")
- ->withDefault(true)
- ->finishBool()
- ->withRequiredBool("interactable")
- ->withDefault(true)
- ->finishBool()
- ->withRequiredString("lightColor")
- ->finishString());
- }
- Framework::Text BasicLightSourceBlockTypeFactory::getTypeToken() const
- {
- return "lightSource";
- }
|