Skip to content

Commit 6e57e5b

Browse files
committed
Add beam modifier system.
1 parent 584bf56 commit 6e57e5b

6 files changed

Lines changed: 114 additions & 3 deletions

File tree

Assets/Scenes/MattsTestingLab.unity

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,7 @@ MonoBehaviour:
16841684
m_EditorClassIdentifier:
16851685
_renderer: {fileID: 331883125}
16861686
_axisHelper: {fileID: 2037731236}
1687+
_beamModifierData: {fileID: 0}
16871688
_lightBeamLength: 20
16881689
_mode: 1
16891690
--- !u!1 &474129002
@@ -1840,6 +1841,7 @@ MonoBehaviour:
18401841
m_EditorClassIdentifier:
18411842
_renderer: {fileID: 474129004}
18421843
_axisHelper: {fileID: 961317270}
1844+
_beamModifierData: {fileID: 0}
18431845
_lightBeamLength: 20
18441846
_mode: 1
18451847
--- !u!1 &496010798
@@ -1996,6 +1998,7 @@ MonoBehaviour:
19961998
m_EditorClassIdentifier:
19971999
_renderer: {fileID: 496010800}
19982000
_axisHelper: {fileID: 1327103495}
2001+
_beamModifierData: {fileID: 0}
19992002
_lightBeamLength: 20
20002003
_mode: 1
20012004
--- !u!1 &604183311
@@ -5282,6 +5285,7 @@ MonoBehaviour:
52825285
m_EditorClassIdentifier:
52835286
_renderer: {fileID: 1226129313}
52845287
_axisHelper: {fileID: 1087210529}
5288+
_beamModifierData: {fileID: 0}
52855289
_lightBeamLength: 20
52865290
_mode: 1
52875291
--- !u!1 &1327103492
@@ -5430,6 +5434,7 @@ GameObject:
54305434
- component: {fileID: 1597989004}
54315435
- component: {fileID: 1597989003}
54325436
- component: {fileID: 1597989002}
5437+
- component: {fileID: 1597989005}
54335438
m_Layer: 0
54345439
m_Name: DummyControllerSource
54355440
m_TagString: Untagged
@@ -5558,6 +5563,7 @@ MonoBehaviour:
55585563
m_EditorClassIdentifier:
55595564
_renderer: {fileID: 1597989002}
55605565
_axisHelper: {fileID: 0}
5566+
_beamModifierData: {fileID: 1597989005}
55615567
_lightBeamLength: 20
55625568
_mode: 0
55635569
--- !u!4 &1597989004
@@ -5575,6 +5581,20 @@ Transform:
55755581
m_Children: []
55765582
m_Father: {fileID: 0}
55775583
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
5584+
--- !u!114 &1597989005
5585+
MonoBehaviour:
5586+
m_ObjectHideFlags: 0
5587+
m_CorrespondingSourceObject: {fileID: 0}
5588+
m_PrefabInstance: {fileID: 0}
5589+
m_PrefabAsset: {fileID: 0}
5590+
m_GameObject: {fileID: 1597989001}
5591+
m_Enabled: 1
5592+
m_EditorHideFlags: 0
5593+
m_Script: {fileID: 11500000, guid: 44c483be8df52a043b562df9c2fe4e89, type: 3}
5594+
m_Name:
5595+
m_EditorClassIdentifier:
5596+
_colour: {r: 1, g: 0.6136042, b: 0, a: 1}
5597+
_beamForce: 200
55785598
--- !u!1 &1782676293
55795599
GameObject:
55805600
m_ObjectHideFlags: 0
@@ -5714,6 +5734,7 @@ MonoBehaviour:
57145734
m_EditorClassIdentifier:
57155735
_renderer: {fileID: 1782676294}
57165736
_axisHelper: {fileID: 604183313}
5737+
_beamModifierData: {fileID: 0}
57175738
_lightBeamLength: 20
57185739
_mode: 1
57195740
--- !u!4 &1782676296

Assets/Scripts/Controllers/PlayerController.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using Environment;
13
using UnityEngine;
24
using UnityEngine.Events;
35
using UnityEngine.InputSystem;
@@ -158,5 +160,21 @@ private void SwapCharacters()
158160
}
159161
}
160162

163+
public void RegisterIncomingBeamForce(LightBeamController sender, int beamPriority, Vector2 senderBeamDirection, float beamForce)
164+
{
165+
// TODO: This method is called every tick that the beam detects the player. The beam priority is a value that increments the more controllers this single beam of light
166+
// has been through. The senderBeamDirection dictates the direction the beam is flowing. The beamForce value is a raw force to be applied in the given direction, the simplest
167+
// application of this being senderBeamDirection * beamForce, but I didn't want to just "implement that" in this way. I might change the beamForce parameter to a vec2
168+
// as we implement new beam modifiers and discover we have a need for that, but since im just following my nose for now, this is the best I got for ya. - Matt
169+
print("Player registers force!");
170+
}
171+
172+
internal void UnregisterIncomingBeamForce(LightBeamController sender, int beamPriority)
173+
{
174+
// TODO: This method is only called once by the sending beam controller to effectively flag the player is no longer under the control of that particular light beam controller.
175+
// This method exists to help you clean up any state, or help you track multiple controllers if your implementation requires it, and need a way to figure out which controllers to
176+
// stop caring about. - Matt
177+
print("Player unregisters force!");
178+
}
161179
}
162180
}

