Skip to content

Commit

Permalink
Improved laser beam rendering
Browse files Browse the repository at this point in the history
Fixed NullReferenceException when binding levers and gates
Fixed a bug which tries to access patients after destroying them
  • Loading branch information
chrisxue815 committed Mar 4, 2014
1 parent bdf8a21 commit b528efa
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 98 deletions.
31 changes: 6 additions & 25 deletions Assets/Scripts/Entities/LaserEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
using System.Collections.Generic;
using Assets.Scripts.Utilities;
using UnityEngine;

[RequireComponent(typeof(LineRenderer))]

public class LaserEmitter : Entity
{
public Direction Direction { get; set; }

RaycastHit _hit;
LineRenderer _line;
private LineStripRenderer lineStrip;

// Use this for initialization
protected override void Start()
{
base.Start();

_line = GetComponent<LineRenderer>();
_line.enabled = true;

//TODO: parameterize
Direction = Direction.Down;

lineStrip = new LineStripRenderer(this);
}

// Update is called once per frame
Expand All @@ -29,8 +23,6 @@ void Update()
var direction = Direction;
var directionVector = direction.ToVector2();
var origin = transform.position.xy();

// drawing
var points = new List<Vector2>();
points.Add(origin);

Expand All @@ -41,14 +33,14 @@ void Update()
DebugUtils.Assert(hit.collider != null);
if (hit.collider == null) break; // for robustness

points.Add(hit.point);
origin = hit.collider.transform.position;
points.Add(origin);

var mirror = hit.collider.GetComponent<Mirror>();
if (mirror != null)
{
direction = mirror.Reflect(direction);
directionVector = direction.ToVector2();
origin = hit.point;
continue;
}

Expand All @@ -58,20 +50,9 @@ void Update()
continue;
}

if (hit.collider.tag == "Pushable")
{
break;
}

break;
}

// drawing
_line.SetVertexCount(points.Count);

for (var i = 0; i < points.Count; i++)
{
_line.SetPosition(i, points[i]);
}
lineStrip.Draw(points);
}
}
10 changes: 4 additions & 6 deletions Assets/Scripts/Entities/Pushables/Mirror.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
using System;
using UnityEngine;
using System.Collections;

