Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace private properties with fields #88

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions TGC.MonoGame.Samples/Cameras/FreeCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ namespace TGC.MonoGame.Samples.Cameras
{
internal class FreeCamera : Camera
{
private readonly bool lockMouse;
private readonly bool _lockMouse;

private readonly Point screenCenter;
private bool changed;
private readonly Point _screenCenter;
private bool _changed;

private Vector2 pastMousePosition;
private float pitch;
private Vector2 _pastMousePosition;
private float _pitch;

// Angles
private float yaw = -90f;
private float _yaw = -90f;

public FreeCamera(float aspectRatio, Vector3 position, Point screenCenter) : this(aspectRatio, position)
{
lockMouse = true;
this.screenCenter = screenCenter;
_lockMouse = true;
this._screenCenter = screenCenter;
}

public FreeCamera(float aspectRatio, Vector3 position) : base(aspectRatio)
{
Position = position;
pastMousePosition = Mouse.GetState().Position.ToVector2();
_pastMousePosition = Mouse.GetState().Position.ToVector2();
UpdateCameraVectors();
CalculateView();
}
Expand All @@ -43,11 +43,11 @@ private void CalculateView()
public override void Update(GameTime gameTime)
{
var elapsedTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
changed = false;
_changed = false;
ProcessKeyboard(elapsedTime);
ProcessMouseMovement(elapsedTime);

if (changed)
if (_changed)
CalculateView();
}

Expand All @@ -62,25 +62,25 @@ private void ProcessKeyboard(float elapsedTime)
if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left))
{
Position += -RightDirection * currentMovementSpeed * elapsedTime;
changed = true;
_changed = true;
}

if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right))
{
Position += RightDirection * currentMovementSpeed * elapsedTime;
changed = true;
_changed = true;
}

if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up))
{
Position += FrontDirection * currentMovementSpeed * elapsedTime;
changed = true;
_changed = true;
}

if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down))
{
Position += -FrontDirection * currentMovementSpeed * elapsedTime;
changed = true;
_changed = true;
}
}

Expand All @@ -90,23 +90,23 @@ private void ProcessMouseMovement(float elapsedTime)

if (mouseState.RightButton.Equals(ButtonState.Pressed))
{
var mouseDelta = mouseState.Position.ToVector2() - pastMousePosition;
var mouseDelta = mouseState.Position.ToVector2() - _pastMousePosition;
mouseDelta *= MouseSensitivity * elapsedTime;

yaw -= mouseDelta.X;
pitch += mouseDelta.Y;
_yaw -= mouseDelta.X;
_pitch += mouseDelta.Y;

if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
if (_pitch > 89.0f)
_pitch = 89.0f;
if (_pitch < -89.0f)
_pitch = -89.0f;

changed = true;
_changed = true;
UpdateCameraVectors();

if (lockMouse)
if (_lockMouse)
{
Mouse.SetPosition(screenCenter.X, screenCenter.Y);
Mouse.SetPosition(_screenCenter.X, _screenCenter.Y);
Mouse.SetCursor(MouseCursor.Crosshair);
}
else
Expand All @@ -115,16 +115,16 @@ private void ProcessMouseMovement(float elapsedTime)
}
}

pastMousePosition = Mouse.GetState().Position.ToVector2();
_pastMousePosition = Mouse.GetState().Position.ToVector2();
}

