Skip to content

Commit

Permalink
Separate camera and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
h8man committed Aug 8, 2021
1 parent d63000e commit eda9388
Show file tree
Hide file tree
Showing 9 changed files with 1,191 additions and 498 deletions.
26 changes: 26 additions & 0 deletions Assets/Codebehind/HQ/HqCameraFollow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;

namespace HQ
{
[ExecuteInEditMode]
class HqCameraFollow: MonoBehaviour
{
public ProjectedBody body;
public HqRenderer hQCamera;

private void Update()
{
#if UNITY_EDITOR
Follow();
#else
Follow();
#endif
}

private void Follow()
{
hQCamera.cameraOffset = body.playerX;
hQCamera.trip = body.trip;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Codebehind/HQ/HqCameraFollow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 25 additions & 23 deletions Assets/Codebehind/HQ/HqRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class HqRenderer : MonoBehaviour
public int screenHeightRef = 240;
public float cameraDepth = 0.84f; //camera depth [0..1]
public int DravingDistance = 300; //segments
public int quadCapacity = 4000;
public int cameraHeight = 1500; //pixels?
public float cameraOffset = 0;
public float centrifugal = 0.1f;
public float paralaxSpeed = 0.1f;
public bool drawRoad;
Expand Down Expand Up @@ -66,7 +68,9 @@ public class HqRenderer : MonoBehaviour
[NonSerialized]
private RenderTexture _renderTexture;
[NonSerialized]
private int speed;
private float speed;
[NonSerialized]
private float prevTrip;
[NonSerialized]
private Vector2 bgOffset;

Expand Down Expand Up @@ -104,7 +108,15 @@ void Awake()
BG.sprite = Sprite.Create(tex3, BGSprite.rect, new Vector2(0.5f, 0.5f), PPU);
BG.sprite.name = "runtimeBG";

quad = new Quad[] { new Quad(), new Quad(), new Quad(), new Quad(), new Quad(), new Quad(), new Quad() };
quad = new Quad[] {
new Quad(quadCapacity),
new Quad(quadCapacity),
new Quad(quadCapacity),
new Quad(quadCapacity),
new Quad(quadCapacity),
new Quad(quadCapacity),
new Quad(quadCapacity)
};
combined = new Mesh[] { new Mesh(), new Mesh(),new Mesh(),new Mesh(),new Mesh(),new Mesh(),new Mesh(),new Mesh(),new Mesh()};
dictionary = new Dictionary<Material, Quad>()
{
Expand Down Expand Up @@ -227,7 +239,6 @@ private void PostRender(Camera cam)

private void FixedUpdate()
{
GetInput();
CalculateProjection();
}
void Update()
Expand All @@ -242,32 +253,16 @@ void Update()
DrawObjects();
}

void GetInput()
{
speed = 0;
if (Input.GetKey(KeyCode.RightArrow)) playerX += 0.1f;
if (Input.GetKey(KeyCode.LeftArrow)) playerX -= 0.1f;
if (Input.GetKey(KeyCode.UpArrow)) speed = 200;
if (Input.GetKey(KeyCode.DownArrow)) speed = -200;
if (Input.GetKey(KeyCode.Tab)) speed *= 3;
if (Input.GetKey(KeyCode.W)) cameraHeight += 100;
if (Input.GetKey(KeyCode.S)) cameraHeight -= 100;

trip += speed;
while (trip >= track.Length * track.segmentLength) trip -= track.Length * track.segmentLength;
while (trip < 0) trip += track.Length * track.segmentLength;
}

void CalculateProjection()
{
{
speed = trip - prevTrip;
prevTrip = trip;
startPos = trip / track.segmentLength;
playerZ = trip + cameraHeight * cameraDepth; // car is in front of cammera
playerPos = (int)(playerZ / track.segmentLength) % track.lines.Length;
playerY = track.lines[playerPos].y;
int camH = (int)(playerY + cameraHeight);
playerX = playerX - track.lines[playerPos].curve * centrifugal * speed * Time.fixedDeltaTime;
playerX = Mathf.Clamp(playerX, -2, 2);

playerX = cameraOffset;
screenWidth2 = screenWidthRef / 2;
screenHeight2 = screenHeightRef / 2;

Expand Down Expand Up @@ -315,7 +310,14 @@ void CalculateProjection()
addQuad(grass, 0, p.Y, screenWidth2, 0, l.Y, screenWidth2, z);
addQuad(rumble, p.X, p.Y, p.W + p.scale * rumbleWidth * screenWidth2, l.X, l.Y, l.W + l.scale * rumbleWidth * screenWidth2, z);
addQuad(road, p.X, p.Y, p.W, l.X, l.Y, l.W, z);
//var p_rumbleWidth = p.W + p.scale * rumbleWidth * screenWidth2;
//var l_rumbleWidth = l.W + l.scale * rumbleWidth * screenWidth2;
//addQuad(grass, p.X - screenWidth2 - p_rumbleWidth, p.Y, screenWidth2, l.X - screenWidth2 - l_rumbleWidth, l.Y, screenWidth2, z);
//addQuad(grass, p.X + screenWidth2 + p_rumbleWidth, p.Y, screenWidth2, l.X + screenWidth2 + l_rumbleWidth, l.Y, screenWidth2, z);
//addQuad(rumble, p.X - p_rumbleWidth, p.Y, p.scale* rumbleWidth* screenWidth2, l.X - l_rumbleWidth, l.Y, l.scale*screenWidth2 * rumbleWidth, z);
//addQuad(rumble, p.X + p_rumbleWidth, p.Y, p.scale* rumbleWidth* screenWidth2, l.X + l_rumbleWidth, l.Y, l.scale*screenWidth2 * rumbleWidth, z);

addQuad(road, p.X, p.Y, p.W, l.X, l.Y, l.W, z);
if ((n / 3) % 2 == 0)
{
addQuad(dashline, p.X, p.Y * 1.1f, p.W * 0.05f, l.X, l.Y * 1.1f, l.W * 0.05f, z);
Expand Down
21 changes: 21 additions & 0 deletions Assets/Codebehind/HQ/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using UnityEngine;

namespace HQ
{
class PlayerController: MonoBehaviour
{
public HqRenderer hQcamera;
public ProjectedBody body;
private void FixedUpdate()
{
body.speed = 0;
if (Input.GetKey(KeyCode.RightArrow)) body.playerX += 0.1f;
if (Input.GetKey(KeyCode.LeftArrow)) body.playerX -= 0.1f;
if (Input.GetKey(KeyCode.UpArrow)) body.speed = 200;
if (Input.GetKey(KeyCode.DownArrow)) body.speed = -200;
if (Input.GetKey(KeyCode.Tab)) body.speed *= 3;
if (Input.GetKey(KeyCode.W)) hQcamera.cameraHeight += 100;
if (Input.GetKey(KeyCode.S)) hQcamera.cameraHeight -= 100;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Codebehind/HQ/PlayerController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Assets/Codebehind/HQ/ProjectedBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using UnityEngine;
namespace HQ
{
class ProjectedBody : MonoBehaviour
{
internal float playerX;
internal int speed;
public TrackObject track;
[NonSerialized]
private int playerPos;
public float centrifugal = 0.1f;
public int trip;

public void FixedUpdate()
{
trip += speed;

while (trip >= track.Length * track.segmentLength) trip -= track.Length * track.segmentLength;
while (trip < 0) trip += track.Length * track.segmentLength;
playerPos = trip / track.segmentLength;
playerX = playerX - track.lines[playerPos].curve * centrifugal * speed * Time.fixedDeltaTime;
playerX = Mathf.Clamp(playerX, -2, 2);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Codebehind/HQ/ProjectedBody.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,535 changes: 1,060 additions & 475 deletions Assets/Scenes/HQ.unity

Large diffs are not rendered by default.

Binary file modified Assets/Sprites/streetlights.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eda9388

Please sign in to comment.