123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #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(
- Framework::Text itemTypeName,
- ModelInfo* model,
- Framework::Text name,
- float hardness,
- int mapColor,
- int lightColor,
- Framework::RCArray<Framework::Text> groupNames)
- : BlockType(0, model, 1, 1, 1, name, false, mapColor, groupNames, hardness),
- itemTypeName(itemTypeName),
- transparent(1),
- passable(1),
- interactable(1),
- lightColor(lightColor)
- {}
- 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();
- }
- int BasicLightSourceBlockType::getLightColor() const
- {
- return lightColor;
- }
- Framework::Text BasicLightSourceBlockType::getItemTypeName() const
- {
- return itemTypeName;
- }
- 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);
- }
- BasicLightSourceBlockTypeFactory::BasicLightSourceBlockTypeFactory()
- : SubTypeFactory()
- {}
- BasicLightSourceBlockType* BasicLightSourceBlockTypeFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<Framework::Text> groupNames;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("groupNames")->asArray())
- {
- groupNames.add(new Framework::Text(value->asString()->getString()));
- }
- return new BasicLightSourceBlockType(
- zJson->zValue("itemType")->asString()->getString(),
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- zJson->zValue("model")),
- zJson->zValue("name")->asString()->getString(),
- (float)zJson->zValue("hardness")->asNumber()->getNumber(),
- (int)zJson->zValue("mapColor")->asString()->getString(),
- (int)zJson->zValue("lightColor")->asString()->getString(),
- groupNames);
- }
- Framework::JSON::JSONObject* BasicLightSourceBlockTypeFactory::toJson(
- BasicLightSourceBlockType* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("itemType",
- new Framework::JSON::JSONString(zObject->getItemTypeName()));
- result->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
- result->addValue(
- "name", new Framework::JSON::JSONString(zObject->getName()));
- result->addValue(
- "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
- result->addValue(
- "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
- result->addValue("lightColor",
- new Framework::JSON::JSONString(zObject->getLightColor()));
- Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
- for (Framework::Text* groupName : zObject->getGroupNames())
- {
- groupNames->addValue(new Framework::JSON::JSONString(*groupName));
- }
- result->addValue("groupNames", groupNames);
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- BasicLightSourceBlockTypeFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredString("itemType")
- ->finishString()
- ->withRequiredAttribute(
- "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredNumber("hardness")
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredString("mapColor")
- ->finishString()
- ->withRequiredString("lightColor")
- ->finishString()
- ->withRequiredArray("groupNames")
- ->withDefault(new Framework::JSON::JSONArray())
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text BasicLightSourceBlockTypeFactory::getTypeToken() const
- {
- return "lightSource";
- }
|