Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes */
- using UnityEngine;
- using System.Collections;
- using System;
- using UnityEngine.EventSystems;
- using System.Collections.Generic;
- /* Block Address Data Structure
- ---------------------------------
- Description:
- Used for storing block addresses relative to an origin point.
- Values:
- 3 Signed Bytes (-128 to 127 range)
- d = Difficulty (Valid Values: 1 to 7, Default/ErrorCheck: -128)
- x = X Position (Valid Values -8 to 12, Default/ErrorCheck: -128)
- y = Y Position (Valid Values -8 to 3, Default/ErrorCheck: -128)
- */
- using System.Security.AccessControl;
- [Serializable] public struct blockAddress {
- public SByte d, x, y;
- public blockAddress (SByte difficulty = -128, SByte xOffset = -128, SByte yOffset = -128) {
- d = difficulty; x = xOffset; y = yOffset;
- }
- }
- /* Difficulty Chance Data Structure
- -------------------------------------
- Description:
- Used for assigning a percent chance on spawning different amount of blocks
- and different difficulty level blocks.
- Values:
- 2 Signed Bytes (-128 to 127 range)
- d = Difficulty (Valid Values: 1 to 7, Default/ErrorCheck: -128)
- c = Chance (Valid Values: 0 to 100, Default/ErrorCheck: -128)
- */
- [Serializable] public struct difficultyChance {
- public SByte d, c;
- public difficultyChance(SByte difficulty = -128, SByte chance = -128) {
- d = difficulty; c = chance;
- }
- }
- /* prefabGenerator Class
- -------------------------------------
- */
- public class prefabGenerator : MonoBehaviour {
- /*============================================================================================================================*/
- /* Public Variables & Test Settings
- /*============================================================================================================================*/
- //Used for difficulty setting from main menu
- public eventSystem eSys;
- //Used to track player position
- public Transform playerPos;
- //Enable test-mode which creates objects on start-up
- public bool createOnStartUp = false;
- //Amount of jumps/platforms to generate ahead of the player (Must be multiple of 2) (createOnStartUp only)
- public int c_leadAheadBy = 4;
- //Difficulty setting to use when generating platforms on start-up (createOnStartUp ONLY)
- [Range(1,4)] public int c_difficultySetting = 4;
- //Starting game object / platform (createOnStartUp ONLY)
- public GameObject c_startPlatform;
- //Used to stop generating platforms once started
- public bool stopGenerating = false;
- //When fully-randomly generating without up/down overrides, prevent blocks from straying too far from 0 with this
- //This means that from 0 it will only go +20 or -20 on y axis when generating a platform
- float maxStray = 20f;
- //Prefab for single cube that we use to construct platforms from
- public GameObject singleCubePrefab;
- //Prefab lists for non-tileable and tileable platforms (SPACE)
- public GameObject[] nonTileablePlatforms_space = new GameObject[2];
- public GameObject[] tileablePlatforms_space = new GameObject[3];
- //Create addressList of all the positions/offsets from origin we can spawn a block at
- public const int blockAddressListEntryCount = 100;
- private blockAddress[] addressList = new blockAddress[blockAddressListEntryCount];
- // Create Chance Arrays (These contain percent chances)
- //Block Difficulty
- public difficultyChance[] easyBlockDifficultySettings = new difficultyChance[7];
- public difficultyChance[] mediumBlockDifficultySettings = new difficultyChance[7];
- public difficultyChance[] hardBlockDifficultySettings = new difficultyChance[7];
- public difficultyChance[] extremeBlockDifficultySettings = new difficultyChance[7];
- //Block Count
- public difficultyChance[] easyBlockCountSettings = new difficultyChance[7];
- public difficultyChance[] mediumBlockCountSettings = new difficultyChance[7];
- public difficultyChance[] hardBlockCountSettings = new difficultyChance[7];
- public difficultyChance[] extremeBlockCountSettings = new difficultyChance[7];
- // Create Chance Tables (These are generated using the arrays)
- //Block Difficulty
- public SByte[] easyBlockDifficultyChancesTable = new SByte[100];
- public SByte[] mediumBlockDifficultyChancesTable = new SByte[100];
- public SByte[] hardBlockDifficultyChancesTable = new SByte[100];
- public SByte[] extremeBlockDifficultyChancesTable = new SByte[100];
- //Block Count
- public SByte[] easyBlockCountChancesTable = new SByte[100];
- public SByte[] mediumBlockCountChancesTable = new SByte[100];
- public SByte[] hardBlockCountChancesTable = new SByte[100];
- public SByte[] extremeBlockCountChancesTable = new SByte[100];
- /*============================================================================================================================*/
- /* Settings - Block Difficulty & Block Count
- /*============================================================================================================================*/
- /* Block Difficulty Settings
- ------------------------------
- Description:
- These settings determine the chances of the generator selecting various difficulty blocks
- at varying difficulty settings. For each difficulty the chances MUST add up to 100 or errors will occur.
- This is the chances for the difficulty of blocks, where 7 is the hardest and 1 is the easiest.
- */
- void blockDifficultySettings() {
- /* Block Difficulty Selection Chances */
- /* Easy Difficulty */
- easyBlockDifficultySettings[0].d = 1; easyBlockDifficultySettings[0].c = 5;
- easyBlockDifficultySettings[1].d = 2; easyBlockDifficultySettings[1].c = 25;
- easyBlockDifficultySettings[2].d = 3; easyBlockDifficultySettings[2].c = 25;
- easyBlockDifficultySettings[3].d = 4; easyBlockDifficultySettings[3].c = 25;
- easyBlockDifficultySettings[4].d = 5; easyBlockDifficultySettings[4].c = 15;
- easyBlockDifficultySettings[5].d = 6; easyBlockDifficultySettings[5].c = 5;
- easyBlockDifficultySettings[6].d = 7; easyBlockDifficultySettings[6].c = 0;
- /* Medium Difficulty */
- mediumBlockDifficultySettings[0].d = 1; mediumBlockDifficultySettings[0].c = 0;
- mediumBlockDifficultySettings[1].d = 2; mediumBlockDifficultySettings[1].c = 20;
- mediumBlockDifficultySettings[2].d = 3; mediumBlockDifficultySettings[2].c = 20;
- mediumBlockDifficultySettings[3].d = 4; mediumBlockDifficultySettings[3].c = 25;
- mediumBlockDifficultySettings[4].d = 5; mediumBlockDifficultySettings[4].c = 25;
- mediumBlockDifficultySettings[5].d = 6; mediumBlockDifficultySettings[5].c = 10;
- mediumBlockDifficultySettings[6].d = 7; mediumBlockDifficultySettings[6].c = 0;
- /* Hard Difficulty */
- hardBlockDifficultySettings[0].d = 1; hardBlockDifficultySettings[0].c = 0;
- hardBlockDifficultySettings[1].d = 2; hardBlockDifficultySettings[1].c = 5;
- hardBlockDifficultySettings[2].d = 3; hardBlockDifficultySettings[2].c = 10;
- hardBlockDifficultySettings[3].d = 4; hardBlockDifficultySettings[3].c = 30;
- hardBlockDifficultySettings[4].d = 5; hardBlockDifficultySettings[4].c = 30;
- hardBlockDifficultySettings[5].d = 6; hardBlockDifficultySettings[5].c = 15;
- hardBlockDifficultySettings[6].d = 7; hardBlockDifficultySettings[6].c = 10;
- /* Extreme Difficulty */
- extremeBlockDifficultySettings[0].d = 1; extremeBlockDifficultySettings[0].c = 0;
- extremeBlockDifficultySettings[1].d = 2; extremeBlockDifficultySettings[1].c = 0;
- extremeBlockDifficultySettings[2].d = 3; extremeBlockDifficultySettings[2].c = 0;
- extremeBlockDifficultySettings[3].d = 4; extremeBlockDifficultySettings[3].c = 25;
- extremeBlockDifficultySettings[4].d = 5; extremeBlockDifficultySettings[4].c = 25;
- extremeBlockDifficultySettings[5].d = 6; extremeBlockDifficultySettings[5].c = 30;
- extremeBlockDifficultySettings[6].d = 7; extremeBlockDifficultySettings[6].c = 20;
- }
- /* Block Count Settings
- ------------------------------
- Description:
- These settings determine the chances of the generator selecting various difficulty blocks
- at varying difficulty settings. For each difficulty the chances MUST add up to 100 or errors will occur.
- This is the chances for the count of blocks, where 7 spawns 7 blocks and 1 spawns 1 block.
- */
- void blockCountSettings() {
- /* Block Amount Selection Chances */
- easyBlockCountSettings[0].d = 1; easyBlockCountSettings[0].c = 0;
- easyBlockCountSettings[1].d = 2; easyBlockCountSettings[1].c = 10;
- easyBlockCountSettings[2].d = 3; easyBlockCountSettings[2].c = 20;
- easyBlockCountSettings[3].d = 4; easyBlockCountSettings[3].c = 20;
- easyBlockCountSettings[4].d = 5; easyBlockCountSettings[4].c = 20;
- easyBlockCountSettings[5].d = 6; easyBlockCountSettings[5].c = 20;
- easyBlockCountSettings[6].d = 7; easyBlockCountSettings[6].c = 10;
- /* Medium Difficulty */
- mediumBlockCountSettings[0].d = 1; mediumBlockCountSettings[0].c = 5;
- mediumBlockCountSettings[1].d = 2; mediumBlockCountSettings[1].c = 25;
- mediumBlockCountSettings[2].d = 3; mediumBlockCountSettings[2].c = 25;
- mediumBlockCountSettings[3].d = 4; mediumBlockCountSettings[3].c = 15;
- mediumBlockCountSettings[4].d = 5; mediumBlockCountSettings[4].c = 15;
- mediumBlockCountSettings[5].d = 6; mediumBlockCountSettings[5].c = 10;
- mediumBlockCountSettings[6].d = 7; mediumBlockCountSettings[6].c = 5;
- /* Hard Difficulty */
- hardBlockCountSettings[0].d = 1; hardBlockCountSettings[0].c = 20;
- hardBlockCountSettings[1].d = 2; hardBlockCountSettings[1].c = 25;
- hardBlockCountSettings[2].d = 3; hardBlockCountSettings[2].c = 30;
- hardBlockCountSettings[3].d = 4; hardBlockCountSettings[3].c = 10;
- hardBlockCountSettings[4].d = 5; hardBlockCountSettings[4].c = 10;
- hardBlockCountSettings[5].d = 6; hardBlockCountSettings[5].c = 5;
- hardBlockCountSettings[6].d = 7; hardBlockCountSettings[6].c = 0;
- /* Extreme Difficulty */
- extremeBlockCountSettings[0].d = 1; extremeBlockCountSettings[0].c = 30;
- extremeBlockCountSettings[1].d = 2; extremeBlockCountSettings[1].c = 40;
- extremeBlockCountSettings[2].d = 3; extremeBlockCountSettings[2].c = 20;
- extremeBlockCountSettings[3].d = 4; extremeBlockCountSettings[3].c = 10;
- extremeBlockCountSettings[4].d = 5; extremeBlockCountSettings[4].c = 0;
- extremeBlockCountSettings[5].d = 6; extremeBlockCountSettings[5].c = 0;
- extremeBlockCountSettings[6].d = 7; extremeBlockCountSettings[6].c = 0;
- }
- /*============================================================================================================================*/
- /* Functions
- /*============================================================================================================================*/
- /* Start-Up (Initialization)
- -------------------------------------
- Description:
- Builds key public values for later use (addressList and chance tables).
- If createOnStartup enabled, run generatePrefabs with given public variables.
- */
- void Start () {
- //We do it this way to make sure we always get the correct game controller
- eSys = GameObject.Find ("GameController").GetComponent<gameController> ().lvlCtrl.GetComponent<eventSystem> ();
- c_difficultySetting = eSys.arcadeDifficulty;
- //Build the address list using pre-determined values for difficulty and xy
- buildAddressList ();
- //Create chance tables from difficulty settings
- buildSettingsChanceTables ();
- //Recommended use...
- if (createOnStartUp) {
- StartCoroutine (prefabSpawnLoop ());
- };
- //Prefab spawning loop co-routine
- } IEnumerator prefabSpawnLoop() {
- //2 is real minimum lead ahead
- c_leadAheadBy = Math.Max(c_leadAheadBy, 3);
- //Create 2 sets of platforms
- GameObject oldestSetLastPlatform = generatePrefabs (c_leadAheadBy / 2, c_difficultySetting, new Vector2(c_startPlatform.transform.position.x, c_startPlatform.transform.position.y));
- GameObject youngestSetLastPlatform = generatePrefabs (c_leadAheadBy / 2, c_difficultySetting, new Vector2(oldestSetLastPlatform.transform.position.x, oldestSetLastPlatform.transform.position.y));
- //Loop until stopped
- while (!stopGenerating) {
- //Delete all that are more than 30 units behind where the player is
- GameObject[] foundPlats = GameObject.FindGameObjectsWithTag ("platform");
- foreach (GameObject plat in foundPlats) { if (plat.transform.position.x < (playerPos.position.x - 30f)) { Destroy(plat); }; };
- //If player has passed the oldest set and is now on the youngest set platforms (giving 3f extra leeway)
- if (playerPos.position.x > (oldestSetLastPlatform.transform.position.x - 3f)) {
- //Create 2 new sets
- oldestSetLastPlatform = generatePrefabs (c_leadAheadBy / 2, c_difficultySetting, new Vector2(youngestSetLastPlatform.transform.position.x, youngestSetLastPlatform.transform.position.y));
- youngestSetLastPlatform = generatePrefabs (c_leadAheadBy / 2, c_difficultySetting, new Vector2(oldestSetLastPlatform.transform.position.x, oldestSetLastPlatform.transform.position.y));
- }
- //Wait before re-checking
- yield return new WaitForSeconds (1f);
- };
- }
- /* Generate Platform
- --------------------
- Description:
- This generates a platform offset from the given origin based on input difficulty and the difficultySettings.
- It starts at the given vector2 and generates a platform of 1-7 blocks (can be overriden). These platforms
- are a jump away from the current one and none are directly connected.
- Inputs (Non-Optional):
- difficultySetting (sByte): Range from 1 (easy), 2 (medium), 3 (hard), 4 (extreme)
- originPoint (Vector2): Where to start (first platform will be created based on this origin point)
- Inputs (Optional Overrides):
- blockCountOverride (sByte): Amount of blocks to spawn at location chosen
- UpOnlyOverride (bool): Whether or not to only spawn blocks with a POSITIVE Y value.
- DownOnlyOverride (bool): Whether or not to only spawn blocks with a NEGATIVE Y value.
- LeftOnlyOverride (bool): Whether or not to only spawn blocks with a NEGATIVE X value.
- RightOnlyOverride (bool): Whether or not to only spawn blocks with a POSITIVE X value.
- */
- GameObject generatePlatform(SByte difficultySetting, Vector2 originPoint, SByte blockCountOverride = 0, bool upOnlyOverride = false, bool downOnlyOverride = false, bool leftOnlyOverride = false, bool rightOnlyOverride = false) {
- /* Variable Declarations & Set-Up
- -----------------------------------
- */
- //Used to store the chosen offset address from the addressList table
- blockAddress addressToSpawnAt;
- //Used to store the address once it has been converted from offset (addressToSpawnAt)
- Vector2 finalAddress;
- //Used to store the last-created object
- GameObject createdObject = null;
- //Selected amount of blocks to spawn (selected from chance table)
- SByte amntOfBlocksToSpawn;
- //Selected block difficulty level (selected from chance table)
- SByte blockDifficulty;
- //Create chance table arrays
- SByte[] difficultyChanceTable;
- SByte[] blockCountChanceTable;
- //Based on input difficulty setting, set our chance table array values (see buildSettingsChanceTables for more info)
- switch (difficultySetting) {
- case 1: difficultyChanceTable = easyBlockDifficultyChancesTable; blockCountChanceTable = easyBlockCountChancesTable; break;
- case 2: difficultyChanceTable = mediumBlockDifficultyChancesTable; blockCountChanceTable = mediumBlockCountChancesTable; break;
- case 3: difficultyChanceTable = hardBlockDifficultyChancesTable; blockCountChanceTable = hardBlockCountChancesTable; break;
- case 4: difficultyChanceTable = extremeBlockDifficultyChancesTable; blockCountChanceTable = extremeBlockCountChancesTable; break;
- default: difficultyChanceTable = mediumBlockDifficultyChancesTable; blockCountChanceTable = mediumBlockCountChancesTable; break;
- }
- /* Select Difficulty & Amount of Blocks to Spawn
- --------------------------------------------------
- */
- //Find random address to start with (prevents compiler errors)
- addressToSpawnAt = findRandomAddressInRange (0, (blockAddressListEntryCount - 1));
- //Select difficulty for block using chance table generated from difficulty settings
- blockDifficulty = Convert.ToSByte(UnityEngine.Random.Range(0,99));
- blockDifficulty = difficultyChanceTable [blockDifficulty];
- //Check amount of block ovverride value
- if (blockCountOverride > 0) {
- //Set amount of blocks to spawn from ovverride
- amntOfBlocksToSpawn = blockCountOverride;
- } else {
- //Pick block amount using chance table generated from difficulty settings
- amntOfBlocksToSpawn = Convert.ToSByte(UnityEngine.Random.Range(0,99));
- amntOfBlocksToSpawn = blockCountChanceTable [amntOfBlocksToSpawn];
- };
- /* Check Up Down Left Right Overrides
- --------------------------------------------------
- */
- SByte minX = -12; SByte maxX = 12; SByte minY = -12; SByte maxY = 12;
- //Up
- if (upOnlyOverride) { minY = 0; };
- //Down
- if (downOnlyOverride) { maxY = 0; };
- //Left
- if (leftOnlyOverride) { maxX = 0; };
- //Right
- if (rightOnlyOverride) { minX = 0; };
- /* Select Block Spawn Address using Difficulty & Overrides
- -----------------------------------------------------------
- */
- //If no override for up/down are active then keep the map flat and level
- if (!upOnlyOverride && !downOnlyOverride) {
- /* To prevent random chance taking the map upwards/downwards until we're at +-1000 in the y axis we need
- to to use a maximum 'stray' from 0 value for y. When we're at 0y they up/down have equal (50% chance).
- As we approach the 'stray' value the chance should adjust to prevent it reaching the min/max. */
- float curY = originPoint.y;
- float upChance = 50f;
- float modifier = 0f;
- //Create chance using minY,maxY,50% starting baseline, and current origin Y pos
- modifier = ((1 / (Math.Abs (maxStray) / Math.Abs (curY))) / 2) * 100;
- //Check if y is positive or negative/0 and add/take depending
- upChance = (curY > 0f) ? (upChance - modifier) : (upChance + modifier);
- //Generate random number between and inclusive of 1 and 100
- //And if it is less than the upChance then generate upwards, if not then downwards
- //Format: (Desired Difficulty Index, Min X Offset, Max X Offset, Min Y Offset, Max Y Offset, AddressList Range to Search In Start & End Array Indexes)
- addressToSpawnAt = (UnityEngine.Random.Range (1, 100) < upChance) ? (findAddress (blockDifficulty, minX, maxX, 0, 12, 0, (blockAddressListEntryCount - 1))) : (findAddress (blockDifficulty, minX, maxX, -8, 0, 0, (blockAddressListEntryCount - 1)));
- } else {
- //We have overrides for up/down, ignore flat-level-keeping code/maxStray value
- findAddress (blockDifficulty, minX, maxX, minY, maxY, 0, (blockAddressListEntryCount - 1));
- };
- //Do maths on address to get real world pos (have to convert from SByte)
- finalAddress.x = originPoint.x + Convert.ToSingle(addressToSpawnAt.x);
- finalAddress.y = originPoint.y + Convert.ToSingle(addressToSpawnAt.y);
- /* Spawn Platform into Level and Return it
- -----------------------------------------------------------
- */
- //Spawn blocks using amount of blocks to spawn chosen from chance table
- for (int j = 0; j < amntOfBlocksToSpawn; j++) {
- //Instantiate a cube, add j (for-index (starts at 0)) to the position on each iteration
- // So that the squares sit in a line from left to right
- createdObject = (GameObject) Instantiate (singleCubePrefab, new Vector3(finalAddress.x + j, finalAddress.y), Quaternion.identity);
- //Update origin point for next iteration to last objects position
- originPoint = createdObject.transform.position;
- }
- //Return last created object to calling code
- return createdObject;
- }
- /* Generate Prefabs
- ---------------------
- Description:
- This generates prefabs based on input difficulty and the difficultySettings.
- It starts at the given vector2 and generates prefabs in a loop until it reaches the prefabsToGenerate number of prefabs created.
- Prefabs here are defined as platforms of 1 to 7 blocks.
- Inputs:
- prefabsToGenerate (int): Amount of platforms/prefabs to create
- inputDifficulty (int): Range from 1 (easy), 2 (medium), 3 (hard), 4 (extreme)
- originPoint (Vector2): Where to start (first platform will be created based on this origin point)
- */
- GameObject generatePrefabs(int prefabsToGenerate, int inputDifficulty, Vector2 originPoint) {
- /* Variable Declarations & Set-Up
- -----------------------------------
- */
- //Used to store the chosen offset address from the addressList table
- blockAddress addressToSpawnAt;
- //Used to store the address once it has been converted from offset (addressToSpawnAt)
- Vector2 finalAddress;
- //Used to store the last-created object
- GameObject createdObject = null;
- //Selected amount of blocks to spawn (selected from chance table)
- SByte amntOfBlocksToSpawn;
- //Selected block difficulty level (selected from chance table)
- SByte blockDifficulty;
- //Create chance table arrays
- SByte[] difficultyChanceTable;
- SByte[] blockCountChanceTable;
- //Based on input difficulty setting, set our chance table array values (see buildSettingsChanceTables for more info)
- switch (inputDifficulty) {
- case 1: difficultyChanceTable = easyBlockDifficultyChancesTable; blockCountChanceTable = easyBlockCountChancesTable; break;
- case 2: difficultyChanceTable = mediumBlockDifficultyChancesTable; blockCountChanceTable = mediumBlockCountChancesTable; break;
- case 3: difficultyChanceTable = hardBlockDifficultyChancesTable; blockCountChanceTable = hardBlockCountChancesTable; break;
- case 4: difficultyChanceTable = extremeBlockDifficultyChancesTable; blockCountChanceTable = extremeBlockCountChancesTable; break;
- default: difficultyChanceTable = mediumBlockDifficultyChancesTable; blockCountChanceTable = mediumBlockCountChancesTable; break;
- }
- /* Prefab-Spawning-Loop
- -------------------------
- */
- //Create and set this to false for start-up of loop
- bool disableDownForPlatform = false;
- //Loop to spawn prefabs/platforms, each iteration spawns one, run until input number reached.
- for (int i = 0; i < prefabsToGenerate; i++) {
- //Find random address to start with (prevents compiler errors)
- addressToSpawnAt = findRandomAddressInRange (0, (blockAddressListEntryCount - 1));
- //Select difficulty for block using chance table generated from difficulty settings
- blockDifficulty = Convert.ToSByte(UnityEngine.Random.Range(0,99));
- blockDifficulty = difficultyChanceTable [blockDifficulty];
- //Pick block amount using chance table generated from difficulty settings
- amntOfBlocksToSpawn = Convert.ToSByte(UnityEngine.Random.Range(0,99));
- amntOfBlocksToSpawn = blockCountChanceTable [amntOfBlocksToSpawn];
- //If first prefab from loop then never go left
- if (i == 0) {
- //Range is 3 to end as first 3 are only blocks with negative/0 for an x value
- addressToSpawnAt = findAddress (blockDifficulty, 0, 12, -8, 12, 3, (blockAddressListEntryCount - 1));
- } else {
- //Not first block from loop, can go left now :)
- //If we don't want to disable down for this platform and we can use the full range of addresses
- if (!disableDownForPlatform) {
- /* To prevent random chance taking the map upwards/downwards until we're at +-1000 in the y axis we need
- to to use a maximum 'stray' from 0 value for y. When we're at 0y they up/down have equal (50% chance).
- As we approach the 'stray' value the chance should adjust to prevent it reaching the min/max. */
- float maxStray = 20;
- float curY = originPoint.y;
- float upChance = 50;
- float modifier = 0;
- //Create chance using minY,maxY,50% starting baseline, and current origin Y pos
- modifier = ((1 / (Math.Abs(maxStray) / Math.Abs(curY))) / 2) * 100;
- //Check if y is positive or negative/0 and add/take depending
- upChance = (curY > 0f) ? (upChance - modifier) : (upChance + modifier);
- //Generate random number between and inclusive of 1 and 100
- //And if it is less than the upChance then generate upwards, if not then downwards
- //Format: (Desired Difficulty Index, Min X Offset, Max X Offset, Min Y Offset, Max Y Offset, AddressList Range to Search In Start & End Array Indexes)
- addressToSpawnAt = (UnityEngine.Random.Range (1, 100) < upChance) ? (findAddress (blockDifficulty, -8, 12, 0, 12, 0, (blockAddressListEntryCount - 1))) : (findAddress (blockDifficulty, -8, 12, -8, 0, 0, (blockAddressListEntryCount - 1)));
- } else {
- //Pick random address, using only values with positive Y Value since disableDownForNextPlatform is ON for this platform.
- //Format: (Desired Difficulty Index, Min X Offset, Max X Offset, Min Y Offset, Max Y Offset, AddressList Range to Search In Start & End Array Indexes)
- addressToSpawnAt = findAddress (blockDifficulty, -8, 12, 1, 12, 0, (blockAddressListEntryCount - 1));
- };
- //If address is same as either of first 3 entries in list (which are blocks directly above/left of origin)
- if (addressToSpawnAt.x == addressList[0].x || addressToSpawnAt.y == addressList[0].y || addressToSpawnAt.x == addressList[1].x || addressToSpawnAt.y == addressList[1].y || addressToSpawnAt.x == addressList[2].x || addressToSpawnAt.y == addressList[2].y) {
- disableDownForPlatform = true;
- } else {
- disableDownForPlatform = false;
- };
- };
- //Do maths on address to get real world pos (have to convert from SByte)
- finalAddress.x = originPoint.x + Convert.ToSingle(addressToSpawnAt.x);
- finalAddress.y = originPoint.y + Convert.ToSingle(addressToSpawnAt.y);
- // If amount of blocks to spawn is 1, then pick one of the single platforms that aren't tilable
- // If amount of blocks is 2 then spawn one with leftEnd and one with rightEnd from tilable set
- // If amount of blocks is 3 then spawn one with leftEnd, 1x middle, and one with rightEnd
- // If amount of blocks is 4 then spawn one with leftEnd, 2x middle, and one with rightEnd
- // If amount of blocks is 5 then spawn one with leftEnd, 3x middle and one with rightEnd
- // etc
- GameObject prefabToSpawn = singleCubePrefab;
- switch (amntOfBlocksToSpawn) {
- case 1:
- //Pick non-tileable platform at random
- if (randBool ()) {
- prefabToSpawn = nonTileablePlatforms_space [0];
- } else {
- prefabToSpawn = nonTileablePlatforms_space [1];
- };
- //Spawn it
- createdObject = (GameObject) Instantiate (prefabToSpawn, new Vector3(finalAddress.x, finalAddress.y), Quaternion.identity);
- //Parent it to levelController
- createdObject.transform.SetParent (this.gameObject.transform);
- break;
- case 2:
- //Spawn left end
- createdObject = spawnRandomTileablePiece (finalAddress, true, false);
- //Spawn right end
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 1, finalAddress.y), false, true);
- break;
- case 3:
- //Spawn left end
- createdObject = spawnRandomTileablePiece (finalAddress, true, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 1, finalAddress.y), false, false);
- //Spawn right end
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 2, finalAddress.y), false, true);
- break;
- case 4:
- //Spawn left end
- createdObject = spawnRandomTileablePiece (finalAddress, true, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 1, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 2, finalAddress.y), false, false);
- //Spawn right end
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 3, finalAddress.y), false, true);
- break;
- case 5:
- //Spawn left end
- createdObject = spawnRandomTileablePiece (finalAddress, true, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 1, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 2, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 3, finalAddress.y), false, false);
- //Spawn right end
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 4, finalAddress.y), false, true);
- break;
- case 6:
- //Spawn left end
- createdObject = spawnRandomTileablePiece (finalAddress, true, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 1, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 2, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 3, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 4, finalAddress.y), false, false);
- //Spawn right end
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 5, finalAddress.y), false, true);
- break;
- case 7:
- //Spawn left end
- createdObject = spawnRandomTileablePiece (finalAddress, true, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 1, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 2, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 3, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 4, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 5, finalAddress.y), false, false);
- //Spawn right end
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 6, finalAddress.y), false, true);
- break;
- case 8:
- //Spawn left end
- createdObject = spawnRandomTileablePiece (finalAddress, true, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 1, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 2, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 3, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 4, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 5, finalAddress.y), false, false);
- //Spawn middle
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 6, finalAddress.y), false, false);
- //Spawn right end
- createdObject = spawnRandomTileablePiece (new Vector3(finalAddress.x + 7, finalAddress.y), false, true);
- break;
- default:
- break;
- }
- //Update origin point for next iteration to last objects position
- originPoint = createdObject.transform.position;
- /*// old code for single cube spawning
- //Spawn blocks using amount of blocks to spawn chosen from chance table
- for (int j = 0; j < amntOfBlocksToSpawn; j++) {
- //Instantiate a cube, add j (for-index (starts at 0)) to the position on each iteration
- // So that the squares sit in a line from left to right
- createdObject = (GameObject) Instantiate (singleCubePrefab, new Vector3(finalAddress.x + j, finalAddress.y), Quaternion.identity);
- //Update origin point for next iteration to last objects position
- originPoint = createdObject.transform.position;
- }
- //*/
- } //End of prefab spawning loop.
- //Return last created object to calling code
- return createdObject;
- }
- /* Function: spawnRandomTilablePiece
- Description: Spawns random space tilable piece it at the input pos with input end pieces
- */
- private GameObject spawnRandomTileablePiece(Vector2 spawnPos, bool enableLeftEnd, bool enableRightEnd) {
- //Pick random piece
- GameObject prefabToSpawn = pickRandomTilablePiece();
- //Spawn it at input pos
- GameObject obj = (GameObject)Instantiate(prefabToSpawn, spawnPos, Quaternion.identity);
- //Parent it to levelController
- obj.transform.SetParent (this.gameObject.transform);
- //Randomly flip it
- if (randBool ()) {
- //Flip it
- Vector3 objScale = obj.transform.localScale; objScale.x *= -1; obj.transform.localScale = objScale;
- //Flip end bools
- if (enableLeftEnd) { enableLeftEnd = false; enableRightEnd = true; } else if (enableRightEnd) { enableRightEnd = false; enableLeftEnd = true; };
- //Move it to correct pos (plus one on x axis)
- obj.transform.position = new Vector2 (obj.transform.position.x + 1, obj.transform.position.y);
- };
- //Enable end pieces
- if (enableLeftEnd || enableRightEnd) {
- enableEndPiece(enableLeftEnd, enableRightEnd, obj);
- }
- //Return object
- return obj;
- }
- /* Function: pickRandomTilablePiece
- Description: Picks a random tileable piece from the set of 4
- */
- private GameObject pickRandomTilablePiece() {
- GameObject prefabToSpawn = singleCubePrefab;
- if (randBool())
- {
- if (randBool())
- {
- prefabToSpawn = tileablePlatforms_space[0];
- }
- else
- {
- prefabToSpawn = tileablePlatforms_space[1];
- };
- }
- else
- {
- prefabToSpawn = tileablePlatforms_space[2];
- };
- return prefabToSpawn;
- }
- /* Function: enableEndPiece
- Description: Enables left or right end pieces based on inputs
- */
- private void enableEndPiece(bool left, bool right, GameObject platformObject) {
- if (left) {
- //Enable leftEnd piece
- GameObject leftEndParent = platformObject.transform.FindChild("endPieces").FindChild ("leftEnd").gameObject;
- leftEndParent.SetActive(true);
- }
- if (right) {
- //Enable rightEnd piece
- GameObject rightEndParent = platformObject.transform.FindChild("endPieces").FindChild ("rightEnd").gameObject;
- rightEndParent.SetActive(true);
- }
- }
- /* Function: randBool
- Description: 50/50 chance of returning true or false
- */
- private bool randBool() { return UnityEngine.Random.value > 0.5f; }
- /* Find Random Address In Range
- -------------------------------------
- Description:
- Picks a random address within the given range.
- Inputs:
- rangeStart (int): The start of the address range (0-(blockAddressListEntryCount - 1)) to search within
- rangeEnd (int): The end of the address range (0-(blockAddressListEntryCount - 1)) to search within
- Outputs:
- blockAddress: An address within the range
- */
- blockAddress findRandomAddressInRange (int rangeStart, int rangeEnd) {
- int randomAddressID;
- randomAddressID = UnityEngine.Random.Range(rangeStart,rangeEnd);
- return (addressList[randomAddressID]);
- }
- /* Find Address
- -------------------------------------
- Description:
- Strips the addressList of all values that don't meet the given criteria, then selects randomly from the new list.
- Inputs:
- blockDifficulty (SByte): The difficulty (1-7) of block to search for.
- Min X Offset (SByte): The minimum x offset position
- Max X Offset (SByte): The maximum x offset position
- Min Y Offset (SByte): The minimum y offset position
- Max Y Offset (SByte): The maximum y offset position
- rangeStart (int): The start of the address range (0-(blockAddressListEntryCount - 1)) to search within
- rangeEnd (int): The end of the address range (0-(blockAddressListEntryCount - 1)) to search within
- Outputs:
- blockAddress: The address found that matches the given criteria
- */
- blockAddress findAddress(SByte blockDifficulty, SByte min_x, SByte max_x, SByte min_y, SByte max_y, int rangeStart, int rangeEnd) {
- //Create temporary addressList to store valid addresses we find
- List<blockAddress> tempAddressList = new List<blockAddress>();
- //For each address in the user specified range
- for (int i = rangeStart; i < (rangeEnd - 1); i++) {
- //Get address as var
- blockAddress currentBlock = addressList[i];
- //If difficulty does not match, move on to next address
- if (currentBlock.d != blockDifficulty) { continue; };
- //If position does not meet criteria move on to next address
- if (currentBlock.x > max_x || currentBlock.x < min_x || currentBlock.y > max_y || currentBlock.y < min_y) { continue; };
- //If we got this far the address is valid, add it to our tempAddressList
- tempAddressList.Add(currentBlock);
- };
- int addressListCount = tempAddressList.Count;
- //Ensure we have values in our list
- if (addressListCount == 0) {
- return addressList [0];
- } else {
- int randSel = 0; randSel = UnityEngine.Random.Range(0,addressListCount);
- return tempAddressList[randSel];
- };
- }
- /* Create Chance Table
- -------------------------------------
- Description:
- Generates arrays with 100 values using the settings values.
- If in the settings a block difficulty of 7 has a 15% chance it will occupy 15/100 values.
- Inputs:
- inputChances (difficultyChance[]): The input chance settings to use when creating the table
- Outputs:
- SByte[] array of repeated values to represent their chances when selecting randomly from them all.
- */
- SByte[] createChanceTable(difficultyChance[] inputChances) {
- //Create output var
- SByte[] table = new SByte[100];
- //Create currentIndex var (Used to maintain position in array)
- int currentIndex = 0;
- /* Generate Chance Table */
- //For each set of difficulty and chance values in the input chances array
- foreach (difficultyChance bD in inputChances) {
- //Loop <chance> (eg 10 for 10%) amount of times, adding the difficulty as an entry to the array each time
- // eg adding 10 of the difficulty (3 for example) to the array for output
- for (int i = 0; (i < bD.c); i++) { table [currentIndex] = bD.d; currentIndex++; }
- }
- //Return created table
- return table;
- }
- /* Build Settings & Chance Tables
- -------------------------------------
- Description:
- Builds settings tables using hard-coded values and then uses them to create chance tables. This is for
- the difficulty of block (1-7) it will select at varying difficulty settings (easy-extreme) and how many
- blocks it will choose to spawn at that location (1-7) depending on difficulty settings (easy-extreme).
- */
- void buildSettingsChanceTables() {
- //Build settings for difficulty and block count
- blockDifficultySettings(); blockCountSettings();
- //Build chance tables for difficulty and block count
- blockDifficultyCountChanceTables();
- }
- /* Block Difficulty & Count Chance Tables
- -------------------------------------------
- Description:
- Using the blockDifficultySettings we create tables/arrays of 100 values.
- With each difficulty/block amount being represented as many times as is listed
- in its blockDifficultySettings chance value. Eg if 1 block has a 5 percent chance
- of spawning in medium mode then '1' will occupy 5/100 values in the table, therefore
- selecting a random value from that table will yeild a 5% chance of getting a 1.
- */
- void blockDifficultyCountChanceTables() {
- /* Create Chance Tables for Block Difficulty */
- easyBlockDifficultyChancesTable = createChanceTable (easyBlockDifficultySettings);
- mediumBlockDifficultyChancesTable = createChanceTable (mediumBlockDifficultySettings);
- hardBlockDifficultyChancesTable = createChanceTable (hardBlockDifficultySettings);
- extremeBlockDifficultyChancesTable = createChanceTable (extremeBlockDifficultySettings);
- /* Create Chance Tables for Block Count*/
- easyBlockCountChancesTable = createChanceTable (easyBlockCountSettings);
- mediumBlockCountChancesTable = createChanceTable (mediumBlockCountSettings);
- hardBlockCountChancesTable = createChanceTable (hardBlockCountSettings);
- extremeBlockCountChancesTable = createChanceTable (extremeBlockCountSettings);
- }
- /* Build Address List
- -------------------------------------
- Description:
- Created using tested max-jump range values. See diagram for more info.
- Format is: blockDifficulty.xOffset.yOffset
- */
- void buildAddressList() {
- int i = 0; //Use int for array index and increment it after each value
- addressList [i] = new blockAddress (5,-2,3); i++;
- addressList [i] = new blockAddress (5,-1,3); i++;
- addressList [i] = new blockAddress (5,0,3); i++;
- addressList [i] = new blockAddress (5,1,3); i++;
- addressList [i] = new blockAddress (6,2,3); i++;
- addressList [i] = new blockAddress (6,3,3); i++;
- addressList [i] = new blockAddress (6,4,3); i++;
- addressList [i] = new blockAddress (6,5,3); i++;
- addressList [i] = new blockAddress (6,6,3); i++;
- addressList [i] = new blockAddress (7,7,3); i++;
- addressList [i] = new blockAddress (7,8,3); i++;
- addressList [i] = new blockAddress (4,1,2); i++;
- addressList [i] = new blockAddress (4,2,2); i++;
- addressList [i] = new blockAddress (4,3,2); i++;
- addressList [i] = new blockAddress (4,4,2); i++;
- addressList [i] = new blockAddress (5,5,2); i++;
- addressList [i] = new blockAddress (6,6,2); i++;
- addressList [i] = new blockAddress (6,7,2); i++;
- addressList [i] = new blockAddress (7,8,2); i++;
- addressList [i] = new blockAddress (7,9,2); i++;
- addressList [i] = new blockAddress (3,3,1); i++;
- addressList [i] = new blockAddress (3,4,1); i++;
- addressList [i] = new blockAddress (3,5,1); i++;
- addressList [i] = new blockAddress (4,6,1); i++;
- addressList [i] = new blockAddress (5,7,1); i++;
- addressList [i] = new blockAddress (6,8,1); i++;
- addressList [i] = new blockAddress (6,9,1); i++;
- addressList [i] = new blockAddress (7,10,1); i++;
- addressList [i] = new blockAddress (1,3,0); i++;
- addressList [i] = new blockAddress (2,4,0); i++;
- addressList [i] = new blockAddress (3,5,0); i++;
- addressList [i] = new blockAddress (3,6,0); i++;
- addressList [i] = new blockAddress (4,7,0); i++;
- addressList [i] = new blockAddress (4,8,0); i++;
- addressList [i] = new blockAddress (5,9,0); i++;
- addressList [i] = new blockAddress (6,10,0); i++;
- addressList [i] = new blockAddress (7,11,0); i++;
- addressList [i] = new blockAddress (1,3,-1); i++;
- addressList [i] = new blockAddress (2,4,-1); i++;
- addressList [i] = new blockAddress (2,5,-1); i++;
- addressList [i] = new blockAddress (3,6,-1); i++;
- addressList [i] = new blockAddress (4,7,-1); i++;
- addressList [i] = new blockAddress (4,8,-1); i++;
- addressList [i] = new blockAddress (5,9,-1); i++;
- addressList [i] = new blockAddress (6,10,-1); i++;
- addressList [i] = new blockAddress (6,11,-1); i++;
- addressList [i] = new blockAddress (1,3,-2); i++;
- addressList [i] = new blockAddress (1,4,-2); i++;
- addressList [i] = new blockAddress (2,5,-2); i++;
- addressList [i] = new blockAddress (3,6,-2); i++;
- addressList [i] = new blockAddress (3,7,-2); i++;
- addressList [i] = new blockAddress (4,8,-2); i++;
- addressList [i] = new blockAddress (5,9,-2); i++;
- addressList [i] = new blockAddress (5,10,-2); i++;
- addressList [i] = new blockAddress (6,11,-2); i++;
- addressList [i] = new blockAddress (1,3,-3); i++;
- addressList [i] = new blockAddress (1,4,-3); i++;
- addressList [i] = new blockAddress (2,5,-3); i++;
- addressList [i] = new blockAddress (2,6,-3); i++;
- addressList [i] = new blockAddress (3,7,-3); i++;
- addressList [i] = new blockAddress (3,8,-3); i++;
- addressList [i] = new blockAddress (4,9,-3); i++;
- addressList [i] = new blockAddress (5,10,-3); i++;
- addressList [i] = new blockAddress (5,11,-3); i++;
- addressList [i] = new blockAddress (1,3,-4); i++;
- addressList [i] = new blockAddress (1,4,-4); i++;
- addressList [i] = new blockAddress (2,5,-4); i++;
- addressList [i] = new blockAddress (2,6,-4); i++;
- addressList [i] = new blockAddress (3,7,-4); i++;
- addressList [i] = new blockAddress (3,8,-4); i++;
- addressList [i] = new blockAddress (4,9,-4); i++;
- addressList [i] = new blockAddress (5,10,-4); i++;
- addressList [i] = new blockAddress (5,11,-4); i++;
- addressList [i] = new blockAddress (1,3,-5); i++;
- addressList [i] = new blockAddress (1,4,-5); i++;
- addressList [i] = new blockAddress (2,5,-5); i++;
- addressList [i] = new blockAddress (2,6,-5); i++;
- addressList [i] = new blockAddress (3,7,-5); i++;
- addressList [i] = new blockAddress (3,8,-5); i++;
- addressList [i] = new blockAddress (4,9,-5); i++;
- addressList [i] = new blockAddress (5,10,-5); i++;
- addressList [i] = new blockAddress (5,11,-5); i++;
- addressList [i] = new blockAddress (1,3,-6); i++;
- addressList [i] = new blockAddress (1,4,-6); i++;
- addressList [i] = new blockAddress (2,5,-6); i++;
- addressList [i] = new blockAddress (3,6,-6); i++;
- addressList [i] = new blockAddress (3,7,-6); i++;
- addressList [i] = new blockAddress (3,8,-6); i++;
- addressList [i] = new blockAddress (4,9,-6); i++;
- addressList [i] = new blockAddress (5,10,-6); i++;
- addressList [i] = new blockAddress (5,11,-6); i++;
- addressList [i] = new blockAddress (3,3,-7); i++;
- addressList [i] = new blockAddress (3,4,-7); i++;
- addressList [i] = new blockAddress (3,5,-7); i++;
- addressList [i] = new blockAddress (3,6,-7); i++;
- addressList [i] = new blockAddress (3,7,-7); i++;
- addressList [i] = new blockAddress (3,8,-7); i++;
- addressList [i] = new blockAddress (4,9,-7); i++;
- addressList [i] = new blockAddress (5,10,-7); i++;
- addressList [i] = new blockAddress (5,11,-7); i++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment