Exemplo n.º 1
0
// ***************************************************************************
int main( int argc, char *argv[] )
{
    std::cout << "__WINDOW" << std::endl;
    GLEngine app( "Simulateur", 800, 800);

    std::cout << "__ENVIRONMENT" << std::endl;
    Environment env(3);
    // Ajout d'Entity
    EntityPtr item1 = EntityPtr( new Wall( {1,1} ));
    env.add( item1 );
    EntityPtr item2 = EntityPtr( new Algae( {-1,1} ));
    env.add( item2 );
    EntityPtr item3 = EntityPtr( new Wall( {2,0} ));
    env.add( item3 );
    EntityPtr item4 = EntityPtr( new Food( {0,-2} ));
    env.add( item4 );

    std::cout << "__AGENT" << std::endl;
    Agent agent;

    std::cout << "__SIMULATOR" << std::endl;
    Simu simu( env, agent );
    GLSimu simu_screen( app, simu );

    std::cout << "__GO" << std::endl;
    app.run<GLSimu,bool>( simu_screen );

    return 0;
}
Exemplo n.º 2
0
	void UpdateBot(AgentStatus status, IEnvironment* env) {
		if(status.left) {
			shape->Rotate(-3);
		}
		if(status.right) {
			shape->Rotate(3);
		}
		float rot = shape->GetRotation();
		Vector2d vel = Vector2d(cos(rot*PI/180),sin(rot*PI/180));
		
		if(status.forward) {
			collide = false;
			EntityPtr ent = env->CircleIntersect(GetPosition() + vel, BOT_SIZE, 0);
			collide = true;
			if(ent == EntityPtr())
				shape->Move(vel);
		}
		if(status.fire) {
			uint32_t ntime = env->GetTicks();
			if(ntime - last_fire > MISSLE_COOLDOWN) {
				EntityPtr self = env->GetSmart(this);
				if(self == EntityPtr()) {
					printf("Failed to fire a missle. Missing smart pointer;");
					return;
				}
				EntityPtr missle(CreateMissle(self, GetPosition(), rot, env->GetDrawer()));
				if(self == EntityPtr()) {
					printf("Failed to fire a missle. Failed to allocate;");
					return;
				}
				env->AddEntity(missle);
				last_fire = ntime;
			}
		}
	}
Exemplo n.º 3
0
// void render(Entity& e)
// {
//   std::cout << "render(Entity& e) : " << w.str_dump() << std::endl;
// }
// ********************************************************************** MAIN
int main( int argc, char *argv[] )
{
  std::list<EntityPtr> l_entity;
  EntityPtr item1 = EntityPtr( new Entity( {1,1} ));
  l_entity.push_back( item1 );
  EntityPtr item2 = EntityPtr( new Wall( {1,2} ));
  l_entity.push_back( item2 );
  EntityPtr item3 = EntityPtr( new Algae( {1,3} ));
  l_entity.push_back( item3 );

  for( auto& item: l_entity) {
    std::cout << item->str_dump() << std::endl;
    // dynamic_cast en cas de polymorphisme
    // sinon un static_cast marcherait aussi.
    if( Wall* w = dynamic_cast<Wall*>(item.get()) ) {
      render(w);
      render(*w);
    }
    else {
      render(item);
      render(*item);
    }
  }
  
  return 0;
}
Exemplo n.º 4
0
	void Update(IEnvironment* env) {
		float rot = shape->GetRotation();
		Vector2d vel(cos(rot * PI/180), sin(rot * PI/180));
		vel = vel * MISSLE_SPEED;
		Vector2d pos = GetPosition();

		Vector2d lt,rt,lb,rb;
		GetLines(lt, rt, lb, rb);

		collide = false;
		((Bot*)owner.get())->collide = false;
		int dummy;
		EntityPtr ent = env->LineIntersect(pos, pos+vel, MISSLE_LENGTH, &dummy);
		collide = true;
		((Bot*)owner.get())->collide = true;

		if(ent == EntityPtr()) {
			shape->Move(vel);
		}else {
			EntityList entities = env->QueueEntities
				(pos + vel.GetNormalized() * MISSLE_LENGTH, MISSLE_AOE, ET_UNKNOWN);
			EntityList::iterator i = entities.begin();
			EntityList::iterator end = entities.end();
			while(i != end) {
				env->DamageEntity(*i, MISSLE_DMG);
				++i;
			}
		}
	}
