Skip to content

Commit

Permalink
added some randomizers and updated texture fan
Browse files Browse the repository at this point in the history
  • Loading branch information
RonenNess committed Jun 4, 2018
1 parent ec56fa8 commit 29a3c2c
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 18 deletions.
6 changes: 5 additions & 1 deletion Controls/GameEditor/ObjectsPlacer/ObjectsPlacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Start this instance.
/// </summary>
Expand Down Expand Up @@ -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
Expand Down
22 changes: 6 additions & 16 deletions Graphics/TextureFan/TextureFan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public class TextureFan : MonoBehaviour {
/// How many sides this textured fan should have.
/// </summary>
public int NumberOfSides = 2;

/// <summary>
/// If true, will disable shadows from the extra fan sides.
/// </summary>
public bool DisableShadowsOnExtraSides = false;

// Use this for initialization
void Start () {
Expand All @@ -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<TextureFan> ());

// 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<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;

// set this side as the next object to clone
toClone = newSide;
}

// remove original mesh renderer
Destroy(GetComponent<MeshRenderer>());
}

// Update is called once per frame
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Randomizers/RandomDoodadsGenerator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# RandomDoodadsGenerator

Create random objects spread across the prefab when it spawns.
60 changes: 60 additions & 0 deletions Randomizers/RandomDoodadsGenerator/RandomDoodadsGenerator.cs
Original file line number Diff line number Diff line change
@@ -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 {

/// <summary>
/// Add random doodads on tile.
/// </summary>
public class RandomDoodadsGenerator : MonoBehaviour {

// size of a single tile square
public static readonly float TileSize = 2;

/// <summary>
/// Doodad type and chance to appear.
/// </summary>
[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);

}
}
}
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions Randomizers/RandomScaler/README.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 34 additions & 0 deletions Randomizers/RandomScaler/RandomScaler.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Create random scaling based on object's position.
/// </summary>
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);
}
}
}

0 comments on commit 29a3c2c

Please sign in to comment.