private void UpdateCameraVectors()
{
// Calculate the new Front vector
Vector3 tempFront;
tempFront.X = MathF.Cos(MathHelper.ToRadians(yaw)) * MathF.Cos(MathHelper.ToRadians(pitch));
tempFront.Y = MathF.Sin(MathHelper.ToRadians(pitch));
tempFront.Z = MathF.Sin(MathHelper.ToRadians(yaw)) * MathF.Cos(MathHelper.ToRadians(pitch));
tempFront.X = MathF.Cos(MathHelper.ToRadians(_yaw)) * MathF.Cos(MathHelper.ToRadians(_pitch));
tempFront.Y = MathF.Sin(MathHelper.ToRadians(_pitch));
tempFront.Z = MathF.Sin(MathHelper.ToRadians(_yaw)) * MathF.Cos(MathHelper.ToRadians(_pitch));

FrontDirection = Vector3.Normalize(tempFront);

Expand Down
26 changes: 13 additions & 13 deletions TGC.MonoGame.Samples/Geometries/FullScreenQuad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ namespace TGC.MonoGame.Samples.Geometries
{
public class FullScreenQuad
{
private readonly GraphicsDevice device;
private IndexBuffer indexBuffer;
private VertexBuffer vertexBuffer;
private readonly GraphicsDevice _device;
private IndexBuffer _indexBuffer;
private VertexBuffer _vertexBuffer;

/// <summary>
/// Create a quad used in clip space
/// </summary>
/// <param name="device">Used to initialize and control the presentation of the graphics device.</param>
public FullScreenQuad(GraphicsDevice device)
{
this.device = device;
this._device = device;
CreateVertexBuffer();
CreateIndexBuffer();
}
Expand All @@ -32,9 +32,9 @@ private void CreateVertexBuffer()
vertices[3].Position = new Vector3(1f, 1f, 0f);
vertices[3].TextureCoordinate = new Vector2(1f, 0f);

vertexBuffer = new VertexBuffer(device, VertexPositionTexture.VertexDeclaration, 4,
_vertexBuffer = new VertexBuffer(_device, VertexPositionTexture.VertexDeclaration, 4,
BufferUsage.WriteOnly);
vertexBuffer.SetData(vertices);
_vertexBuffer.SetData(vertices);
}

private void CreateIndexBuffer()
Expand All @@ -48,27 +48,27 @@ private void CreateIndexBuffer()
indices[4] = 3;
indices[5] = 2;

indexBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, 6, BufferUsage.WriteOnly);
indexBuffer.SetData(indices);
_indexBuffer = new IndexBuffer(_device, IndexElementSize.SixteenBits, 6, BufferUsage.WriteOnly);
_indexBuffer.SetData(indices);
}


public void Draw(Effect effect)
{
device.SetVertexBuffer(vertexBuffer);
device.Indices = indexBuffer;
_device.SetVertexBuffer(_vertexBuffer);
_device.Indices = _indexBuffer;

foreach (var pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 2);
_device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 2);
}
}

public void Dispose()
{
vertexBuffer.Dispose();
indexBuffer.Dispose();
_vertexBuffer.Dispose();
_indexBuffer.Dispose();
}
}
}
106 changes: 53 additions & 53 deletions TGC.MonoGame.Samples/Models/Tank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ public class Tank
/// </summary>
public void Load(Model model)
{
tankModel = model;
_tankModel = model;

// Look up shortcut references to the bones we are going to animate.
leftBackWheelBone = tankModel.Bones["l_back_wheel_geo"];
rightBackWheelBone = tankModel.Bones["r_back_wheel_geo"];
leftFrontWheelBone = tankModel.Bones["l_front_wheel_geo"];
rightFrontWheelBone = tankModel.Bones["r_front_wheel_geo"];
leftSteerBone = tankModel.Bones["l_steer_geo"];
rightSteerBone = tankModel.Bones["r_steer_geo"];
turretBone = tankModel.Bones["turret_geo"];
cannonBone = tankModel.Bones["canon_geo"];
hatchBone = tankModel.Bones["hatch_geo"];
_leftBackWheelBone = _tankModel.Bones["l_back_wheel_geo"];
_rightBackWheelBone = _tankModel.Bones["r_back_wheel_geo"];
_leftFrontWheelBone = _tankModel.Bones["l_front_wheel_geo"];
_rightFrontWheelBone = _tankModel.Bones["r_front_wheel_geo"];
_leftSteerBone = _tankModel.Bones["l_steer_geo"];
_rightSteerBone = _tankModel.Bones["r_steer_geo"];
_turretBone = _tankModel.Bones["turret_geo"];
_cannonBone = _tankModel.Bones["canon_geo"];
_hatchBone = _tankModel.Bones["hatch_geo"];

// Store the original transform matrix for each animating bone.
leftBackWheelTransform = leftBackWheelBone.Transform;
rightBackWheelTransform = rightBackWheelBone.Transform;
leftFrontWheelTransform = leftFrontWheelBone.Transform;
rightFrontWheelTransform = rightFrontWheelBone.Transform;
leftSteerTransform = leftSteerBone.Transform;
rightSteerTransform = rightSteerBone.Transform;
turretTransform = turretBone.Transform;
cannonTransform = cannonBone.Transform;
hatchTransform = hatchBone.Transform;
_leftBackWheelTransform = _leftBackWheelBone.Transform;
_rightBackWheelTransform = _rightBackWheelBone.Transform;
_leftFrontWheelTransform = _leftFrontWheelBone.Transform;
_rightFrontWheelTransform = _rightFrontWheelBone.Transform;
_leftSteerTransform = _leftSteerBone.Transform;
_rightSteerTransform = _rightSteerBone.Transform;
_turretTransform = _turretBone.Transform;
_cannonTransform = _cannonBone.Transform;
_hatchTransform = _hatchBone.Transform;

// Allocate the transform matrix array.
boneTransforms = new Matrix[tankModel.Bones.Count];
_boneTransforms = new Matrix[_tankModel.Bones.Count];
}