public class Mirror : Pushable
{
/// <summary>
/// Indicates the direction of the mirror. If true, laser coming from top is reflected to right.
/// </summary>
public bool Forward { get; set; }
public bool Forward;

public Mirror()
{
MovingWithPlayer = true;
Sfx = "Push Crate";

//TODO: parameterize
Forward = true;
}

// Use this for initialization
protected override void Start()
{
base.Start();

//TODO: parameterize
Forward = true;
}

// Update is called once per frame
Expand Down
4 changes: 1 addition & 3 deletions Assets/Scripts/Entities/Pushables/MirrorInverse.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
public class MirrorInverse : Mirror
{
protected override void Start()
public MirrorInverse()
{
base.Start();

Forward = false;
}
}
38 changes: 26 additions & 12 deletions Assets/Scripts/GameWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class GameWorld : MonoBehaviour
{ 'w', ' ', ' ', ' ', 't', ' ', ' ', ' ', 'b', ' ', ' ', ' ', ' ', ' ', ' ', 'w' },// -1.5
{ 'w', ' ', 'm', ' ', ' ', 'i', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w' },// -2.5
{ 'w', ' ', 'P', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w' },// -3.5
{ 'w', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 's', ' ', 'w' },// -4.5
{ 'w', 'p', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 's', ' ', 'w' },// -4.5
{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' } // -5.5
//-7.5 -6.5 -5.5 -4.5 -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5
},
Expand All @@ -40,7 +40,7 @@ public class GameWorld : MonoBehaviour
{ 'w', ' ', 't', 'P', 't', ' ', ' ', 't', ' ', 'w', ' ', 'b', ' ', ' ', 's', 'w' },
{ 'w', ' ', ' ', 't', ' ', ' ', ' ', ' ', ' ', 'w', 's', ' ', ' ', ' ', 'b', 'w' },
{ 'w', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w', 's', ' ', ' ', ' ', 'b', 'w' },
{ 'w', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w', ' ', 'b', ' ', ' ', 's', 'w' },
{ 'w', 'p', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w', ' ', 'b', ' ', ' ', 's', 'w' },
{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
},
{
Expand All @@ -54,7 +54,7 @@ public class GameWorld : MonoBehaviour
{ 'w', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w', ' ', ' ', ' ', ' ', ' ', 'w' },
{ 'w', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w', ' ', ' ', ' ', ' ', 'b', 'w' },
{ 'w', ' ', 'P', ' ', ' ', ' ', ' ', ' ', ' ', 'w', ' ', ' ', ' ', ' ', ' ', 'w' },
{ 'w', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w', 'f', ' ', ' ', 's', ' ', 'w' },
{ 'w', 'p', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'w', 'f', ' ', ' ', 's', ' ', 'w' },
{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
},
{
Expand Down Expand Up @@ -160,7 +160,7 @@ void Update()

void GameMenu()
{
level = 3;
level = 0;
}

void LevelStart()
Expand Down Expand Up @@ -262,20 +262,34 @@ void LevelStart()
}
}

// TO BE CHANGED
Lever lever = GameObject.Find("Lever(Clone)").GetComponent<Lever>();
if (lever != null)
{
Gate gate = GameObject.Find("Gate(Clone)").GetComponent<Gate>();
lever.gate = gate;
}
//
BindLeverGate();
}

void LevelOver()
{
// Clear resources
if (EntityContainer != null)
Destroy(EntityContainer);

Walls.Clear();
Collectibles.Clear();
Pushables.Clear();
Accessibles.Clear();
Switchables.Clear();
}

void BindLeverGate()
{
// TO BE CHANGED
var leverObj = GameObject.Find("Lever(Clone)");
if (leverObj == null) return;
var lever = leverObj.GetComponent<Lever>();

var gateObj = GameObject.Find("Gate(Clone)");
if (gateObj == null) return;
var gate = gateObj.GetComponent<Gate>();

lever.gate = gate;
//
}
}
14 changes: 5 additions & 9 deletions Assets/Scripts/Utilities/DebugUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

using System;
using System.Diagnostics;
using Debug = UnityEngine.Debug;

namespace Assets.Scripts.Utilities
public class DebugUtils
{
public class DebugUtils
[Conditional("DEBUG")]
public static void Assert(bool condition)
{
[Conditional("DEBUG")]
public static void Assert(bool condition)
if (!condition)
{
if (!condition)
{
throw new Exception();
}
throw new Exception();
}
}
}
42 changes: 42 additions & 0 deletions Assets/Scripts/Utilities/LineStripRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using UnityEngine;

public class LineStripRenderer : Component
{
private readonly List<LineRenderer> lines;
private readonly Component parent;

public LineStripRenderer(Component parent)
{
lines = new List<LineRenderer>();
this.parent = parent;
}

public void Draw(List<Vector2> points)
{
var newLinesCount = points.Count - 1;

for (var i = newLinesCount; i < lines.Count; i++)
{
lines[i].enabled = false;
}

for (var i = lines.Count; i < newLinesCount; i++)
{
var laser = new GameObject("Laser");
laser.transform.parent = parent.transform;

var line = laser.AddComponent<LineRenderer>();
line.SetWidth(0.1f, 0.1f);
line.SetVertexCount(2);

lines.Add(line);
}

for (var i = 0; i < newLinesCount; i++)
{
lines[i].SetPosition(0, points[i]);
lines[i].SetPosition(1, points[i + 1]);
}
}
}
8 changes: 8 additions & 0 deletions Assets/Scripts/Utilities/LineStripRenderer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 40 additions & 43 deletions Assets/Scripts/Utilities/Physics2DExt.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
using UnityEngine;

namespace Assets.Scripts.Utilities
public static class Physics2DExt
{
public static class Physics2DExt
/// <summary>
///
/// </summary>
/// <param name="origin"></param>
/// <param name="direction"></param>
/// <param name="distance"></param>
/// <param name="includingOrigin">If true, include the origin when detecting collision</param>
/// <returns></returns>
public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance, bool includingOrigin)
{
/// <summary>
///
/// </summary>
/// <param name="origin"></param>
/// <param name="direction"></param>
/// <param name="distance"></param>
/// <param name="includingOrigin">If true, include the origin when detecting collision</param>
/// <returns></returns>
public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance, bool includingOrigin)
{
var currentDistance = includingOrigin ? 0 : 1;

for (; currentDistance <= distance; currentDistance++)
{
var newOrigin = origin + currentDistance * direction;
var hit = Physics2D.Raycast(newOrigin, direction, 0);
var currentDistance = includingOrigin ? 0 : 1;

if (hit.collider != null) return hit;
}
for (; currentDistance <= distance; currentDistance++)
{
var newOrigin = origin + currentDistance * direction;
var hit = Physics2D.Raycast(newOrigin, direction, 0);

return new RaycastHit2D();
if (hit.collider != null) return hit;
}

/// <summary>
/// Include the origin when detecting collision
/// </summary>
/// <param name="origin"></param>
/// <param name="direction"></param>
/// <param name="distance"></param>
/// <returns></returns>
public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance)
{
return Raycast(origin, direction, distance, true);
}
return new RaycastHit2D();
}

/// <summary>
/// Exclude the origin when detecting collision
/// </summary>
/// <param name="origin"></param>
/// <param name="direction"></param>
/// <param name="distance"></param>
/// <returns></returns>
public static RaycastHit2D RaycastExclusive(Vector2 origin, Vector2 direction, float distance)
{
return Raycast(origin, direction, distance, false);
}
/// <summary>
/// Include the origin when detecting collision
/// </summary>
/// <param name="origin"></param>
/// <param name="direction"></param>
/// <param name="distance"></param>
/// <returns></returns>
public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance)
{
return Raycast(origin, direction, distance, true);
}

/// <summary>
/// Exclude the origin when detecting collision
/// </summary>
/// <param name="origin"></param>
/// <param name="direction"></param>
/// <param name="distance"></param>
/// <returns></returns>
public static RaycastHit2D RaycastExclusive(Vector2 origin, Vector2 direction, float distance)
{
return Raycast(origin, direction, distance, false);
}
}

0 comments on commit b528efa

Please sign in to comment.