Skip to content

Commit

Permalink
Weighted random neighbour tile selection
Browse files Browse the repository at this point in the history
  • Loading branch information
SebLague committed Jul 12, 2019
1 parent ad443a1 commit 78e6d87
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 215 deletions.
44 changes: 44 additions & 0 deletions Assets/CamA.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CamA : MonoBehaviour {

This comment has been minimized.

Copy link
@Crytios

Crytios Jul 9, 2021

ok


public Vector3 targetPos;
Vector3 startPos;
public float easeTime;
bool running;
float t;

void Start () {
startPos = transform.position;
}

void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
running = true;
}
if (running) {
t += Time.deltaTime * 1 / easeTime;
float e = CubicEase (Mathf.Clamp01 (t));
transform.position = Vector3.Lerp (startPos, targetPos, e);
}
}

float CubicEase (float t, float b = 0, float c = 1, float d = 1) {
t /= d / 2;
if (t < 1) return c / 2 * t * t * t + b;
t -= 2;
return c / 2 * (t * t * t + 2) + b;
}

float Ease (float time) {
time /= 1f/2;
if (time < 1) {
return 1 / 2f * time * time;
}

time--;
return -1f / 2 * (time * (time - 2) - 1);
}
}
11 changes: 11 additions & 0 deletions Assets/CamA.cs.meta

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

27 changes: 21 additions & 6 deletions Assets/Scenes/Terrain.unity

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions Assets/Scripts/Behaviour/Animal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,52 @@