/// <summary>
Expand All @@ -63,7 +63,7 @@ public void Load(Model model)
public void Draw(Matrix world, Matrix view, Matrix projection)
{
// Set the world matrix as the root transform of the model.
tankModel.Root.Transform = world;
_tankModel.Root.Transform = world;

// Calculate matrices based on the current animation position.
var wheelRotation = Matrix.CreateRotationX(WheelRotation);
Expand All @@ -73,25 +73,25 @@ public void Draw(Matrix world, Matrix view, Matrix projection)
var hatchRotation = Matrix.CreateRotationX(HatchRotation);

// Apply matrices to the relevant bones.
leftBackWheelBone.Transform = wheelRotation * leftBackWheelTransform;
rightBackWheelBone.Transform = wheelRotation * rightBackWheelTransform;
leftFrontWheelBone.Transform = wheelRotation * leftFrontWheelTransform;
rightFrontWheelBone.Transform = wheelRotation * rightFrontWheelTransform;
leftSteerBone.Transform = steerRotation * leftSteerTransform;
rightSteerBone.Transform = steerRotation * rightSteerTransform;
turretBone.Transform = turretRotation * turretTransform;
cannonBone.Transform = cannonRotation * cannonTransform;
hatchBone.Transform = hatchRotation * hatchTransform;
_leftBackWheelBone.Transform = wheelRotation * _leftBackWheelTransform;
_rightBackWheelBone.Transform = wheelRotation * _rightBackWheelTransform;
_leftFrontWheelBone.Transform = wheelRotation * _leftFrontWheelTransform;
_rightFrontWheelBone.Transform = wheelRotation * _rightFrontWheelTransform;
_leftSteerBone.Transform = steerRotation * _leftSteerTransform;
_rightSteerBone.Transform = steerRotation * _rightSteerTransform;
_turretBone.Transform = turretRotation * _turretTransform;
_cannonBone.Transform = cannonRotation * _cannonTransform;
_hatchBone.Transform = hatchRotation * _hatchTransform;

// Look up combined bone matrices for the entire model.
tankModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
_tankModel.CopyAbsoluteBoneTransformsTo(_boneTransforms);

// Draw the model.
foreach (var mesh in tankModel.Meshes)
foreach (var mesh in _tankModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = boneTransforms[mesh.ParentBone.Index];
effect.World = _boneTransforms[mesh.ParentBone.Index];
effect.View = view;
effect.Projection = projection;

Expand All @@ -105,36 +105,36 @@ public void Draw(Matrix world, Matrix view, Matrix projection)
#region Fields

// The XNA framework Model object that we are going to display.
private Model tankModel;
private Model _tankModel;

// Shortcut references to the bones that we are going to animate.
// We could just look these up inside the Draw method, but it is more efficient to do the lookups while loading and cache the results.
private ModelBone leftBackWheelBone;
private ModelBone _leftBackWheelBone;

private ModelBone rightBackWheelBone;
private ModelBone leftFrontWheelBone;
private ModelBone rightFrontWheelBone;
private ModelBone leftSteerBone;
private ModelBone rightSteerBone;
private ModelBone turretBone;
private ModelBone cannonBone;
private ModelBone hatchBone;
private ModelBone _rightBackWheelBone;
private ModelBone _leftFrontWheelBone;
private ModelBone _rightFrontWheelBone;
private ModelBone _leftSteerBone;
private ModelBone _rightSteerBone;
private ModelBone _turretBone;
private ModelBone _cannonBone;
private ModelBone _hatchBone;

// Store the original transform matrix for each animating bone.
private Matrix leftBackWheelTransform;
private Matrix _leftBackWheelTransform;

private Matrix rightBackWheelTransform;
private Matrix leftFrontWheelTransform;
private Matrix rightFrontWheelTransform;
private Matrix leftSteerTransform;
private Matrix rightSteerTransform;
private Matrix turretTransform;
private Matrix cannonTransform;
private Matrix hatchTransform;
private Matrix _rightBackWheelTransform;
private Matrix _leftFrontWheelTransform;
private Matrix _rightFrontWheelTransform;
private Matrix _leftSteerTransform;
private Matrix _rightSteerTransform;
private Matrix _turretTransform;
private Matrix _cannonTransform;
private Matrix _hatchTransform;

// Array holding all the bone transform matrices for the entire model.
// We could just allocate this locally inside the Draw method, but it is more efficient to reuse a single array, as this avoids creating unnecessary garbage.
private Matrix[] boneTransforms;
private Matrix[] _boneTransforms;

// Current animation positions.

Expand Down
Loading
Loading