EntityRef EntityManager::createEntityRef(int ID) { if (!entityExists(ID)) // Entity doesn't exist, return a null EntityRef { return EntityRef(this, EntityRef::NullID); } return EntityRef(this, ID); }
void EntityProperty::set(const Atlas::Message::Element & val) { // INT id? if (val.isString()) { const std::string & id = val.String(); if (m_data.get() == 0 || m_data->getId() != id) { debug(std::cout << "Assigning " << id << std::endl << std::flush;); if (id.empty()) { m_data = EntityRef(0); } else { LocatedEntity * e = BaseWorld::instance().getEntity(id); if (e != 0) { debug(std::cout << "Assigned" << std::endl << std::flush;); m_data = EntityRef(e); }
virtual void Initialize(NodeArguments* args) { auto entity = args->GetInput<EntityRef>(0); if (entity->IsValid()) { auto child_index = *args->GetInput<int>(1); TransformData* transform_data = transform_component_->GetComponentData(*entity); // Find the child at the given index. auto iter = transform_data->children.begin(); while (child_index && iter != transform_data->children.end()) { ++iter; --child_index; } args->SetOutput(0, iter != transform_data->children.end() ? iter->owner : EntityRef()); } else { args->SetOutput(0, EntityRef()); } }
EntityRef SplashState::loadEntity(const Path& path, EntityRef parent, const Path& cd) { Path localPath = make_absolute(cd, path); log().info("Load entity \"", localPath, "\""); Json::Value json; Path realPath = game()->dataPath() / localPath; if(!parseJson(json, realPath, localPath, log())) { return EntityRef(); } return _entities.createEntityFromJson(parent, json, localPath.dir()); }
void WorldViewer::update(double time_diff) { FWK_PROFILE("WorldViewer::update"); Actor *spectator = m_world->refEntity<Actor>(m_spectator); if((int)m_entities.size() != m_world->entityCount()) m_entities.resize(m_world->entityCount()); if(!m_spectator) { for(int n = 0; n < (int)m_entities.size(); n++) { Entity *entity = m_world->refEntity(n); VisEntity &vis_entity = m_entities[n]; vis_entity.ref = entity? entity->ref() : EntityRef(); vis_entity.mode = entity? VisEntity::visible : VisEntity::invisible; vis_entity.occluder_id = -1; } return; } if(!spectator) return; FBox bbox = spectator->boundingBox(); m_cur_pos = bbox.center(); m_eye_pos = asXZY(m_cur_pos.xz(), bbox.min.y + bbox.height() * 0.75f); for(int n = 0; n < (int)m_entities.size(); n++) { Entity *entity = m_world->refEntity(n); VisEntity &vis_entity = m_entities[n]; if(!entity) { vis_entity = VisEntity(); continue; } const auto *desc = m_world->refEntityDesc(n); DASSERT(desc); bool is_visible = m_see_all || spectator == entity || spectator->canSee(entity->ref(), !isMovable(*entity)); if(vis_entity.ref != entity->ref()) { vis_entity = VisEntity(); vis_entity.ref = entity->ref(); vis_entity.mode = is_visible? VisEntity::visible : VisEntity::invisible; } if(is_visible) { vis_entity.occluder_id = desc->occluder_id; if(vis_entity.mode == VisEntity::visible) continue; if(vis_entity.mode == VisEntity::shadowed || vis_entity.mode == VisEntity::pre_blending_out) { vis_entity.shadow.reset(); vis_entity.mode = VisEntity::visible; } else { float blend_value = 0.0; if(vis_entity.mode == VisEntity::blending_in) blend_value = vis_entity.blend_value; else if(vis_entity.mode == VisEntity::blending_out) blend_value = blend_time - vis_entity.blend_value; blend_value += time_diff; vis_entity.mode = blend_value > blend_time? VisEntity::visible : VisEntity::blending_in; vis_entity.blend_value = blend_value; } } else { if(vis_entity.mode == VisEntity::shadowed || vis_entity.mode == VisEntity::invisible) continue; if(vis_entity.mode == VisEntity::visible) { vis_entity.blend_value = 0.0f; vis_entity.mode = VisEntity::pre_blending_out; } if(vis_entity.mode == VisEntity::pre_blending_out) { vis_entity.blend_value += time_diff; if(vis_entity.blend_value > blend_time) { if(isMovable(*entity)) { vis_entity.mode = VisEntity::blending_out; vis_entity.blend_value = vis_entity.blend_value - blend_time; } else { vis_entity.mode = VisEntity::shadowed; vis_entity.occluder_id = desc->occluder_id; vis_entity.shadow.reset(entity->clone()); } } } else { float blend_value = 0.0f; if(vis_entity.mode == VisEntity::blending_in) blend_value = blend_time - vis_entity.blend_value; else if(vis_entity.mode == VisEntity::blending_out) blend_value = vis_entity.blend_value; vis_entity.blend_value = blend_value + time_diff; vis_entity.mode = VisEntity::blending_out; if(vis_entity.blend_value > blend_time) vis_entity.mode = VisEntity::invisible; } } } if(m_occluder_config.update(spectator->boundingBox())) m_world->tileMap().updateVisibility(m_occluder_config); }
Box2 CollisionComponent::worldAlignedBox() const { lairAssert(_shape && _shape->type() == SHAPE_ALIGNED_BOX); Vector2 pos = EntityRef(_entityPtr).computeWorldTransform().translation().head<2>(); return Box2(pos + _shape->point(0), pos + _shape->point(1)); }
EntityRef World::toEntityRef(int index) const { Entity *entity = const_cast<World*>(this)->refEntity(index); return entity? entity->ref() : EntityRef(); }
EntityRef World::toEntityRef(ObjectRef ref) const { Entity *entity = const_cast<World*>(this)->refEntity(ref); return entity? entity->ref() : EntityRef(); }
EntityRef EntityWorldProxy::ref() const { return EntityRef(m_index, m_unique_id); }
void MainState::initialize() { _loop.reset(); _loop.setTickDuration( 1000000000 / 60); _loop.setFrameDuration( 1000000000 / 60); _loop.setMaxFrameDuration(_loop.frameDuration() * 3); _loop.setFrameMargin( _loop.frameDuration() / 2); _game->window()->onResize.connect(std::bind(&MainState::layoutScreen, this)) .track(_slotTracker); layoutScreen(); _menuInputs.up = _inputs.addInput("up"); _menuInputs.down = _inputs.addInput("down"); // _menuInputs.left = _inputs.addInput("left"); // _menuInputs.right = _inputs.addInput("right"); _menuInputs.ok = _inputs.addInput("ok"); _menuInputs.cancel = _inputs.addInput("cancel"); _inputs.mapScanCode(_menuInputs.up, SDL_SCANCODE_UP); _inputs.mapScanCode(_menuInputs.down, SDL_SCANCODE_DOWN); // _inputs.mapScanCode(_menuInputs.left, SDL_SCANCODE_LEFT); // _inputs.mapScanCode(_menuInputs.right, SDL_SCANCODE_RIGHT); _inputs.mapScanCode(_menuInputs.ok, SDL_SCANCODE_RIGHT); _inputs.mapScanCode(_menuInputs.ok, SDL_SCANCODE_RETURN); _inputs.mapScanCode(_menuInputs.ok, SDL_SCANCODE_SPACE); _inputs.mapScanCode(_menuInputs.cancel, SDL_SCANCODE_LEFT); _inputs.mapScanCode(_menuInputs.cancel, SDL_SCANCODE_ESCAPE); //TODO: Remove cheats. _debugInput = _inputs.addInput("Debug"); _inputs.mapScanCode(_debugInput, SDL_SCANCODE_F1); _fontJson = _game->sys()->loader().getJson("8-bit_operator+_regular_23.json"); _fontTex = _game->renderer()->getTexture(_fontJson["file"].asString(), Texture::NEAREST | Texture::REPEAT); _font.reset(new Font(_fontJson, _fontTex)); _font->baselineToTop = 12; _bgSprite = loadSprite("bg.png"); _healthEmptySprite = loadSprite("health_bar_empty.png"); _healthFullSprite = loadSprite("health_bar_full.png"); _menuBgSprite = loadSprite("menu.png", 3, 3); _statusSprite = loadSprite("status.png", 3, 1); _bossSprite[0] = loadSprite("BigBoss1.png"); _bossSprite[1] = loadSprite("BigBoss2.png"); _bossSprite[2] = loadSprite("BigBoss3.png"); _pcSprite[0] = loadSprite("GTP.png"); _pcSprite[1] = loadSprite("MB.png"); _pcSprite[2] = loadSprite("MN.png"); _pcSprite[3] = loadSprite("Ninja.png"); _tombSprite = loadSprite("tomb.png"); _music1 = _game->audio()->loadMusic(_game->dataPath() / "music1.ogg"); _music2 = _game->audio()->loadMusic(_game->dataPath() / "music2.ogg"); _music3 = _game->audio()->loadMusic(_game->dataPath() / "music3.ogg"); _transition1 = _game->audio()->loadMusic(_game->dataPath() / "transition1.ogg"); _transition2 = _game->audio()->loadMusic(_game->dataPath() / "transition2.ogg"); _hitSound = _game->audio()->loadSound(_game->dataPath() / "hit.ogg"); _spellSound = _game->audio()->loadSound(_game->dataPath() / "spell.ogg"); _healSound = _game->audio()->loadSound(_game->dataPath() / "heal.ogg"); _pcDeathSound = _game->audio()->loadSound(_game->dataPath() / "death_pc.ogg"); _bossDeathSound = _game->audio()->loadSound(_game->dataPath() / "death_boss.ogg"); _victorySound = _game->audio()->loadSound(_game->dataPath() / "boss_victory.ogg"); _damageAnim.reset(new MoveAnim(ONE_SEC/2, Vector3(0, 30, 0), RELATIVE)); _damageAnim->onEnd = [this](_Entity* e) { _entities.destroyEntity(EntityRef(e)); }; _deathAnim.reset(new SwapSpriteAnim(ONE_SEC/10, &_tombSprite, 9)); _boss0to1Anim.reset(new Sequence); _boss0to1Anim->anims.push_back(new MoveAnim(ONE_SEC, Vector3(-300, 0, 0), RELATIVE)); _boss0to1Anim->anims.push_back(new MoveAnim(ONE_SEC, Vector3(+300, 0, 0), RELATIVE)); _boss0to1Anim->anims[0]->onEnd = [this](_Entity* e) { e->sprite->setSprite(&_bossSprite[1]); }; _boss1to2Anim.reset(new Sequence); _boss1to2Anim->anims.push_back(new MoveAnim(ONE_SEC, Vector3(-300, 0, 0), RELATIVE)); _boss1to2Anim->anims.push_back(new MoveAnim(ONE_SEC, Vector3(+280, 0, 0), RELATIVE)); _boss1to2Anim->anims[0]->onEnd = [this](_Entity* e) { e->sprite->setSprite(&_bossSprite[2]); }; _statusFrame.reset(new Frame(&_menuBgSprite, Vector2(640, 120))); _statusFrame->position = Vector3(0, 0, -.05); _messageMargin = 12; _messageOutMargin = 8; _messageFrame.reset(new Frame(&_menuBgSprite, Vector2(640 - 2 * _messageOutMargin, 0))); _messageFrame->position = Vector3(_messageOutMargin, 0, .9); _mainMenu.reset(new Menu(&_menuBgSprite, _font.get(), &_menuInputs)); _switchMenu.reset(new Menu(&_menuBgSprite, _font.get(), &_menuInputs, closeMenuFunc())); _spellMenu.reset(new Menu(&_menuBgSprite, _font.get(), &_menuInputs, closeMenuFunc())); _summonMenu.reset(new Menu(&_menuBgSprite, _font.get(), &_menuInputs, closeMenuFunc())); _pcMenu.reset(new Menu(&_menuBgSprite, _font.get(), &_menuInputs, closeMenuFunc())); _mainMenu->addEntry("Attack", Menu::ENABLED, openMenuFunc(_pcMenu.get(), _mainMenu.get(), 0)); _mainMenu->addEntry("Switch", Menu::ENABLED, openMenuFunc(_switchMenu.get(), _mainMenu.get(), 1)); _mainMenu->addEntry("Curse", Menu::ENABLED, openMenuFunc(_spellMenu.get(), _mainMenu.get(), 2)); // _mainMenu->addEntry("Summon", Menu::ENABLED, // openMenuFunc(_summonMenu.get(), _mainMenu.get(), 3)); // _mainMenu->addEntry("Scan"); // _mainMenu->addEntry("QTE", Menu::HIDDEN); // _mainMenu->addEntry("Twist 1", Menu::HIDDEN); _mainMenu->layout(); _mainMenu->show(Vector3(0, 0, 0)); _switchMenu->addEntry("None", Menu::ENABLED, doActionFunc()); _switchMenu->addEntry("Fire", Menu::ENABLED, doActionFunc()); _switchMenu->addEntry("Ice", Menu::ENABLED, doActionFunc()); _switchMenu->addEntry("Thunder", Menu::ENABLED, doActionFunc()); _switchMenu->addEntry("Acid", Menu::ENABLED, doActionFunc()); _switchMenu->layout(); _switchMenu->show(Vector3(_mainMenu->width(), 16, .1)); _spellMenu->addEntry("Storm", Menu::ENABLED, doActionFunc()); _spellMenu->addEntry("Strike", Menu::ENABLED, openMenuFunc(_pcMenu.get(), _spellMenu.get(), 1)); _spellMenu->addEntry("Crippling strike", Menu::ENABLED, openMenuFunc(_pcMenu.get(), _spellMenu.get(), 2)); _spellMenu->addEntry("Soul drain", Menu::ENABLED, openMenuFunc(_pcMenu.get(), _spellMenu.get(), 3)); _spellMenu->addEntry("Vorpal sword", Menu::ENABLED, openMenuFunc(_pcMenu.get(), _spellMenu.get(), 4)); _spellMenu->addEntry("Mud pit", Menu::ENABLED, doActionFunc()); _spellMenu->addEntry("Dispel magic", Menu::ENABLED, openMenuFunc(_pcMenu.get(), _spellMenu.get(), 6)); _spellMenu->layout(); _spellMenu->show(Vector3(_mainMenu->width(), 16, .1)); _summonMenu->addEntry("Sprites", Menu::ENABLED, doActionFunc()); _summonMenu->addEntry("Tomberry", Menu::ENABLED, doActionFunc()); _summonMenu->addEntry("Mageling", Menu::ENABLED, doActionFunc()); _summonMenu->layout(); _summonMenu->show(Vector3(_mainMenu->width(), 16, .1)); _pcMenu->addEntry("", Menu::ENABLED, doActionFunc()); _pcMenu->addEntry("", Menu::ENABLED, doActionFunc()); _pcMenu->addEntry("", Menu::ENABLED, doActionFunc()); _pcMenu->addEntry("", Menu::ENABLED, doActionFunc()); _pcMenu->layout(); _pcMenu->show(Vector3(_mainMenu->width(), 16, .2)); _rules.setFromJson(_game->sys()->loader().getJson("rules.json")); _initialized = true; }
void checkSignal() { { // Check the assignment operator causes the signal to fire emitted = false; Entity e("1", 1); EntityRef ref; assert(emitted == false); ref.Changed.connect(sigc::ptr_fun(&signal_emitted)); assert(emitted == false); ref = EntityRef(&e); assert(ref.get() == &e); assert(emitted == true); } { // Check the assignment operator does not cause the signal to fire // the the pointer is unchanged emitted = false; Entity e("1", 1); EntityRef ref(&e); assert(emitted == false); ref.Changed.connect(sigc::ptr_fun(&signal_emitted)); assert(emitted == false); ref = EntityRef(&e); assert(ref.get() == &e); assert(emitted == false); } { // Check that destroying the Entity makes the reference null. emitted = false; Entity e("1", 1); Entity * container = new Entity("2", 2); // Set the location of the entity being tested, as destroy requires it. e.m_location.m_loc = container; // Make sure the container has a contains structure, as destroy // requires it. container->m_contains = new LocatedEntitySet; // Increment the refcount on the container, else the tested Entity's // destructor will delete it. container->incRef(); EntityRef ref(&e); assert(emitted == false); ref.Changed.connect(sigc::ptr_fun(&signal_emitted)); assert(ref.get() == &e); assert(emitted == false); e.destroy(); assert(ref.get() == 0); assert(emitted == true); } }
int main() { { // Check the default constructor EntityRef ref; } { // Check the default constructor initialises to NULL via get EntityRef ref; assert(ref.get() == 0); } { // Check the default constructor initialises to NULL via dereference EntityRef ref; assert(&(*ref) == 0); } { // Check the default constructor initialises to NULL via -> EntityRef ref; assert(ref.operator->() == 0); } { // Check the default constructor initialises to NULL via == EntityRef ref; assert(ref == 0); } { // Check the initialising constructor via get Entity * e = new Entity("1", 1); EntityRef ref(e); assert(ref.get() == e); } { // Check the initialising constructor via dereference Entity * e = new Entity("1", 1); EntityRef ref(e); assert(&(*ref) == e); } { // Check the initialising constructor via -> Entity * e = new Entity("1", 1); EntityRef ref(e); assert(ref.operator->() == e); } { // Check the initialising constructor via == Entity * e = new Entity("1", 1); EntityRef ref(e); assert(ref == e); } { // Check the copy constructor Entity * e = new Entity("1", 1); EntityRef ref(e); EntityRef ref2(ref); assert(ref2.get() == e); } { // Check the comparison operator Entity * e = new Entity("1", 1); EntityRef ref(e); EntityRef ref2(e); assert(ref == ref2); } { // Check the comparison operator Entity * e = new Entity("1", 1); Entity * e2 = new Entity("2", 2); EntityRef ref(e); EntityRef ref2(e2); assert(!(ref == ref2)); } #if 0 // These tests should be included should we add operator!= { // Check the comparison operator Entity e("1", 1); EntityRef ref(&e); EntityRef ref2(&e); assert(!(ref != ref2)); } { // Check the comparison operator Entity e("1", 1); Entity e2("2", 2); EntityRef ref(&e); EntityRef ref2(&e2); assert(ref != ref2); } #endif { // Check the less than operator Entity * e = new Entity("1", 1); EntityRef ref(e); EntityRef ref2(e); assert(!(ref < ref2) && !(ref2 < ref)); } { // Check the less than operator Entity * e = new Entity("1", 1); Entity * e2 = new Entity("2", 2); EntityRef ref(e); EntityRef ref2(e2); assert(ref < ref2 || ref2 < ref); } { // Check the assignment operator Entity * e = new Entity("1", 1); EntityRef ref; ref = EntityRef(e); assert(ref.get() == e); } { // Check that destroying the Entity makes the reference null. Entity e("1", 1); Entity * container = new Entity("2", 2); // Set the location of the entity being tested, as destroy requires it. e.m_location.m_loc = container; // Make sure the container has a contains structure, as destroy // requires it. container->m_contains = new LocatedEntitySet; // Increment the refcount on the container, else the tested Entity's // destructor will delete it. container->incRef(); EntityRef ref(&e); assert(ref.get() == &e); e.destroy(); assert(ref.get() == 0); } checkSignal(); }