public class Animal : MonoBehaviour {

float hopHeight = .1f;
float hopHeight = .2f;
float hopSpeed = 1.5f;
Vector2Int coord;

// Hop data:
bool hopping;
Vector2Int hopStartCoord;
Vector2Int targetCoord;
Vector3 hopStart;
Vector3 hopTarget;
float hopTime;
float hopSpeedFactor;
float hopHeightFactor;

public void SetCoord (Vector2Int coord) {
transform.position = Environment.tileCentres[coord.x, coord.y];
this.coord = coord;
hopStartCoord = coord;

}
protected virtual void Start () {
OnFinishedMoving ();
}

protected virtual void OnFinishedMoving () {

StartHopToCoord (Environment.GetRandomWalkableNeighbour (coord));
StartHopToCoord (Environment.GetNextTileWeighted (coord, hopStartCoord));
}

protected void StartHopToCoord (Vector2Int coord) {
targetCoord = coord;
protected void StartHopToCoord (Vector2Int target) {
hopStartCoord = coord;
targetCoord = target;
hopStart = transform.position;
hopTarget = Environment.tileCentres[coord.x, coord.y];
hopTarget = Environment.tileCentres[targetCoord.x, targetCoord.y];
hopping = true;

bool diagonalHop = (hopStartCoord - targetCoord).sqrMagnitude > 1;
hopHeightFactor = (diagonalHop) ? 1.4142f : 1;
hopSpeedFactor = (diagonalHop) ? 0.7071f : 1;

LookAt (targetCoord);
}

protected void LookAt (Vector2Int target) {
if (target != coord) {
Vector2Int offset = target - coord;
transform.eulerAngles = Vector3.up * Mathf.Atan2 (offset.x, -offset.y) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.Atan2 (offset.x, offset.y) * Mathf.Rad2Deg;
}
}

Expand All @@ -52,12 +61,12 @@ protected virtual void Update () {
}

void AnimateHop () {
hopTime = Mathf.Min (1, hopTime + Time.deltaTime * hopSpeed);
float height = (1 - 4 * (hopTime - .5f) * (hopTime - .5f)) * hopHeight;
hopTime = Mathf.Min (1, hopTime + Time.deltaTime * hopSpeed * hopSpeedFactor);
float height = (1 - 4 * (hopTime - .5f) * (hopTime - .5f)) * hopHeight * hopHeightFactor;
transform.position = Vector3.Lerp (hopStart, hopTarget, hopTime) + Vector3.up * height;
if (hopTime >= 1) {
coord = targetCoord;
hopTime = 0;
coord = targetCoord;
OnFinishedMoving ();
}
}
Expand Down
57 changes: 51 additions & 6 deletions Assets/Scripts/Environment/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Environment : MonoBehaviour {
public static Vector3[, ] tileCentres;
public static bool[, ] walkable;

static int size;
static WalkableNeigbours[, ] walkableNeighboursMap;
static System.Random prng;

Expand All @@ -22,29 +23,73 @@ void Start () {
SpawnInitialPopulations ();
}

public static Vector2Int GetRandomWalkableNeighbour (Vector2Int coord) {
var neighbours = walkableNeighboursMap[coord.x, coord.y];
public static Vector2Int GetNextTileRandom (Vector2Int current) {
var neighbours = walkableNeighboursMap[current.x, current.y];
if (neighbours.count == 0) {
return coord;
return current;
}
return neighbours.coords[prng.Next (neighbours.count)];
}

/// Get random neighbour tile, weighted towards those in similar direction as currently facing
public static Vector2Int GetNextTileWeighted (Vector2Int current, Vector2Int previous, double forwardProbability = 0.0, int weightingIterations = 3) {

if (current == previous) {
return GetNextTileRandom (current);
}

Vector2Int forwardOffset = (current - previous);
// Random chance of returning foward tile (if walkable)
if (prng.NextDouble () < forwardProbability) {
Vector2Int forwardCoord = current + forwardOffset;

if (forwardCoord.x >= 0 && forwardCoord.x < size && forwardCoord.y >= 0 && forwardCoord.y < size) {
if (walkable[forwardCoord.x, forwardCoord.y]) {
return forwardCoord;
}
}
}

// Get walkable neighbours
var neighbours = walkableNeighboursMap[current.x, current.y];
if (neighbours.count == 0) {
return current;
}

// From n random tiles, pick the one that is most aligned with the forward direction:
Vector2 forwardDir = new Vector2 (forwardOffset.x, forwardOffset.y).normalized;
float bestScore = float.MinValue;
Vector2Int bestNeighbour = current;

for (int i = 0; i < weightingIterations; i++) {
Vector2Int neighbour = neighbours.coords[prng.Next (neighbours.count)];
Vector2 offset = neighbour - current;
float score = Vector2.Dot (offset.normalized, forwardDir);
if (score > bestScore) {
bestScore = score;
bestNeighbour = neighbour;
}
}

return bestNeighbour;
}

void CreateTerrain () {
var terrainGenerator = FindObjectOfType<TerrainGenerator> ();
terrainData = terrainGenerator.Generate ();
tileCentres = terrainData.tileCentres;
walkable = terrainData.walkable;
int size = terrainData.size;
size = terrainData.size;

walkableNeighboursMap = new WalkableNeigbours[size, size];

// Find and store all walkable neighbours for each walkable tile on the map
for (int y = 0; y < terrainData.size; y++) {
for (int x = 0; x < terrainData.size; x++) {
if (walkable[x, y]) {
List<Vector2Int> walkableNeighbours = new List<Vector2Int> ();
for (int offsetY = 0; offsetY <= 1; offsetY++) {
for (int offsetX = 0; offsetX <= 1; offsetX++) {
for (int offsetY = -1; offsetY <= 1; offsetY++) {
for (int offsetX = -1; offsetX <= 1; offsetX++) {
if (offsetX != 0 || offsetY != 0) {
int neighbourX = x + offsetX;
int neighbourY = y + offsetY;
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Terrain/TerrainGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public TerrainData Generate () {
var biomes = new Biome[] { water, sand, grass };
Vector3[] upVectorX4 = { Vector3.up, Vector3.up, Vector3.up, Vector3.up };
Vector2Int[] nswe = { Vector2Int.up, Vector2Int.down, Vector2Int.left, Vector2Int.right };
int[][] sideVertIndexByDir = { new int[] { 3, 2 }, new int[] { 0, 1 }, new int[] { 2, 0 }, new int[] { 1, 3 } };
Vector3[] sideNormalsByDir = { Vector3.forward, Vector3.back, Vector3.right, Vector3.left };
int[][] sideVertIndexByDir = { new int[] { 0, 1 }, new int[] { 3, 2 }, new int[] { 2, 0 }, new int[] { 1, 3 } };
Vector3[] sideNormalsByDir = { Vector3.forward, Vector3.back, Vector3.left, Vector3.right };

// Terrain data:
var terrainData = new TerrainData (numTilesPerLine);
Expand All @@ -83,7 +83,7 @@ public TerrainData Generate () {
// Vertices
int vertIndex = vertices.Count;
float height = (isWaterTile) ? -waterDepth : 0;
Vector3 nw = new Vector3 (min + x, height, -min - y);
Vector3 nw = new Vector3 (min + x, height, min + y + 1);
Vector3 ne = nw + Vector3.right;
Vector3 sw = nw - Vector3.forward;
Vector3 se = sw + Vector3.right;
Expand Down
Loading

0 comments on commit 78e6d87

Please sign in to comment.