Assets/Scripts/Environment/Debug.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Environment/LightBeamController.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Controllers;
23
using UnityEngine;
34

45
namespace Environment
@@ -12,18 +13,26 @@ public class LightBeamController : MonoBehaviour
1213
[SerializeField]
1314
private Transform _axisHelper;
1415

16+
[SerializeField]
17+
private LightBeamModifier _beamModifierData;
18+
1519
[Header("Configuration")]
1620
[SerializeField]
1721
private float _lightBeamLength;
1822

1923
[SerializeField]
2024
private LightBeamMode _mode;
25+
2126
private Quaternion _cachedStartRotation;
2227
private LightBeamController _targetHit;
2328
private Vector3? _contactPoint;
2429
private Vector3 _senderDirection;
25-
private RaycastHit2D[] _beamRaycastData = new RaycastHit2D[2];
30+
private RaycastHit2D[] _beamRaycastData = new RaycastHit2D[3];
2631
private ContactFilter2D _beamRaycastFilter;
32+
private int _beamPriority;
33+
private PlayerController _player;
34+
35+
public LightBeamModifier BeamModifierData => _beamModifierData;
2736

2837
private Vector3? RegisterPotentialBeamHit()
2938
{
@@ -37,17 +46,35 @@ public class LightBeamController : MonoBehaviour
3746
_targetHit = null;
3847
}
3948

49+
if (_player != null)
50+
{
51+
_beamModifierData.ClearBeamEffect(this, _beamPriority, _player);
52+
_player = null;
53+
}
54+
4055
return null;
4156
}
4257

4358
RaycastHit2D? hitInfo = null;
59+
60+
bool appliedForceToPlayerThisFrame = false;
4461

4562
foreach (var hit in _beamRaycastData)
4663
{
4764
if (hit.transform != null && hit.transform.GetComponentInChildren<LightBeamController>() != this)
4865
{
49-
hitInfo = hit;
50-
break;
66+
_player = hit.transform.GetComponentInChildren<PlayerController>();
67+
68+
if (_player != null && !appliedForceToPlayerThisFrame)
69+
{
70+
appliedForceToPlayerThisFrame = true;
71+
_beamModifierData.ApplyBeamEffect(this, _beamPriority, _player, transform.right);
72+
}
73+
else
74+
{
75+
hitInfo = hit;
76+
break;
77+
}
5178
}
5279
}
5380

@@ -100,6 +127,8 @@ private void ProduceBeam()
100127
var translatedPosition = transform.position;
101128
translatedPosition += transform.right * _lightBeamLength;
102129
_renderer.SetPositions(new[] { _contactPoint ?? transform.position, potentialHitPoint ?? translatedPosition});
130+
_renderer.startColor = _beamModifierData.Colour;
131+
_renderer.endColor = _beamModifierData.Colour;
103132
}
104133

105134
protected void Start()
@@ -142,6 +171,7 @@ public void RegisterHit(LightBeamController sender, Vector3 hitPoint)
142171
switch (_mode)
143172
{
144173
case LightBeamMode.Bounce:
174+
_beamModifierData = sender.BeamModifierData;
145175
var reflectedDirection = Vector2.Reflect(_senderDirection, _axisHelper.up);
146176
var reflectedAngle = Mathf.Atan2(reflectedDirection.y, reflectedDirection.x) * Mathf.Rad2Deg;
147177
transform.rotation = Quaternion.Euler(0, 0, reflectedAngle);
@@ -167,6 +197,11 @@ public void UnregisterHit()
167197
_targetHit = null;
168198
}
169199

200+
if (_mode == LightBeamMode.Bounce)
201+
{
202+
_beamModifierData = null;
203+
}
204+
170205
transform.rotation = _cachedStartRotation;
171206
_renderer.enabled = false;
172207
_contactPoint = null;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Controllers;
2+
using UnityEngine;
3+
4+
namespace Environment
5+
{
6+
public abstract class LightBeamModifier : MonoBehaviour
7+
{
8+
[Header("Configuration")]
9+
[SerializeField]
10+
private Color _colour;
11+
12+
[SerializeField]
13+
private float _beamForce;
14+
15+
public Color Colour => _colour;
16+
17+
public virtual void ApplyBeamEffect(LightBeamController sender, int beamPriority, PlayerController player, Vector2 senderBeamDirection)
18+
{
19+
player.RegisterIncomingBeamForce(sender, beamPriority, senderBeamDirection, _beamForce);
20+
}
21+
22+
public virtual void ClearBeamEffect(LightBeamController sender, int beamPriority, PlayerController player)
23+
{
24+
player.UnregisterIncomingBeamForce(sender, beamPriority);
25+
}
26+
}
27+
}

Assets/Scripts/Environment/LightBeamModifier.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)