Skip to content

Commit

Permalink
All trees are now TerrainObjects from the beginning.
Browse files Browse the repository at this point in the history
  • Loading branch information
nihohit committed Oct 17, 2017
1 parent 7f3b8bc commit 8239252
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 49 deletions.
40 changes: 0 additions & 40 deletions God Game/Assets/PlayModeTests/FreeFallingObjectPlayTests.cs

This file was deleted.

69 changes: 69 additions & 0 deletions God Game/Assets/PlayModeTests/TerrainObjectScriptPlayTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;

public class TerrainObjectScriptPlayTests {
[UnityTest]
public IEnumerator objectShouldBeDestroyedWhenTooHigh() {
var gameObject = new GameObject();
var fallingScripts = gameObject.AddComponent<TerrainObjectScript>();

yield return null;

Assert.IsFalse(gameObject == null);

fallingScripts.transform.position = new Vector3(0, Constants.MaxHeight + 1, 0);

yield return null;
yield return null;

Assert.IsTrue(gameObject == null);
}

[UnityTest]
public IEnumerator objectShouldBeDestroyedWhenTooLow() {
var gameObject = new GameObject();
var fallingScripts = gameObject.AddComponent<TerrainObjectScript>();

yield return null;

Assert.IsFalse(gameObject == null);

fallingScripts.transform.position = new Vector3(0, Constants.MaxHeight + 1, 0);

yield return null;
yield return null;

Assert.IsTrue(gameObject == null);
}

[UnityTest]
public IEnumerator objectShouldDisconnectWhenRotationIsLarge() {
var gameObject = new GameObject();
var parent = new GameObject();
var fallingScripts = gameObject.AddComponent<TerrainObjectScript>();
gameObject.transform.parent = parent.transform;

yield return null;

Assert.AreEqual(parent.transform, gameObject.transform.parent);
Assert.IsNull(gameObject.GetComponent<Rigidbody>());

fallingScripts.transform.rotation = Quaternion.FromToRotation(Vector3.up, new Vector3(1, 1, 0));

yield return null;

Assert.AreEqual(parent.transform, gameObject.transform.parent);
Assert.IsNull(gameObject.GetComponent<Rigidbody>());

fallingScripts.transform.rotation = fallingScripts.transform.rotation = Quaternion.FromToRotation(Vector3.up, new Vector3(1, 1, 1));

yield return null;

Assert.IsNull(gameObject.transform.parent);
Assert.IsNotNull(gameObject.GetComponent<Rigidbody>());

yield return null;
}
}
3 changes: 3 additions & 0 deletions God Game/Assets/Scripts/Map/BoardScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ private void initializeTiles() {
tileScripts[i + x, j + z].board = this;
tile.name = string.Format("Tile {0}, {1}", i + x, j + z);
tile.transform.parent = transform;

var tree = instantiateObject(treePrefab, Vector3.zero);
tree.transform.parent = tile.transform;
tree.transform.localPosition = new Vector3((float)Assets.Scripts.Base.Randomizer.NextDouble(-5, 5), 0, (float)Assets.Scripts.Base.Randomizer.NextDouble(-5, 5));
tree.AddComponent<TerrainObjectScript>();

var man = instantiateObject(manPrefab, Vector3.zero);
man.transform.parent = tile.transform;
man.transform.localPosition = new Vector3((float)Assets.Scripts.Base.Randomizer.NextDouble(-5, 5), 0, (float)Assets.Scripts.Base.Randomizer.NextDouble(-5, 5));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
using UnityEngine;

public class FreeFallingObject : MonoBehaviour {
// Update is called once per frame
void Update () {
public class TerrainObjectScript : MonoBehaviour {

void Update () {
if (transform.position.y > Constants.MaxHeight || transform.position.y < Constants.MinHeight) {
Destroy(gameObject);
return;
}

if (transform.parent == null) {
return;
}
}

if (Vector3.Angle(transform.up, Vector3.up) > 45) {
TerrainObjectScript.freeObject(transform);
}
}

public static void freeObject(Transform obj) {
obj.parent = null;
Rigidbody rigidBody = obj.gameObject.AddComponent<Rigidbody>();
rigidBody.mass = 5;
rigidBody.useGravity = true;
obj.GetComponent<Collider>().enabled = true;
obj.gameObject.AddComponent<FreeFallingObject>();
}
}
3 changes: 0 additions & 3 deletions God Game/Assets/Scripts/Map/TileScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ public static void adjustChildrenLocation(TileScript tile) {
pointInformationForTile(tile, child.position, out position, out normal);
child.position = position;
child.rotation = Quaternion.FromToRotation(Vector3.up, normal);
if (Vector3.Angle(normal, Vector3.up) > 45) {
FreeFallingObject.freeObject(child);
}
}
}
}

0 comments on commit 8239252

Please sign in to comment.