From 29a3c2cbae077caf858727cce094031c80f1738b Mon Sep 17 00:00:00 2001 From: Ronen Date: Mon, 4 Jun 2018 20:50:43 +0200 Subject: [PATCH] added some randomizers and updated texture fan --- .../GameEditor/ObjectsPlacer/ObjectsPlacer.cs | 6 +- Graphics/TextureFan/TextureFan.cs | 22 ++----- README.md | 4 +- Randomizers/RandomDoodadsGenerator/README.md | 3 + .../RandomDoodadsGenerator.cs | 60 +++++++++++++++++++ .../RandomRotator/README.md | 0 .../RandomRotator/RandomRotator.cs | 0 Randomizers/RandomScaler/README.md | 4 ++ Randomizers/RandomScaler/RandomScaler.cs | 34 +++++++++++ 9 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 Randomizers/RandomDoodadsGenerator/README.md create mode 100644 Randomizers/RandomDoodadsGenerator/RandomDoodadsGenerator.cs rename {Graphics => Randomizers}/RandomRotator/README.md (100%) rename {Graphics => Randomizers}/RandomRotator/RandomRotator.cs (100%) create mode 100644 Randomizers/RandomScaler/README.md create mode 100644 Randomizers/RandomScaler/RandomScaler.cs diff --git a/Controls/GameEditor/ObjectsPlacer/ObjectsPlacer.cs b/Controls/GameEditor/ObjectsPlacer/ObjectsPlacer.cs index da468c1..06c2d5d 100644 --- a/Controls/GameEditor/ObjectsPlacer/ObjectsPlacer.cs +++ b/Controls/GameEditor/ObjectsPlacer/ObjectsPlacer.cs @@ -60,6 +60,9 @@ public class ObjectsPlacer : MonoBehaviour // If 0.0, from its top. public float PivotY = 1f; + // if true, will adjust position Y while placing objects based on pivot. + public bool AdjustPositionFromPivotY = false; + /// /// Start this instance. /// @@ -201,7 +204,8 @@ void Update() } // update the position of the object-in-hand, to make it show where you are going to place it - ObjectToPlace.transform.position = collisionPoint + new Vector3(0, -targetMaxExtentHeight + targetMaxExtentHeight * (PivotY * 2f), 0); + var extraY = AdjustPositionFromPivotY ? -targetMaxExtentHeight + targetMaxExtentHeight * (PivotY * 2f) : 0f; + ObjectToPlace.transform.position = collisionPoint + new Vector3(0, extraY, 0); ObjectToPlace.transform.rotation = Quaternion.identity; // if click, leave object where we placed it diff --git a/Graphics/TextureFan/TextureFan.cs b/Graphics/TextureFan/TextureFan.cs index bfb24ef..6b35b91 100644 --- a/Graphics/TextureFan/TextureFan.cs +++ b/Graphics/TextureFan/TextureFan.cs @@ -20,11 +20,6 @@ public class TextureFan : MonoBehaviour { /// How many sides this textured fan should have. /// public int NumberOfSides = 2; - - /// - /// If true, will disable shadows from the extra fan sides. - /// - public bool DisableShadowsOnExtraSides = false; // Use this for initialization void Start () { @@ -36,30 +31,25 @@ void Start () { GameObject toClone = gameObject; // now clone based on number of leafs - for (int i = 0; i < NumberOfSides * 2 - 1; ++i) { + for (int i = 0; i < NumberOfSides * 2; ++i) { // clone self var newSide = Object.Instantiate(toClone); Destroy (newSide.GetComponent ()); - - // if its first run, make sure rotation y starts with 0 - if (i == 0) { - newSide.transform.rotation = Quaternion.identity; - } // rotate it newSide.transform.parent = transform; - newSide.transform.Rotate(new Vector3(0, 1, 0), (180 / NumberOfSides)); + newSide.transform.localRotation = Quaternion.identity; + newSide.transform.Rotate(new Vector3(0, 1, 0), (180 / NumberOfSides) * i); newSide.transform.localScale = Vector3.one; newSide.transform.localPosition = Vector3.zero; - // disable shadow casting - if (DisableShadowsOnExtraSides) - newSide.GetComponent().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; - // set this side as the next object to clone toClone = newSide; } + + // remove original mesh renderer + Destroy(GetComponent()); } // Update is called once per frame diff --git a/README.md b/README.md index 4f60f74..7eb6433 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,12 @@ The following is a list with all scripts found in this repo: - [Controls\SimpleFpsControls\FPSMouseLooking.cs](Controls/SimpleFpsControls) - [Controls\SimpleFpsControls\FPSWalker.cs](Controls/SimpleFpsControls) - [Graphics\Billboard\Billboard.cs](Graphics/Billboard) -- [Graphics\RandomRotator\RandomRotator.cs](Graphics/RandomRotator) - [Graphics\TextureFan\TextureFan.cs](Graphics/TextureFan) - [Misc\ExMath\ExMath.cs](Misc/ExMath) - [Oldies\Controls\GameEditor\ObjectsPickAndPlacer\ObjectsPickAndPlacer.cs](Oldies/Controls/GameEditor/ObjectsPickAndPlacer) +- [Randomizers\RandomDoodadsGenerator\RandomDoodadsGenerator.cs](Randomizers/RandomDoodadsGenerator) +- [Randomizers\RandomRotator\RandomRotator.cs](Randomizers/RandomRotator) +- [Randomizers\RandomScaler\RandomScaler.cs](Randomizers/RandomScaler) - [TileMap3D\Tile.cs](TileMap3D) - [TileMap3D\TileMap.cs](TileMap3D) - [TileMap3D\TilemapEditor.cs](TileMap3D) diff --git a/Randomizers/RandomDoodadsGenerator/README.md b/Randomizers/RandomDoodadsGenerator/README.md new file mode 100644 index 0000000..58c5d5e --- /dev/null +++ b/Randomizers/RandomDoodadsGenerator/README.md @@ -0,0 +1,3 @@ +# RandomDoodadsGenerator + +Create random objects spread across the prefab when it spawns. \ No newline at end of file diff --git a/Randomizers/RandomDoodadsGenerator/RandomDoodadsGenerator.cs b/Randomizers/RandomDoodadsGenerator/RandomDoodadsGenerator.cs new file mode 100644 index 0000000..576165d --- /dev/null +++ b/Randomizers/RandomDoodadsGenerator/RandomDoodadsGenerator.cs @@ -0,0 +1,60 @@ +/** + * Add random doodads on tile. + * + * Author: Ronen Ness. + * Since: 2018. +*/ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Ness.Graphics { + + /// + /// Add random doodads on tile. + /// + public class RandomDoodadsGenerator : MonoBehaviour { + + // size of a single tile square + public static readonly float TileSize = 2; + + /// + /// Doodad type and chance to appear. + /// + [System.Serializable] + public struct DoodadType + { + // doodad prefab + public GameObject Prefab; + + // occurance chance in percent + public float Chance; + } + + // different types and their chances + public DoodadType[] Types; + + // Use this for initialization + void Start () { + + // create random and seed based on position + System.Random rand = new System.Random((int)(transform.position.x * 12.3f * transform.position.z / 1.234f)); + + // spawn doodads + for (int i = 0; i < Types.Length; ++i) { + if ((float)rand.NextDouble() < Types[i].Chance) { + GameObject newObj = Object.Instantiate (Types [i].Prefab); + newObj.transform.parent = transform; + newObj.transform.position = new Vector3( + transform.position.x + -TileSize + (float)rand.NextDouble () * TileSize, + 0, + transform.position.z + -TileSize + (float)rand.NextDouble () * TileSize); + } + } + + // destroy self + Destroy(this); + + } + } +} \ No newline at end of file diff --git a/Graphics/RandomRotator/README.md b/Randomizers/RandomRotator/README.md similarity index 100% rename from Graphics/RandomRotator/README.md rename to Randomizers/RandomRotator/README.md diff --git a/Graphics/RandomRotator/RandomRotator.cs b/Randomizers/RandomRotator/RandomRotator.cs similarity index 100% rename from Graphics/RandomRotator/RandomRotator.cs rename to Randomizers/RandomRotator/RandomRotator.cs diff --git a/Randomizers/RandomScaler/README.md b/Randomizers/RandomScaler/README.md new file mode 100644 index 0000000..74c6789 --- /dev/null +++ b/Randomizers/RandomScaler/README.md @@ -0,0 +1,4 @@ +# RandomScaler + +Scale the object randomly when start, but use a semi-randomness based on position (so it will remain consistent every time level is loaded). +This script is useful to generate randomness in unimportant level objects. \ No newline at end of file diff --git a/Randomizers/RandomScaler/RandomScaler.cs b/Randomizers/RandomScaler/RandomScaler.cs new file mode 100644 index 0000000..84b6b8f --- /dev/null +++ b/Randomizers/RandomScaler/RandomScaler.cs @@ -0,0 +1,34 @@ +/** + * Randomly scales an object, based on position. + * + * Author: Ronen Ness. + * Since: 2018. +*/ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace NesScripts.Graphics +{ + /// + /// Create random scaling based on object's position. + /// + public class RandomScaler : MonoBehaviour { + + // min scale + public float MinScale = 0.8f; + + // max scale + public float MaxScale = 1.2f; + + // Use this for initialization + void Start () { + + System.Random rand = new System.Random((int)(transform.position.x * 1234.25 + transform.position.z * 97.5 + transform.position.y)); + float factor = MinScale + ((float)rand.NextDouble() * (MaxScale - MinScale)); + transform.localScale = transform.localScale * factor; + Destroy (this); + } + } +} \ No newline at end of file