Skip to content

Commit

Permalink
Now using collision detection when placing enemies / obstacles / char…
Browse files Browse the repository at this point in the history
…gers.

Now not teleporting directly, but initiating warning procedure.
  • Loading branch information
cguckelsberger committed Nov 28, 2014
1 parent 9e095d3 commit 6a9d981
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 64 deletions.
192 changes: 134 additions & 58 deletions Assets/Scripts/LevelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public class LevelGenerator : MonoBehaviour
public int m_obstaclesCount = 20;
public int m_chargerCount = 5;
public int m_monsterCount = 50;
public float m_minDistanceBetweenObjects = 3;
public Dictionary<int,Dictionary<List<Vector2>,Node>> teleportAreas {get; set;}
public Dictionary<int,List<Vector2>> objectPositions;

private Delaunay.Voronoi v;
private GameGraph graphTele;
Expand Down Expand Up @@ -130,8 +132,9 @@ void Awake ()
teleportAreas.Add (n.id,areas);
}


//Create cell walls from edges
//Create meshes and store the positions of those that might collide in "objectsLocation" (per cell)
objectPositions = new Dictionary<int,List<Vector2>> ();

createWalls ();

createFloor ();
Expand Down Expand Up @@ -525,13 +528,6 @@ private Dictionary<List<Vector2>,Node> getTeleportAreas(Node n, Delaunay.Voronoi
return subdivision;
}

private void spawnPlayer(Node startNode)
{
GameObject newPlayer=Instantiate(prefab_player, new Vector3(startNode.coords.x - m_mapWidth / 2, 0.2f, startNode.coords.y - m_mapHeight / 2), Quaternion.Euler(new Vector3(0.0f, 0.0f, 0.0f))) as GameObject;
newPlayer.GetComponent<PlayerController>().setCurrentCellId(startNode.id);
newPlayer.transform.parent = gameObject_players.transform;
}

private GameObject getRandomEnemy()
{
if (prefab_enemies.Count == 0)
Expand All @@ -542,31 +538,6 @@ private GameObject getRandomEnemy()

return prefab_enemies[(int)UnityEngine.Random.Range(0, prefab_enemies.Count)];
}

private void spawnMonsters(GameGraph graphCells)
{
for (int i=0; i<m_monsterCount; i++) {

int nodeIndex = UnityEngine.Random.Range(0,graphTele.Nodes ().Count-1);
Node node = graphTele.Nodes()[nodeIndex];

//Do not place enemies in the start node
if(node.type==Node.CellType.start){
continue;
}

GameObject enemy = getRandomEnemy();
if (enemy != null)
{
Vector2 position = new Vector2(node.coords.x-m_mapWidth/2f,node.coords.y-m_mapHeight/2f);
position += UnityEngine.Random.insideUnitCircle*node.minRadius*0.8f;
GameObject newEnemy = Instantiate(enemy, new Vector3(position.x, 0.1f, position.y), Quaternion.identity) as GameObject;

newEnemy.GetComponent<EnemyAI_BasicCollider>().CurrentCellId = node.id;
newEnemy.transform.parent=gameObject_enemies.transform;
}
}
}

// Create a number of rectangles that can be traversed horizontally
private List<Rectangle> createBoundaries(){
Expand Down Expand Up @@ -639,52 +610,157 @@ private void createWalls(){

}
}

private void createAlarmLights(){
alarmLights = new List<AlarmLight> ();
foreach (Node n in graphTele.Nodes ()) {
GameObject warnLight = Instantiate (prefab_warnLight, new Vector3 (n.coords.x - m_mapWidth / 2, 0.5f, n.coords.y - m_mapHeight / 2), Quaternion.identity) as GameObject;
AlarmLight alarmLight = warnLight.GetComponent<AlarmLight> ();
alarmLight.transform.parent = gameObject_alarmLights.transform;
alarmLights.Add(alarmLight);
}
}

// Create teleporter from prefab on specified line
private void createTeleporter(Vector2 p0, Vector2 p1){

Vector2 midpoint = (p0+p1)*0.5f;
float length = Vector2.Distance(p0,p1)*0.5f;
float angle = -Mathf.Atan2(p0.y - p1.y, p0.x - p1.x) * (180 / Mathf.PI) + 90;
GameObject teleporter = Instantiate(prefab_teleporter,new Vector3(midpoint.x-m_mapWidth/2,0.5f*m_wallHeight,midpoint.y-m_mapHeight/2),Quaternion.Euler(new Vector3(0.0f,angle,0.0f))) as GameObject;
teleporter.transform.localScale = new Vector3(0.5f,0.8f*m_wallHeight,length);
teleporter.transform.parent = gameObject_teleporters.transform;

}

