Last active
November 24, 2017 21:34
-
-
Save mattmccray/d3bc48eca5623c7128515439a6bb51c7 to your computer and use it in GitHub Desktop.
Game Event definitions for Drumpf Flinger 9000 (a Unity 3D game)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/* | |
You could name this class GameEvents or whatever you want. I chose Announcments because | |
it doesn't conflict with any builtin Unity class names. I can just type 'ann' and my IDE's | |
auto-complete takes care of the rest. | |
*/ | |
public static class Announcements | |
{ | |
public static class Camera | |
{ | |
public static void ToLaunch() { if (OnToLaunch != null) OnToLaunch.Invoke(); } | |
public static event Action OnToLaunch; | |
public static void AtLaunch() { if (OnAtLaunch != null) OnAtLaunch.Invoke(); } | |
public static event Action OnAtLaunch; | |
public static void ToSpawn() { if (OnToSpawn != null) OnToSpawn.Invoke(); } | |
public static event Action OnToSpawn; | |
public static void AtSpawn() { if (OnAtSpawn != null) OnAtSpawn.Invoke(); } | |
public static event Action OnAtSpawn; | |
} | |
public static class Game | |
{ | |
public static void Start() { if (OnStart != null) OnStart.Invoke(); } | |
public static event Action OnStart; | |
public static void Over(WallSegment brokenWall) { if (OnOver != null) OnOver.Invoke(brokenWall); } | |
public static event Action<WallSegment> OnOver; | |
} | |
public static class Projectile | |
{ | |
public static void Spawn(Projectile newProjectile) { if (OnSpawn != null) OnSpawn.Invoke(newProjectile); } | |
public static event Action<Projectile> OnSpawn; | |
public static void Launch(LaunchDirection direction, Transform lookAtTarget) { if (OnLaunch != null) OnLaunch.Invoke(direction, lookAtTarget); } | |
public static event Action<LaunchDirection, Transform> OnLaunch; | |
public static void ImpactWall(Vector3 position, Vector3 normal) { if (OnImpactWall != null) OnImpactWall.Invoke(position, normal, 0f); } | |
public static void ImpactWall(Vector3 position, Vector3 normal, float velocity) { if (OnImpactWall != null) OnImpactWall.Invoke(position, normal, velocity); } | |
public static event Action<Vector3, Vector3, float> OnImpactWall; | |
public static void ImpactGround(Vector3 position, Vector3 normal) { if (OnImpactGround != null) OnImpactGround.Invoke(position, normal, 0f); } | |
public static void ImpactGround(Vector3 position, Vector3 normal, float velocity) { if (OnImpactGround != null) OnImpactGround.Invoke(position, normal, velocity); } | |
public static event Action<Vector3, Vector3, float> OnImpactGround; | |
public static void Dead(Projectile oldProjectile) { if (OnDead != null) OnDead.Invoke(oldProjectile); } | |
public static event Action<Projectile> OnDead; | |
} | |
public static class UI | |
{ | |
public static void DragStart(Vector2 position) { if (OnDragStart != null) OnDragStart.Invoke(position); } | |
public static event Action<Vector2> OnDragStart; | |
public static void Drag(Vector2 position, Vector2 direction) { if (OnDrag != null) OnDrag.Invoke(position, direction); } | |
public static event Action<Vector2, Vector2> OnDrag; | |
public static void DragRelease(Vector2 position, Vector2 direction) { if (OnDragRelease != null) OnDragRelease.Invoke(position, direction); } | |
public static event Action<Vector2, Vector2> OnDragRelease; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class GameCamera : MonoBehaviour | |
{ | |
private void OnEnable() | |
{ | |
// Add handlers for game events... | |
Announcements.Projectile.OnSpawn += Projectile_OnSpawn; | |
Announcements.Projectile.OnLaunch += Projectile_OnLaunch; | |
Announcements.Projectile.OnImpactWall += Projectile_OnImpact; | |
Announcements.Projectile.OnImpactGround += Projectile_OnImpact; | |
Announcements.Projectile.OnDead += Projectile_OnDead; | |
Announcements.Game.OnStart += Game_OnStart; | |
} | |
private void OnDisable() | |
{ | |
// Clean up handlers for game events... | |
Announcements.Projectile.OnSpawn -= Projectile_OnSpawn; | |
Announcements.Projectile.OnLaunch -= Projectile_OnLaunch; | |
Announcements.Projectile.OnImpactWall -= Projectile_OnImpact; | |
Announcements.Projectile.OnImpactGround -= Projectile_OnImpact; | |
Announcements.Projectile.OnDead -= Projectile_OnDead; | |
Announcements.Game.OnStart -= Game_OnStart; | |
} | |
// All your handling code down here... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment