Ejemplo n.º 1
0
void LifeForm::handle_event(const SDL_Event &event)
{
    auto world = m_world.lock();
    if (!world) {
        return;
    }

    if (event.type == SDL_MOUSEBUTTONDOWN) {
        if (event.button.button == SDL_BUTTON_LEFT) {
            const WorldRect viewport(world->get_viewport());
            WorldPosition pos = get_pos();
            WorldRect rect((int32_t)round(pos.x) - width/2,
                           (int32_t)round(pos.y) - height/2,
                           width, height);
            if (rect.contains(WorldPoint(event.button.x + viewport.x, event.button.y + viewport.y))) {
                set_focused(true);
            } else {
                set_focused(false);
            }
        } else if (focused() && event.button.button == SDL_BUTTON_RIGHT) {
            // Cancel m_lifeform's commands
            while (!m_commands.empty()) {
                m_commands.pop();
            }
            const WorldRect viewport(world->get_viewport());
            for (auto pt : world->get_path(get_pos(), WorldPosition(event.button.x + viewport.x,
                                                                 event.button.y + viewport.y))) {
                m_commands.emplace(new MoveCommand(this, WorldPosition(pt.x, pt.y)));
            }
        }
    }
}
Ejemplo n.º 2
0
Bool __fastcall TMissionEditor::OverStatic( int X, int Y,UniqueID& currUID)
{

	int	tlx=X-ICONW/2,	//The icons are centred on the world position already,
		tly=Y-ICONW/2,	//
		brx=X+ICONW/2,	//I am considering a box around the mouse to be equivalent
		bry=Y+ICONW/2;	//to a box around the icon

		WorldPosition(brx,bry);
		WorldPosition(tlx,tly);

	int	uid=currobj;
	info_base* i=*Persons2::ConvertPtrUID(UniqueID(uid));
	if (!i
		||	brx<i->World.X || bry>i->World.Z
		||	tlx>i->World.X || tly<i->World.Z)
	for(SWord b = 0; b < (int)MaxBANDNum;b++)
	{
		i=NULL;
		if (b!=WayPointBANDNum && staticbands[b].visible)
		{
			for(uid = staticbands [b].startband;
				uid < (int)staticbands [b].endband;
				uid ++)
			{
				i=*Persons2::ConvertPtrUID(UniqueID(uid));
				if (i
					&&	brx>i->World.X && bry<i->World.Z
					&&	tlx<i->World.X && tly>i->World.Z)
				{
					if (	(MissionEditor->loadedbfs[i->bfieldindex].viewable)
						&&  (SuperGroupViewable(uid))
						)

//						if (	(i->bfieldindex || canmodifymainworld)
 //							||	(displayworldtemplateitems)
  //							)
							{
								b=MaxBANDNum;
								break;
							}
				}
				i=NULL;
			}
		}
	}
	if (i)
	{
		currUID=uid;
		return(true);
	}
	else
		return(false);
}
Ejemplo n.º 3
0
bool SpatialNode::LookAt(const Vector3& target, const Vector3& up, TransformSpace space)
{
    SpatialNode* parentNode = SpatialParent();
    Vector3 worldSpaceTarget;

    switch (space)
    {
    case TS_LOCAL:
        worldSpaceTarget = WorldTransform() * target;
        break;

    case TS_PARENT:
        worldSpaceTarget = !parentNode ? target : parentNode->WorldTransform() * target;
        break;

    case TS_WORLD:
        worldSpaceTarget = target;
        break;
    }

    Vector3 lookDir = worldSpaceTarget - WorldPosition();
    // Check if target is very close, in that case can not reliably calculate lookat direction
    if (lookDir.Equals(Vector3::ZERO))
        return false;
    Quaternion newRotation;
    // Do nothing if setting look rotation failed
    if (!newRotation.FromLookRotation(lookDir, up))
        return false;

    SetWorldRotation(newRotation);
    return true;
}
Ejemplo n.º 4
0
// used to get our current WorldPosition
WorldPosition PseuGUI::GetWorldPosition(void)
{
    if(_scene && _scene->GetState() == SCENESTATE_WORLD)
    {
        return ((SceneWorld*)_scene)->GetWorldPosition();
    }
    return WorldPosition();
}
void mw2_Rect::Draw(mw2_Camera* camera)
{
	float x = WorldPosition()->x - camera->WorldPosition()->x;
	float y = WorldPosition()->y - camera->WorldPosition()->y;
	float teta = - camera->WorldRotation() * (float) PI / 180;

	float finalAngle = WorldRotation() - camera->WorldRotation();
	float finalX = (x * (float) cos(teta) - y * (float) sin(teta))
					+ mw2_Application::Settings::width / 2;
	float finalY = (x * (float) sin(teta) + y * (float) cos(teta))
					+ mw2_Application::Settings::height / 2;

	ALLEGRO_TRANSFORM T;
	al_identity_transform(&T);
	al_rotate_transform(&T, finalAngle * (float) PI / 180);
	al_translate_transform(&T, finalX, finalY);
	al_use_transform(&T);

	ALLEGRO_COLOR color = al_map_rgba(mColor->x, mColor->y, mColor->z, mColor->t);

	switch (mStyle)
	{
		case FILL:
			al_draw_filled_rectangle(
				- mSize->x /2, - mSize->y /2,
				mSize->x /2, mSize->y /2,
				color
			);
			break;
		case LINE:
			al_draw_rectangle(
				- mSize->x /2, - mSize->y /2,
				mSize->x /2, mSize->y /2,
				color,
				1
			);
			break;
	}

	al_identity_transform(&T);
	al_use_transform(&T);
}
Ejemplo n.º 6
0
void LifeForm::update(uint32_t elapsed)
{
    if (m_commands.empty() && !m_unvisited_tiles.empty()) {
        auto world = m_world.lock();
        if (!world) {
            return;
        }

        auto current_tile = world->location(get_pos());
        auto closest_tile = world->closest(current_tile, m_unvisited_tiles);
        if (current_tile == closest_tile) {
            m_unvisited_tiles.erase(current_tile);
            m_visited_tiles.insert(current_tile);
        } else {
            int x, y;
            std::tie(x, y) = closest_tile;
            for (auto pt : world->get_path(get_pos(),
                                           WorldPosition(x * TILE_WIDTH + TILE_WIDTH/2,
                                                         y * TILE_HEIGHT + TILE_HEIGHT/2))) {
                m_commands.emplace(new MoveCommand(this, WorldPosition(pt.x, pt.y)));
            }
        }
    }

    if (m_commands.empty()) {
        return;
    }

    auto command = m_commands.front().get();
    uint32_t timeleft = elapsed;
    while (timeleft) {
        timeleft = command->update(timeleft);
        if (command->done()) {
            m_commands.pop();
            if (m_commands.empty()) {
                break;
            }
            command = m_commands.front().get();
        }
    }
}
Ejemplo n.º 7
0
void UpdateMainCamera(Camera* main_camera, World* world) {
  auto player = world->player_component.begin()->entity;
  auto transform_component = &world->transform_component;
  main_camera->set_position(transform_component->WorldPosition(player));
  main_camera->set_facing(
      transform_component->WorldOrientation(player).Inverse() *
      mathfu::kAxisY3f);
  auto player_data = world->entity_manager.GetComponentData<PlayerData>(player);
  auto raft_orientation = transform_component->WorldOrientation(
      world->entity_manager.GetComponent<ServicesComponent>()->raft_entity());
  main_camera->set_up(raft_orientation.Inverse() * player_data->GetUp());
}
Ejemplo n.º 8
0
void ASrPlayerController::OnHoldPressed(const FVector2D& ScreenPosition, float DownTime)
{
	FVector WorldPosition(0.0f);
	AActor* const HitActor = GetFriendlyTargetUnderCursor(ScreenPosition, WorldPosition);

	SetSelectedActor(HitActor, WorldPosition);

	if (HitActor && HitActor->Implements<USrInputInterface>())
	{
		ISrInputInterface::Execute_OnInputHold(HitActor);
	}
}
Ejemplo n.º 9
0
void Light::OnWorldBoundingBoxUpdate() const
{
    switch (lightType)
    {
    case LIGHT_DIRECTIONAL:
        // Directional light always sets humongous bounding box not affected by transform
        worldBoundingBox.Define(-M_MAX_FLOAT, M_MAX_FLOAT);
        break;
        
    case LIGHT_POINT:
        {
            const Vector3& center = WorldPosition();
            Vector3 edge(range, range, range);
            worldBoundingBox.Define(center - edge, center + edge);
        }
        break;
        
    case LIGHT_SPOT:
        worldBoundingBox.Define(WorldFrustum());
        break;
    }

    SetFlag(NF_BOUNDING_BOX_DIRTY, false);
}
Ejemplo n.º 10
0
void OctreeNode::OnWorldBoundingBoxUpdate() const
{
    // The OctreeNode base class does not have a defined size, so represent as a point
    worldBoundingBox.Define(WorldPosition());
    SetFlag(NF_BOUNDING_BOX_DIRTY, false);
}
Ejemplo n.º 11
0
void OctreeNode::OnPrepareRender(unsigned frameNumber, Camera* camera)
{
    lastFrameNumber = frameNumber;
    distance = camera->Distance(WorldPosition());
}
Ejemplo n.º 12
0
void	TMissionEditor::MoveDragPix(int x,int y)
{
    WorldPosition(x,y);
    MoveDrag(x,y);
}
Ejemplo n.º 13
0
void Light::SetupShadowViews(Camera* mainCamera, Vector<AutoPtr<ShadowView> >& shadowViews, size_t& useIndex)
{
    size_t numViews = NumShadowViews();
    if (!numViews)
        return;

    if (shadowViews.Size() < useIndex + numViews)
        shadowViews.Resize(useIndex + numViews);

    int numVerticalSplits = (lightType == LIGHT_POINT || (lightType == LIGHT_DIRECTIONAL && NumShadowSplits() > 2)) ? 2 : 1;
    int actualShadowMapSize = shadowRect.Height() / numVerticalSplits;

    for (size_t i = 0; i < numViews; ++i)
    {
        if (!shadowViews[useIndex + i])
            shadowViews[useIndex + i] = new ShadowView();

        ShadowView* view = shadowViews[useIndex + i].Get();
        view->Clear();
        view->light = this;
        Camera& shadowCamera = view->shadowCamera;

        switch (lightType)
        {
        case LIGHT_DIRECTIONAL:
            {
                IntVector2 topLeft(shadowRect.left, shadowRect.top);
                if (i & 1)
                    topLeft.x += actualShadowMapSize;
                if (i & 2)
                    topLeft.y += actualShadowMapSize;
                view->viewport = IntRect(topLeft.x, topLeft.y, topLeft.x + actualShadowMapSize, topLeft.y + actualShadowMapSize);

                float splitStart = Max(mainCamera->NearClip(), (i == 0) ? 0.0f : ShadowSplit(i - 1));
                float splitEnd = Min(mainCamera->FarClip(), ShadowSplit(i));
                float extrusionDistance = mainCamera->FarClip();
                
                // Calculate initial position & rotation
                shadowCamera.SetTransform(mainCamera->WorldPosition() - extrusionDistance * WorldDirection(), WorldRotation());

                // Calculate main camera shadowed frustum in light's view space
                Frustum splitFrustum = mainCamera->WorldSplitFrustum(splitStart, splitEnd);
                const Matrix3x4& lightView = shadowCamera.ViewMatrix();
                Frustum lightViewFrustum = splitFrustum.Transformed(lightView);

                // Fit the frustum inside a bounding box
                BoundingBox shadowBox;
                shadowBox.Define(lightViewFrustum);

                // If shadow camera is far away from the frustum, can bring it closer for better depth precision
                /// \todo The minimum distance is somewhat arbitrary
                float minDistance = mainCamera->FarClip() * 0.25f;
                if (shadowBox.min.z > minDistance)
                {
                    float move = shadowBox.min.z - minDistance;
                    shadowCamera.Translate(Vector3(0.0f, 0.f, move));
                    shadowBox.min.z -= move,
                    shadowBox.max.z -= move;
                }

                shadowCamera.SetOrthographic(true);
                shadowCamera.SetFarClip(shadowBox.max.z);

                Vector3 center = shadowBox.Center();
                Vector3 size = shadowBox.Size();
                shadowCamera.SetOrthoSize(Vector2(size.x, size.y));
                shadowCamera.SetZoom(1.0f);

                // Center shadow camera to the view space bounding box
                Vector3 pos(shadowCamera.WorldPosition());
                Quaternion rot(shadowCamera.WorldRotation());
                Vector3 adjust(center.x, center.y, 0.0f);
                shadowCamera.Translate(rot * adjust, TS_WORLD);

                // Snap to whole texels
                {
                    Vector3 viewPos(rot.Inverse() * shadowCamera.WorldPosition());
                    float invSize = 1.0f / actualShadowMapSize;
                    Vector2 texelSize(size.x * invSize, size.y * invSize);
                    Vector3 snap(-fmodf(viewPos.x, texelSize.x), -fmodf(viewPos.y, texelSize.y), 0.0f);
                    shadowCamera.Translate(rot * snap, TS_WORLD);
                }
            }
            break;

        case LIGHT_POINT:
            {
                static const Quaternion pointLightFaceRotations[] = {
                    Quaternion(0.0f, 90.0f, 0.0f),
                    Quaternion(0.0f, -90.0f, 0.0f),
                    Quaternion(-90.0f, 0.0f, 0.0f),
                    Quaternion(90.0f, 0.0f, 0.0f),
                    Quaternion(0.0f, 0.0f, 0.0f),
                    Quaternion(0.0f, 180.0f, 0.0f)
                };

                IntVector2 topLeft(shadowRect.left, shadowRect.top);
                if (i & 1)
                    topLeft.y += actualShadowMapSize;
                topLeft.x += ((unsigned)i >> 1) * actualShadowMapSize;
                view->viewport = IntRect(topLeft.x, topLeft.y, topLeft.x + actualShadowMapSize, topLeft.y + actualShadowMapSize);

                shadowCamera.SetTransform(WorldPosition(), pointLightFaceRotations[i]);
                shadowCamera.SetFov(90.0f);
                // Adjust zoom to avoid edge sampling artifacts (there is a matching adjustment in the shadow sampling)
                shadowCamera.SetZoom(0.99f);
                shadowCamera.SetFarClip(Range());
                shadowCamera.SetNearClip(Range() * 0.01f);
                shadowCamera.SetOrthographic(false);
                shadowCamera.SetAspectRatio(1.0f);
            }
            break;

        case LIGHT_SPOT:
            view->viewport = shadowRect;
            shadowCamera.SetTransform(WorldPosition(), WorldRotation());
            shadowCamera.SetFov(fov);
            shadowCamera.SetZoom(1.0f);
            shadowCamera.SetFarClip(Range());
            shadowCamera.SetNearClip(Range() * 0.01f);
            shadowCamera.SetOrthographic(false);
            shadowCamera.SetAspectRatio(1.0f);
            break;
        }
    }

    // Setup shadow matrices now as camera positions have been finalized
    if (lightType != LIGHT_POINT)
    {
        shadowMatrices.Resize(numViews);
        
        for (size_t i = 0; i < numViews; ++i)
        {
            ShadowView* view = shadowViews[useIndex + i].Get();

            Camera& shadowCamera = view->shadowCamera;
            float width = (float)shadowMap->Width();
            float height = (float)shadowMap->Height();
            Vector3 offset((float)view->viewport.left / width, (float)view->viewport.top / height, 0.0f);
            Vector3 scale(0.5f * (float)view->viewport.Width() / width, 0.5f * (float)view->viewport.Height() / height, 1.0f);

            offset.x += scale.x;
            offset.y += scale.y;
            scale.y = -scale.y;

            // OpenGL has different depth range
            #ifdef TURSO3D_OPENGL
            offset.z = 0.5f;
            scale.z = 0.5f;
            #endif
            
            Matrix4 texAdjust(Matrix4::IDENTITY);
            texAdjust.SetTranslation(offset);
            texAdjust.SetScale(scale);

            shadowMatrices[i] = texAdjust * shadowCamera.ProjectionMatrix() * shadowCamera.ViewMatrix();
        }
    }
    else
    {
        // Point lights use an extra constant instead
        shadowMatrices.Clear();

        Vector2 textureSize((float)shadowMap->Width(), (float)shadowMap->Height());
        pointShadowParameters = Vector4(actualShadowMapSize / textureSize.x, actualShadowMapSize / textureSize.y,
            (float)shadowRect.left / textureSize.x, (float)shadowRect.top / textureSize.y);
    }

    // Calculate shadow mapping constants
    Camera& shadowCamera = shadowViews[useIndex]->shadowCamera;
    float nearClip = shadowCamera.NearClip();
    float farClip = shadowCamera.FarClip();
    float q = farClip / (farClip - nearClip);
    float r = -q * nearClip;
    shadowParameters = Vector4(0.5f / (float)shadowMap->Width(), 0.5f / (float)shadowMap->Height(), q, r);
    
    useIndex += numViews;
}
Ejemplo n.º 14
0
Sphere Light::WorldSphere() const
{
    return Sphere(WorldPosition(), range);
}
Ejemplo n.º 15
0
WorldPosition LifeForm::get_pos() const
{
    return WorldPosition(m_pos_x, m_pos_y);
}