//TODO: Merge create Obstacles / Chargers into one method
private void createObstacles(){
for (int i=0; i<m_obstaclesCount; i++) {
int nodeIndex = UnityEngine.Random.Range(0,graphTele.Nodes ().Count-1);
Node n = graphTele.Nodes()[nodeIndex];
//Do not place obstacles in the start node
if(n.type==Node.CellType.start){
i--;
continue;
}

Vector2 position = new Vector2(n.coords.x-m_mapWidth/2f,n.coords.y-m_mapHeight/2f);
position += UnityEngine.Random.insideUnitCircle*n.minRadius*0.8f;
GameObject obstacle = Instantiate(prefab_obstacle) as GameObject;
obstacle.transform.position=new Vector3(position.x,0.8f,position.y);
obstacle.transform.parent = gameObject.transform;

if(!objectPositions.ContainsKey(n.id)){
List<Vector2> positions = new List<Vector2>();
positions.Add (position);
objectPositions.Add(n.id,positions);
}else{
//Check if collides
bool collides = false;
foreach(Vector2 obj in objectPositions[n.id]){
if(Vector2.Distance(obj, position)<m_minDistanceBetweenObjects){
collides = true;
break;
}
}
if(!collides){
objectPositions[n.id].Add (position);
GameObject obstacle = Instantiate(prefab_obstacle) as GameObject;
obstacle.transform.position=new Vector3(position.x,0.8f,position.y);
obstacle.transform.parent = gameObject.transform;
}else{
i--;
}

}
}
}

private void createChargers(){
for (int i=0; i<m_chargerCount; i++) {
int nodeIndex = UnityEngine.Random.Range(0,graphTele.Nodes ().Count-1);
Node n = graphTele.Nodes()[nodeIndex];
//Do not place chargers in the start node
if(n.type==Node.CellType.start){
i--;
continue;
}

Vector2 position = new Vector2(n.coords.x-m_mapWidth/2f,n.coords.y-m_mapHeight/2f);
position += UnityEngine.Random.insideUnitCircle*n.minRadius*0.8f;
GameObject charger = Instantiate(prefab_charger) as GameObject;
charger.transform.position=new Vector3(position.x,0.5f,position.y);
charger.transform.parent = gameObject_chargers.transform;

if(!objectPositions.ContainsKey(n.id)){
List<Vector2> positions = new List<Vector2>();
positions.Add (position);
objectPositions.Add(n.id, positions);
}else{
//Check if collides
bool collides = false;
foreach(Vector2 obj in objectPositions[n.id]){
if(Vector2.Distance(obj, position)<m_minDistanceBetweenObjects){
collides = true;
break;
}
}
if(!collides){
objectPositions[n.id].Add (position);
GameObject charger = Instantiate(prefab_charger) as GameObject;
charger.transform.position=new Vector3(position.x,0.5f,position.y);
charger.transform.parent = gameObject_chargers.transform;
}else{
i--;
}
}
}
}

