Skip to content

Commit

Permalink
Story #3 Add automatic generation of testScene
Browse files Browse the repository at this point in the history
- Modified prefabs

The generated map is a square with 3 platforms
The player is automaticaly instanciated at a currently hardcoded spawn location
The collisions are handled with BoxCollider2D

An additional saveScene function has been added. It allows to store the
generated scene into a new file. It is then possible to reload the
specific scene into unity.
  • Loading branch information
Feinte75 committed Oct 10, 2015
1 parent 9c5ed7e commit a9d1dcf
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 0 deletions.
Binary file added Assets/Prefab/Environment/Wall.prefab
Binary file not shown.
Binary file removed Assets/Prefab/Floor.prefab
Binary file not shown.
Binary file modified Assets/Prefab/Player.prefab
Binary file not shown.
Binary file added Assets/Prefab/PlayerCamera.prefab
Binary file not shown.
Binary file added Assets/Scene/GeneratedScene.unity
Binary file not shown.
Binary file added Assets/Scene/SceneGenerator.unity
Binary file not shown.
Binary file removed Assets/Scene/SceneTest.unity
Binary file not shown.
87 changes: 87 additions & 0 deletions Assets/Scripts/ProceduralGeneration/TestSceneGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using UnityEngine;
using UnityEditor;
using System.Collections;

public class TestSceneGenerator : MonoBehaviour {

[Tooltip("Put here Player Prefab ! ")]
public Transform player;
[Tooltip("Put here Player Camera Prefab ! ")]
public Transform playerCamera;
[Tooltip("Put here Wall Prefab ! ")]
public Transform wall;

private Transform environment; // Stores the roo object for the environment

// Use this for initialization
void Start () {

/* Temporary hard coded parameters */
int width = 100;
int height = 40;
Vector2 spawnPosition = new Vector2 (10, 2);

/* Create the root environment transform which will contain all the walls */
GameObject obj = new GameObject ("Environment");
environment = obj.GetComponent<Transform> ();

/* Generate the scene */
generateSquare (width, height, Vector2.zero);
generatePlatform (10, new Vector2(10, 20));
generatePlatform (10, new Vector2(25, 10));
generatePlatform (30, new Vector2(50, 25));

/* Instantiate the player */
spawnPlayer (spawnPosition);

/* Save the scene to a file */
//saveGeneratedScene ("./Assets/Scene/GeneratedScene.unity");
}

/**
* Generate a bordered square (not filled) beginning at position
*/
private void generateSquare(int width, int height, Vector2 position) {

for (int i = 0; i <= width; i++) {
for (int j = 0; j <= height; j++) {
if(i == 0 || i == width || j == 0 || j == height) {

Transform wallInstance = (Transform) Instantiate(wall, new Vector3(i + position.x, j + position.y, 0), Quaternion.identity);
wallInstance.SetParent(environment);
}
}
}
}

/**
* Generate Horizontal platform beginning at position
*/
private void generatePlatform(int width, Vector2 position) {

for (int i = 0; i <= width; i++) {
Transform wallInstance = (Transform) Instantiate(wall, new Vector3(i + position.x, position.y, 0), Quaternion.identity);
wallInstance.SetParent(environment);
}
}

/**
* Instantiate the player and camera at given spawn position
*/
private void spawnPlayer(Vector2 spawnPosition) {
Transform playerInstance = (Transform) Instantiate (player, new Vector3 (spawnPosition.x, spawnPosition.y, 0), Quaternion.identity);
Transform playerCameraInstance = (Transform) Instantiate (playerCamera, new Vector3 (spawnPosition.x, spawnPosition.y, playerCamera.position.z), Quaternion.identity);

playerCameraInstance.SetParent (playerInstance);
}

/**
* Save the current scene to a scene file
* Stores all previously instantiated GameObject
*/
private void saveGeneratedScene(string path) {
// path example : "./Assets/Scene/test.unity"
EditorApplication.SaveScene (path);
}
}

Binary file added ProjectSettings/NavMeshAreas.asset
Binary file not shown.
Binary file modified ProjectSettings/QualitySettings.asset
Binary file not shown.

1 comment on commit a9d1dcf

@Feinte75
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2 for the platform part

Please sign in to comment.