Exemplo n.º 5
0
void CameraApplication::CreateCamera()
{
    if (!lastScene_)
        return;

    StringVector components;
    components.Push("Placeable");
    components.Push("Camera");
    Entity* cameraEntity = lastScene_->CreateEntity(0, components, AttributeChange::LocalOnly, false, false, true);
    if (!cameraEntity)
    {
        LogError("CameraApplication::CreateCamera: failed to create camera entity");
        return;
    }
    cameraEntity->SetName("FreeLookCamera");
    IRenderer* renderer = framework->Renderer();
    if (!renderer)
    {
        LogError("CameraApplication::CreateCamera: can not assign camera; no renderer assigned");
        return;
    }
    renderer->SetMainCamera(cameraEntity);
    TundraLogic* logic = framework->Module<TundraLogic>();
    if (logic)
        logic->SyncManager()->SetObserver(EntityPtr(cameraEntity));

    lastCamera_ = cameraEntity;

    lastScene_->EntityCreated.Connect(this, &CameraApplication::CheckCameraSpawnPos);

    CheckCameraSpawnPos(lastScene_->EntityByName("FreeLookCameraSpawnPos"), AttributeChange::Default);
}
Exemplo n.º 6
0
std::vector<EntityPtr> EntityMgr::create(Entity proto, size_t number)
{

	std::vector<EntityPtr> entities;
   	size_t added_entities;

	//First, we search for available slots among already allocated ones
	for(added_entities = 0; added_entities < number && added_entities < _freeSlots.size(); added_entities++)
	{
		entities.push_back(EntityPtr(*this, _entities.size()));
		_entities[_freeSlots[added_entities]] = proto;
	}

	//Then, if we still have entities left to create, we allocate space for them
	for(size_t i=added_entities; i<number; i++)
	{
		entities.push_back(EntityPtr(*this, _entities.size()));
		_entities.push_back(proto);
	}
		
	return entities;
	
}
Exemplo n.º 7
0
EntityVector GraphicsWorld::FrustumQuery(const Urho3D::IntRect &viewrect) const
{
    URHO3D_PROFILE(GraphicsWorld_FrustumQuery);
    
    EntityVector ret;
    Camera* cameraComp = renderer_->MainCameraComponent();
    if (!cameraComp || cameraComp->ParentScene() != scene_)
        return ret;
     Urho3D::Camera* cam = cameraComp->UrhoCamera();
     if (!cam)
        return ret;

    int width = renderer_->WindowWidth();
    int height = renderer_->WindowHeight();
    float w = (float)width;
    float h = (float)height;
    float left = (float)(viewrect.left_) / w, right = (float)(viewrect.right_) / w;
    float top = (float)(viewrect.top_) / h, bottom = (float)(viewrect.bottom_) / h;
    if (left > right) std::swap(left, right);
    if (top > bottom) std::swap(top, bottom);
    // don't do selection if box is too small
    if ((right - left) * (bottom - top) < 0.0001)
        return ret;

    Urho3D::Frustum fr;
    fr.vertices_[0] = cam->ScreenToWorldPoint(Urho3D::Vector3(right, top, cam->GetNearClip()));
    fr.vertices_[1] = cam->ScreenToWorldPoint(Urho3D::Vector3(right, bottom, cam->GetNearClip()));
    fr.vertices_[2] = cam->ScreenToWorldPoint(Urho3D::Vector3(left, bottom, cam->GetNearClip()));
    fr.vertices_[3] = cam->ScreenToWorldPoint(Urho3D::Vector3(left, top, cam->GetNearClip()));
    fr.vertices_[4] = cam->ScreenToWorldPoint(Urho3D::Vector3(right, top, cam->GetFarClip()));
    fr.vertices_[5] = cam->ScreenToWorldPoint(Urho3D::Vector3(right, bottom, cam->GetFarClip()));
    fr.vertices_[6] = cam->ScreenToWorldPoint(Urho3D::Vector3(left, bottom, cam->GetFarClip()));
    fr.vertices_[7] = cam->ScreenToWorldPoint(Urho3D::Vector3(left, top, cam->GetFarClip()));
    fr.UpdatePlanes();

    Urho3D::PODVector<Urho3D::Drawable*> result;
    Urho3D::FrustumOctreeQuery query(result, fr, Urho3D::DRAWABLE_GEOMETRY);
    urhoScene_->GetComponent<Urho3D::Octree>()->GetDrawables(query);

    for (Urho3D::PODVector<Urho3D::Drawable*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
    {
        Entity* entity = static_cast<Entity*>((*i)->GetNode()->GetVar(entityLink).GetPtr());
        if (entity)
            ret.Push(EntityPtr(entity));
    }

    return ret;
}
Exemplo n.º 8
0
EntityPtr EntityReference::Lookup(Scene* scene) const
{
    if (!scene)
        return EntityPtr();
    // If ref looks like an ID, lookup by ID first
    bool ok = false;
    entity_id_t id = ref.toInt(&ok);
    if (ok)
    {
        EntityPtr entity = scene->GetEntity(id);
        if (entity)
            return entity;
    }
    // Then get by name
    return scene->GetEntityByName(ref.trimmed());
}
Exemplo n.º 9
0
bool Placeable::IsGrandparentOf(Entity *entity) const
{
    if (!entity)
    {
        LogError("Placeable::IsGrandParentOf: called with null pointer.");
        return false;
    }
    if (!ParentEntity())
        return false;
    if (entity == ParentEntity())
        return true;

    EntityVector allChildren = Grandchildren(ParentEntity());
    EntityVector::ConstIterator iter = allChildren.Find(EntityPtr(entity));
    return iter != allChildren.End();
}
Exemplo n.º 10
0
void ECBrowser::AddEntity(EntityPtr entity)
{
    PROFILE(ECBrowser_AddNewEntity);

    assert(entity);
    if(!entity)
        return;

    //If entity is already added to browser no point to continue. 
    if(HasEntity(entity))
        return;
    entities_.push_back(EntityPtr(entity));

    connect(entity.get(), SIGNAL(ComponentAdded(IComponent*, AttributeChange::Type)),
        SLOT(OnComponentAdded(IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
    connect(entity.get(), SIGNAL(ComponentRemoved(IComponent*, AttributeChange::Type)),
        SLOT(OnComponentRemoved(IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
}
Exemplo n.º 11
0
EntityPtr EntityPalette::Create( std::string type, std::string id )
{
	EntityDefPtr def = GetDefinition(type);
	if (!def)
		return EntityPtr();

	Body* body;
	if (def->GetBodyDefinition()->Type == BodyDef::SimpleBody)
	{ 
		body = new SimpleBody();
	}
	else if (def->GetBodyDefinition()->Type == BodyDef::Box2DBody)
	{
		body = new Box2DBody(def->GetBodyDefinition());
	}

	GraphicsPtr graphics	= def->GetGraphicsDefinition()->Create();
	BehaviourPtr behaviour	= def->GetBehaviourDefinition()->Create();

	EntityPtr entity(new Entity());
	entity->mGraphics = graphics;
	entity->mBehaviour = behaviour;
	entity->mBody = body;

	graphics->mEntity = entity;
	behaviour->mActor = entity;
	body->SetEntity(entity.get());

	if (id == "")
	{
		std::string prefix = "GameObject_";
		int n = 0;
		while (mEntites.find(prefix + itoa(n, 16)) != mEntites.end())
			++n;
		id = prefix + itoa(n, 16);
	}

	entity->mName = id;
	entity->mType = def->GetType();

	mEntites[id] = entity;

	return entity;
}
Exemplo n.º 12
0
	Entity& EntityManager::createEntity()
	{
		// Retrieve recycled Entity or allocate a new one if there's no
		// recycled entities available
		EntityPtr entity = !m_removedAndAvailable.empty() ? 
			std::move(m_removedAndAvailable.back()) : 
			EntityPtr(new Entity(m_engine, m_uniqueEntityId++));

		if(!m_removedAndAvailable.empty())
			m_removedAndAvailable.pop_back();

		// So we have something to return, seeing how the Entity gets
		// moved before we reach the return statement.
		Entity *entityPtr = entity.get();

		entity->setStatus(true);
		m_entities[entity->getId()] = std::move(entity);

		return *entityPtr;
	}
Exemplo n.º 13
0
	EnvirInfo CalculateAbstractInfo(IEnvironment* env, Vector2d pos, float rotation) {
		EnvirInfo out;
		for(int i = 0; i < RAY_COUNT; ++i) {
			Vector2d orient = Vector2d(1,0);
			float angle = rotation + RAY_ANGLE / (RAY_COUNT - 1) * (i - RAY_COUNT / 2);
			orient.x = cos(angle *PI/180);
			orient.y = sin(angle *PI/180);
			EntityPtr ent = env->LineIntersect(pos, pos + orient, MAX_DIST, &out.ray_length[i]);
			if(ent != EntityPtr())
				out.ray_detail[i] = ent->Type();
			else
				out.ray_detail[i] = ET_NONE;

			if(out.ray_detail[i] == ET_BOT) {
				out.ray_udata[i] = ((Bot*)ent.get())->team;
			}
			rays[i] = pos + orient * out.ray_length[i];
			out.ray_pos[i].x = rays[i].x;
			out.ray_pos[i].y = rays[i].y;
		}
		return out;
	}
Exemplo n.º 14
0
EventsListWidget::EventsListWidget(QWidget *parent) :
    QListWidget(parent)
{
    m_pCurrentEntity = EntityPtr();
}
Exemplo n.º 15
0
EntityPtr Entity::Clone(bool local, bool temporary, const QString &cloneName, AttributeChange::Type changeType) const
{
    QDomDocument doc("Scene");
    QDomElement sceneElem = doc.createElement("scene");
    QDomElement entityElem = doc.createElement("entity");
    entityElem.setAttribute("sync", BoolToString(!local));
    entityElem.setAttribute("id", local ? scene_->NextFreeIdLocal() : scene_->NextFreeId());
    // Set the temporary status in advance so it's valid when Scene::CreateContentFromXml signals changes in the scene
    entityElem.setAttribute("temporary", BoolToString(temporary));
    // Setting of a new name for the clone is a bit clumsy, but this is the best way to do it currently.
    const bool setNameForClone = !cloneName.isEmpty();
    bool cloneNameWritten = false;
    for(ComponentMap::const_iterator i = components_.begin(); i != components_.end(); ++i)
    {
        i->second->SerializeTo(doc, entityElem);
        if (setNameForClone && !cloneNameWritten && i->second->TypeId() == EC_Name::ComponentTypeId)
        {
            // Now that we've serialized the Name component, overwrite value of the "name" attribute.
            QDomElement nameComponentElem = entityElem.lastChildElement();
            nameComponentElem.firstChildElement().setAttribute("value", cloneName);
            cloneNameWritten = true;
        }
    }
    sceneElem.appendChild(entityElem);
    doc.appendChild(sceneElem);

    QList<Entity *> newEntities = scene_->CreateContentFromXml(doc, true, changeType);
    return (!newEntities.isEmpty() && newEntities.first() ? newEntities.first()->shared_from_this() : EntityPtr());
}
Exemplo n.º 16
0
bool Buffered::Initialize( Renderer* renderer )
{
    // create offscreen
    int width(512);
    int height(512);
    bool r = m_FrameBuffer.Allocate( width, height );
    ASSERT( r, "Cannot allocate offscreen buffer!");
    m_Texture = m_FrameBuffer.GetTexture();

#if 0
    // Add a cube
    Viewport *viewport(new Viewport(width, height));
    viewport->SetClearColor( {1.0f, 1.0f, 1.0f } );
    AddEntity( EntityPtr(viewport) );

    EntityPtr particleEmitter( new Emitter(renderer) );
    particleEmitter->GetRenderState()->Translate( Vector(0.0, 0.0, -60), Vector(1.0f, 1.0f, 1.0f) );
    viewport->AddEntity( particleEmitter );
#endif
    boost::shared_ptr<BmpBrush> bmpBrush( new BmpBrush );
    ASSERT( bmpBrush->Load( "data/Floor_D.bmp"), "BMP Load error!" );

    ////////////////////////////////////////////////////////////////////////////
    // Compose our scene

    // Add a cube
    EntityPtr viewport(new Viewport(width, height));
    // this entity renders
    AddEntity(viewport);

    // Add the camera
    EntityPtr camera(new Camera(NULL));
    // this entity renders
    viewport->AddEntity(camera, 0);

    EntityPtr cube( new Cube() );
    cube->GetRenderState()->Translate( Vector(-8.0, 0, 10), Vector(2.0f, 2.0f, 2.0f) );
    cube->GetRenderState()->Rotate( Vector(0.0f, 0.0f, 0.0f ) );
    // this entity renders
    camera->AddEntity(cube, 20 );

    EntityPtr cube2( new Cube( bmpBrush ) );
    cube2->GetRenderState()->Translate( Vector(+8.0, 0, 10), Vector(2.0f, 2.0f, 2.0f) );
    cube2->GetRenderState()->Rotate( Vector(0.0f, 0.0f, 0.0f ) );
    // this entity renders
    camera->AddEntity(cube2, 20 );

    if ( --rec > 0 ) {
        // this creates a recursion
        EntityPtr buffered( new Cube );
        buffered->GetRenderState()->Translate( Vector(0.0, 0.0, 15.0), Vector(3.0f, 3.0f, 3.0f) );
        // this entity renders
        camera->AddEntity(buffered, 30 );

    }

#define PARTICLES
#ifdef PARTICLES
    EntityPtr particleEmitter( new Emitter( renderer ) );
    particleEmitter->GetRenderState()->Translate( Vector(0.0, 0.0, -30.0), Vector(1.0f, 1.0f, 1.0f) );
    // this entity renders
    camera->AddEntity(particleEmitter, 100 );
#endif

    // Subtree will be initialized here:
    return Cube::Initialize( renderer );
}
Exemplo n.º 17
0
EntityPtr EntityManager::add_entity(const std::string &name)
{
	auto entity = EntityPtr(new Entity(name));
	entities.push_back(entity);
	return entity;
}
Exemplo n.º 18
0
EntityPtr Query::GetEntity(int index)
{
	if (index < (int)mEntities.size())
		return mEntities.at(index);
	return EntityPtr();
}