private void createAlarmLights(){
alarmLights = new List<AlarmLight> ();
foreach (Node n in graphTele.Nodes ()) {
GameObject warnLight = Instantiate (prefab_warnLight, new Vector3 (n.coords.x - m_mapWidth / 2, 0.5f, n.coords.y - m_mapHeight / 2), Quaternion.identity) as GameObject;
AlarmLight alarmLight = warnLight.GetComponent<AlarmLight> ();
alarmLight.transform.parent = gameObject_alarmLights.transform;
alarmLights.Add(alarmLight);
}
private void spawnPlayer(Node startNode)
{
GameObject newPlayer=Instantiate(prefab_player, new Vector3(startNode.coords.x - m_mapWidth / 2, 0.2f, startNode.coords.y - m_mapHeight / 2), Quaternion.Euler(new Vector3(0.0f, 0.0f, 0.0f))) as GameObject;
newPlayer.GetComponent<PlayerController>().setCurrentCellId(startNode.id);
newPlayer.transform.parent = gameObject_players.transform;
}

private void spawnMonsters(GameGraph graphCells)
{
for (int i=0; i<m_monsterCount; i++) {

int nodeIndex = UnityEngine.Random.Range(0,graphTele.Nodes ().Count-1);
Node node = graphTele.Nodes()[nodeIndex];

//Do not place enemies in the start node
if(node.type==Node.CellType.start){
i--;
continue;
}

GameObject enemy = getRandomEnemy();
if (enemy != null)
{
Vector2 position = new Vector2(node.coords.x-m_mapWidth/2f,node.coords.y-m_mapHeight/2f);
position += UnityEngine.Random.insideUnitCircle*node.minRadius*0.8f;

// Create teleporter from prefab on specified line
private void createTeleporter(Vector2 p0, Vector2 p1){

Vector2 midpoint = (p0+p1)*0.5f;
float length = Vector2.Distance(p0,p1)*0.5f;
float angle = -Mathf.Atan2(p0.y - p1.y, p0.x - p1.x) * (180 / Mathf.PI) + 90;
GameObject teleporter = Instantiate(prefab_teleporter,new Vector3(midpoint.x-m_mapWidth/2,0.5f*m_wallHeight,midpoint.y-m_mapHeight/2),Quaternion.Euler(new Vector3(0.0f,angle,0.0f))) as GameObject;
teleporter.transform.localScale = new Vector3(0.5f,0.8f*m_wallHeight,length);
teleporter.transform.parent = gameObject_teleporters.transform;

if(!objectPositions.ContainsKey(node.id)){
List<Vector2> positions = new List<Vector2>();
positions.Add (position);
objectPositions.Add(node.id, positions);
}else{
//Check if collides
bool collides = false;
foreach(Vector2 obj in objectPositions[node.id]){
if(Vector2.Distance(obj, position)<m_minDistanceBetweenObjects){
collides = true;
break;
}
}
if(!collides){
objectPositions[node.id].Add (position);
GameObject newEnemy = Instantiate(enemy, new Vector3(position.x, 0.1f, position.y), Quaternion.identity) as GameObject;
newEnemy.GetComponent<EnemyAI_BasicCollider>().CurrentCellId = node.id;
newEnemy.transform.parent=gameObject_enemies.transform;
}else{
i--;
}
}
}
}
}

//Create a custom mesh representing a triangle from the 3 given points, located at their midpoint and assigned the given (semi-transparent) color
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void performActions() {
}
if (Input.GetButtonDown ("Teleport") && !enemiesInSameCell()) {
//If no enemies in cell, allow to teleport
teleportController.teleport();
teleportController.initializeTeleporting();
}

// DEBUG BINDINGS BELOW THIS POINT
Expand Down
18 changes: 13 additions & 5 deletions Assets/Scripts/TeleportCountdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class TeleportCountdown : MonoBehaviour {
public float timerLength = 15;
private float lastReset;
private bool alarmsActive = false;
private float alarmPeriod = 5;
private float alarmPeriod = 3;
private List<AlarmLight> alarmLights;
private float dimRamp = 1.5f;
private float dimPeriod = 5.75f;
Expand Down Expand Up @@ -40,6 +40,7 @@ void Start () {

void Update () {
float remaining = timeLeft();

if (remaining == 0) {
deactivateAlarms();
raiseLights();
Expand Down Expand Up @@ -92,13 +93,20 @@ public float timeLeft() {
return Mathf.Max(lastReset + timerLength - Time.time, 0f);
}


public void teleport(){

//Called when a player wants to teleport deliberately
public void initializeTeleporting(){
//Do not allow players to teleport deliberately when alarms are already active
if (alarmsActive) {
return;
}
this.lastReset = Time.time - (timerLength - alarmPeriod);
}
/*
public void removeTeleporter(){
this.levelGenerator.getTeleportGraph().
}*/

public void teleport(){

List<PlayerController> players = new List<PlayerController> ();
List<EnemyAI_BasicCollider> enemies = new List<EnemyAI_BasicCollider> ();
Expand Down Expand Up @@ -144,7 +152,7 @@ public void teleport(){
foreach (PlayerController player in newPlayerNode.Keys) {
Node newNode = newPlayerNode[player];
Vector2 newPosition = new Vector2(newNode.coords.x-levelGenerator.m_mapWidth/2f,newNode.coords.y-levelGenerator.m_mapHeight/2f);
newPosition += Random.insideUnitCircle*newNode.minRadius;
newPosition += Random.insideUnitCircle*newNode.minRadius*0.8f;
player.transform.position = new Vector3(newPosition.x,0.0f,newPosition.y);
player.setCurrentCellId(newNode.id);
}
Expand Down

0 comments on commit 6a9d981

Please sign in to comment.