Skip to content

Instantly share code, notes, and snippets.

@tgjones
Created February 18, 2016 12:57
Show Gist options
  • Save tgjones/880648320963346ce21f to your computer and use it in GitHub Desktop.
Save tgjones/880648320963346ce21f to your computer and use it in GitHub Desktop.
public class TorqGame : Tuki.Game
{
protected override void ConfigureGraphics(GraphicsDeviceManager graphics)
{
graphics.PreferredBackBufferWidth = 1920;
graphics.PreferredBackBufferHeight = 1200;
graphics.IsFullScreen = false;
}
protected override void LoadContent()
{
IsFixedTimeStep = true;
var scene = new Scene();
const string trackName = "FourOaks";
const string carName = "Ford";
var skyboxMaterial = Material.CreateSkyboxMaterial(Content);
skyboxMaterial.SetPropertyValue("Texture", Content.Load<TextureCube>($"Tracks/{trackName}/Skybox"));
scene.Settings.FogType = FogType.None;
scene.Settings.SkyboxMaterial = skyboxMaterial;
scene.Settings.AmbientIntensity = 0.6f;
scene.Settings.AmbientSource = AmbientSource.Skybox;
scene.Settings.AmbientColor = Color.White;
scene.Settings.DefaultReflectionResolution = 128;
var trackDefinition = Content.Load<TrackDefinition>($"Tracks/{trackName}/Track");
scene.AddChild(new GameEntity(), e =>
{
e.Name = "Directional Light";
e.AddComponent(new DirectionalLightComponent
{
Color = Color.LightYellow,
Intensity = 5,
Shadows = ShadowsType.Soft,
ShadowDistance = 200.0f,
CullingMask = int.MaxValue & ~(1 << 8)
});
e.Transform.LocalPosition = new Vector3(1, 1, 1);
e.Transform.LookAt(Vector3.Zero);
});
foreach (var segment in trackDefinition.Segments)
{
scene.AddChild(Content.Load<GameEntity>($"Tracks/{trackName}/{segment}"), e =>
{
var meshComponent = e.GetComponent<MeshComponent>();
meshComponent.ReceivesShadows = true;
//meshComponent.CastsShadows = true;
var needsCollider = false;
var textureFileName = Path.GetFileNameWithoutExtension(meshComponent.Material.GetPropertyValueTexture2D("Texture").Tag as string);
if (textureFileName.EndsWith("_0"))
textureFileName = textureFileName.Substring(0, textureFileName.Length - 2);
var terrainType = trackDefinition.TerrainTypes.FirstOrDefault(x => x.Texture == textureFileName);
if (terrainType != null && terrainType.Type != TerrainType.Unknown)
needsCollider = true;
var objectType = trackDefinition.ObjectTypes.FirstOrDefault(x => x.Texture == textureFileName);
if (objectType != null && objectType.Type != ObjectType.Unknown && objectType.Type != ObjectType.Soft)
needsCollider = true;
if (needsCollider)
e.AddComponent<MeshColliderComponent>(c =>
{
c.Mesh = meshComponent.Mesh;
c.Material.KineticFriction = 0.5f;
});
});
}
var carDefinition = Content.Load<CarDefinition>($"Cars/{carName}/Car");
var parentVehicleEntity = scene.AddChild(new GameEntity());
var vehicleEntity = parentVehicleEntity.AddChild(new GameEntity(), e =>
{
e.Transform.WorldPosition = trackDefinition.StartingGridPosition + new Vector3(0, 2, 0);
e.Transform.Rotate(0.0f, trackDefinition.StartingGridAngle, 0.0f);
// Car body collider.
e.AddComponent<BoxColliderComponent>(c => { c.Size = new Vector3(3.0f, 2.0f, 7.1f); });
var vehicleComponent = e.AddComponent<VehicleComponent>(c =>
{
c.CenterOfMass = carDefinition.CenterOfMass;
c.Mass = carDefinition.Mass;
c.ForwardSpeed = 50;
});
e.AddComponent<MeshComponent>(c =>
{
var carBodyMeshComponent = scene.AddChild(Content.Load<GameEntity>($"Cars/{carName}/body")).GetComponent<MeshComponent>();
c.Mesh = carBodyMeshComponent.Mesh;
c.Materials = carBodyMeshComponent.Materials;
c.CastsShadows = true;
});
e.AddComponent<AudioEmitterComponent>(c =>
{
c.SoundEffect = Content.Load<SoundEffect>($"Cars/{carName}/Sounds/Engine");
c.SoundEffectInstance.IsLooped = true;
c.PlayOnInitialize = true;
e.AddComponent<AdjustEngineSoundComponent>(c2 =>
{
c2.Vehicle = vehicleComponent;
c2.SoundEffectInstance = c.SoundEffectInstance;
});
});
});
scene.AddChild(new GameEntity(), e =>
{
e.Name = "Camera";
e.AddComponent<PerspectiveCameraComponent>(c =>
{
c.FarPlaneDistance = trackDefinition.ClippingDistance;
c.NearPlaneDistance = 0.1f;
c.FieldOfView = 45;
c.ClearType = CameraClearType.DepthAndSkybox;
c.BackgroundColor = Color.CornflowerBlue;
c.CullingMask = int.MaxValue & ~(1 << 8);
c.PostProcessEffects.Add(new BloomEffect { BloomThreshold = 0.5f });
//c.PostProcessEffects.Add(new AntiAliasingEffect());
//c.PostProcessEffects.Add(new GaussianBlurEffect());
});
e.Transform.WorldPosition = trackDefinition.StartingGridPosition + new Vector3(0, 1, 7);
e.AddComponent(new FirstPersonCameraController());
//e.AddComponent<ChaseCameraController>(c =>
//{
// c.Target = vehicleEntity.Transform;
// c.DesiredPositionOffset = new Vector3(0, 3, 8.5f);
// c.LookAtOffset = new Vector3(0, 2, 0);
// c.HorizontalRotationStiffness = 3.0f;
// c.VerticalRotationStiffness = 5000;
// c.LinearStiffness = 5000;
//});
//e.AddComponent<FramesPerSecondComponent>();
e.AddComponent<AudioListenerComponent>();
});
var wheelEntity = Content.Load<GameEntity>($"Cars/{carName}/wheel");
var wheelMeshComponent = wheelEntity.GetComponentsInChildren<MeshComponent>().Single();
foreach (var wheelDefinition in carDefinition.Wheels)
{
parentVehicleEntity.AddChild(new GameEntity(), e =>
{
e.AddComponent<WheelComponent>(c =>
{
c.WheelRadius = carDefinition.WheelRadius;
c.WheelWidth = carDefinition.WheelWidth;
c.SuspensionRestLength = carDefinition.MaxSpringLength;
c.AttachmentPoint = wheelDefinition.Position;
c.IsDriveWheel = wheelDefinition.Drive;
c.IsSteeringWheel = wheelDefinition.Steering;
c.SlidingKineticFriction = 3;
if (wheelDefinition.GraphicFlipped)
c.GraphicRotation = Matrix.CreateFromYawPitchRoll(MathHelper.Pi, 0, 0);
});
e.AddComponent<MeshComponent>(c =>
{
c.Mesh = wheelMeshComponent.Mesh;
c.Materials = wheelMeshComponent.Materials;
});
});
}
scene.AddChild(Content.Load<GameEntity>("Tracks/RallyWalesStageB/rally_wales_stage_b"));
Engine.Scene = scene;
//var song = Content.Load<Song>("Music/GreenHills");
//MediaPlayer.Volume = 0.3f;
//MediaPlayer.Play(song);
//MediaPlayer.IsRepeating = true;
base.LoadContent();
}
private class AdjustEngineSoundComponent : ScriptComponent
{
public VehicleComponent Vehicle { get; set; }
public SoundEffectInstance SoundEffectInstance { get; set; }
public override void Update(GameTime gameTime)
{
SoundEffectInstance.Pitch = (Math.Abs(Vehicle.Speed) / 150.0f * 2.0f) - 1.0f;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment