Skip to content

Commit a3cb89f

Browse files
committed
Added subclass sandbox pattern
1 parent 3b42d1b commit a3cb89f

File tree

13 files changed

+478
-1
lines changed

13 files changed

+478
-1
lines changed

Assets/Patterns/11. Subclass Sandbox.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Patterns/11. Subclass Sandbox/Superpowers.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Patterns/11. Subclass Sandbox/Superpowers/Scripts.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace SubclassSandbox.Superpowers
6+
{
7+
//Implementation of the Subclass Sandbox pattern from the book Game Programming Patterns
8+
public class GameController : MonoBehaviour
9+
{
10+
private SkyLaunch skyLaunch;
11+
12+
13+
void Start()
14+
{
15+
skyLaunch = new SkyLaunch();
16+
}
17+
18+
19+
void Update()
20+
{
21+
if (Input.GetKey(KeyCode.Space))
22+
{
23+
skyLaunch.Activate();
24+
}
25+
}
26+
}
27+
}

Assets/Patterns/11. Subclass Sandbox/Superpowers/Scripts/GameController.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace SubclassSandbox.Superpowers
6+
{
7+
//Child class that defines a single superpower behavior
8+
public class SkyLaunch : Superpower
9+
{
10+
public override void Activate()
11+
{
12+
PlaySound("Launch sound");
13+
SpawnParticles("Dust");
14+
Move("The sky");
15+
}
16+
}
17+
}

Assets/Patterns/11. Subclass Sandbox/Superpowers/Scripts/SkyLaunch.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace SubclassSandbox.Superpowers
6+
{
7+
//Superpower parent class which defines all superpowers we can combine in the child classes
8+
public abstract class Superpower
9+
{
10+
//This is the sandbox method in which the children combine superpowers
11+
public abstract void Activate();
12+
13+
//Different superpowers
14+
protected void Move(string where)
15+
{
16+
Debug.Log($"Moving towards {where}");
17+
}
18+
19+
protected void PlaySound(string sound)
20+
{
21+
Debug.Log($"Playing sound {sound}");
22+
}
23+
24+
protected void SpawnParticles(string particles)
25+
{
26+
Debug.Log($"Firing {particles}");
27+
}
28+
}
29+
}

Assets/Patterns/11. Subclass Sandbox/Superpowers/Scripts/Superpower.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)