Skip to content

Commit

Permalink
Adds comments, cleanup and MiniEngineAO license file
Browse files Browse the repository at this point in the history
  • Loading branch information
tomezpl committed Apr 23, 2020
1 parent e61907a commit a6a40da
Show file tree
Hide file tree
Showing 24 changed files with 195 additions and 107 deletions.
1 change: 1 addition & 0 deletions Assets/Code/ClueHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine;
using UnityEngine.UI;

// On-screen help prompts, dynamically adapting to the situation
public class ClueHUD : MonoBehaviour
{
public GameObject FPPTerminalPrompts;
Expand Down
3 changes: 2 additions & 1 deletion Assets/Code/ExecutionStatus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public class ExecutionStatus
// Status returned after handling a node
public class ExecutionStatus
{
public bool success; // Was the execution successful?
public bool handover; // Should the execution be handed over to the derived class?
Expand Down
2 changes: 2 additions & 0 deletions Assets/Code/Levels/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

public class Car : Programmable
{
// Direction to move in on the grid
public int XDir = 0;
public int YDir = 1;

// Starting coordinate and current position on the grid
public Vector2 StartingCoord = new Vector2(2, 1);
public Vector2 CurrentCoord = new Vector2(2, 1);

Expand Down
14 changes: 14 additions & 0 deletions Assets/Code/Levels/CarProgramController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,40 @@

public class CarProgramController : ProgramController
{
// Controlled car/robot
public Car car;

// The grid object
public GameObject grid;

// The cells on the grid
public GridCell[] cells;

// Controlled door
public UnlockableDoorWithLock doorToUnlock;

// Destination cell (which unlocks the door)
GridCell endCell = null;

// Puzzle-specific functions for the user program
protected override Dictionary<string, Delegate> ControllerFunctions()
{
return new Dictionary<string, Delegate> { { "moveForward", new Action(MoveForward) }, { "turnLeft", new Action(TurnLeft) }, { "turnRight", new Action(TurnRight) } };
}

// Rotate clockwise
private void TurnRight()
{
car.TurnRight();
}

// Rotate counter-clockwise
private void TurnLeft()
{
car.TurnLeft();
}

// Move the car in the current direction, if that cell isn't blocked
private void MoveForward()
{
bool reachedEnd = false;
Expand Down Expand Up @@ -72,6 +85,7 @@ protected override void Start()
cells = grid.GetComponentsInChildren<GridCell>();
}

// Handle puzzle-specific function calls if ProgramController.ExecuteNode set the handover flag
public override ExecutionStatus ExecuteNode(NodeBase node)
{
ExecutionStatus baseStatus = base.ExecuteNode(node);
Expand Down
5 changes: 5 additions & 0 deletions Assets/Code/Levels/DoorProgramController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ public class DoorProgramController : ProgramController
// Controlled door
public GameObject doorObject;

// Timer for processing the setLock function node - this is based on the doorOpenDuration of the controlled door
float timer = 0.0f;
bool timerActive = false;

// Lock type used for this door (BasicLock, FizzBuzz)
public UnlockableDoorWithLock.LockType lockType;

// For FizzBuzz puzzle: for how many numbers should the fizzbuzz game be played?
public int fizzBuzzN = 30;

protected virtual void SetLock(string state)
Expand All @@ -28,6 +32,7 @@ protected virtual void SetLock(string state)
timerActive = true;
}

// Check the FizzBuzz output when ProgramEnd is reached
protected override void ProgramEndCallback()
{
base.ProgramEndCallback();
Expand Down
6 changes: 6 additions & 0 deletions Assets/Code/Levels/Fan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

public class Fan : Programmable
{
// Current angular velocity
public double speed = 0.0;

// Is the fan deadly? (ie. does contact cause respawn?)
public bool isDeadly = false;

// Is the fan sentient? (ie. does it spin on its own)
public bool isSentient = false;

// Respawn point used if fan is deadly
public Transform respawnPoint;

// The fan is made out of multiple meshes, but only features one box collider for checking if player hits it.
BoxCollider col;

public float speedMult = 1.0f;
Expand Down
5 changes: 5 additions & 0 deletions Assets/Code/Levels/FanProgramController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

public class FanProgramController : ProgramController
{
// Fan in the scene
public Fan fan;

// Declare puzzle-specific functions for the user program
protected override Dictionary<string, Delegate> ControllerFunctions()
{
return new Dictionary<string, Delegate> { { "speedUp", new Action<string>(SpeedUp) }, { "speedDown", new Action<string>(SpeedDown) } };
}

// Add acceleration to the fan's torque
private void SpeedUp(string acceleration)
{
double accel = 0.0;
Expand All @@ -24,6 +27,7 @@ private void SpeedUp(string acceleration)
fan.speedMult = 1.0f;
}

// Slow down the fan torque
private void SpeedDown(string deceleration)
{
double decel = 0.0;
Expand All @@ -35,6 +39,7 @@ private void SpeedDown(string deceleration)
fan.speed = Mathf.Max(0.0f, (float)(fan.speed - decel));
}

// Handle puzzle-specific function calls if ProgramController.ExecuteNode set the handover flag
public override ExecutionStatus ExecuteNode(NodeBase node)
{
ExecutionStatus baseStatus = base.ExecuteNode(node);
Expand Down
3 changes: 3 additions & 0 deletions Assets/Code/Levels/GridCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

public class GridCell : MonoBehaviour
{
// Is this cell meant to block the Car?
public bool blocked;

// Grid coordinates of the cell
public Vector2 Coords;

// Start is called before the first frame update
Expand Down
30 changes: 26 additions & 4 deletions Assets/Code/Levels/SampleScene/PlatformProgramController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

public class PlatformProgramController : ProgramController
{
// Object in scene that contains Platforms
public GameObject PlatformContainer;

// Amount to raise/lower the platform by
public float stepHeight = 0.25f;

// Original local positions of the platforms (at the time of calling raisePlatform/lowerPlatform, used to track progress in V3.Lerp)
// Original local positions of the platforms (at the time of calling raisePlatform/lowerPlatform, used to track progress in Vector3.Lerp)
public List<Vector3> originalPositions = new List<Vector3>();

// Initial local positions of the platforms (at the time of starting the level, used to cap their elevation)
Expand All @@ -21,6 +23,7 @@ public class PlatformProgramController : ProgramController
// Time taken already to raise/lower current platform (each PlatformProgram can only raise/lower one platform at a tick!)
protected float currentRaiseTime = 0.0f;

// Declare puzzle-specific functions
protected override Dictionary<string, Delegate> ControllerFunctions()
{
return new Dictionary<string, Delegate> { { "raisePlatform", new Action<string>(RaisePlatform) }, { "lowerPlatform", new Action<string>(LowerPlatform) } };
Expand All @@ -29,17 +32,22 @@ protected override Dictionary<string, Delegate> ControllerFunctions()
protected override void Start()
{
base.Start();

// Combine base functions and puzzle functions
CombineControllerFunctions(base.ControllerFunctions());
CombineControllerFunctions(ControllerFunctions());

// Store the initial platform positions as a reference point for interpolation
GetPlatformPositions();

// Set each platform's Computer reference to this terminal
foreach(Platform platform in PlatformContainer.GetComponentsInChildren<Platform>())
{
platform.Computer = gameObject;
}
}

// Store positions of each platform
protected void GetPlatformPositions()
{
Programmable[] platforms = PlatformContainer.GetComponentsInChildren<Programmable>();
Expand All @@ -61,8 +69,11 @@ protected void GetPlatformPositions()
}
}

// Puzzle-specific ExecuteNode implementation. This should only execute if ProgramController.ExecuteNode set the handover flag.
public override ExecutionStatus ExecuteNode(NodeBase node)
{
// Check if node can be handled by ProgramController first.
// If not, it will return .handover as true
ExecutionStatus baseStatus = base.ExecuteNode(node);
if (!baseStatus.handover)
return baseStatus;
Expand All @@ -87,20 +98,19 @@ public override ExecutionStatus ExecuteNode(NodeBase node)
{
Platform platform = GetChildProgrammable(PlatformContainer, int.Parse((string)functionCall.GetRawParameters(symbolTable)[0])).GetComponent<Platform>();

// Check elevation boundaries
// Check elevation boundaries (e.g. to prevent platforms going under ground)
bool allowElevation = functionCall.functionName == "raisePlatform" && initPositions[platform.index].y + platform.MaxElevation < platform.transform.localPosition.y + stepHeight;
bool allowDown = functionCall.functionName == "lowerPlatform" && initPositions[platform.index].y + platform.MinElevation > platform.transform.localPosition.y - stepHeight;
bool allowMove = allowElevation || allowDown;

if (allowMove)
{
// Ignore this call if outside limits
// If outside limits, prevent moving
processingDone = true;
return new ExecutionStatus { success = true, handover = false };
}

// Otherwise continue with raising/lowering the platform

GetPlatformPositions();
currentRaiseTime = 0.0f;
processingDone = false;
Expand All @@ -115,22 +125,30 @@ public override ExecutionStatus ExecuteNode(NodeBase node)
return new ExecutionStatus { success = true, handover = false };
}


public override bool ExecuteFrame()
{
// Check if ProgramController.ExecuteFrame moved to next node already, in which case this child implementation does not need to be called.
if (!base.ExecuteFrame())
return false;

// Is the node still being processed?
if(!processingDone)
{
// Is it a function?
if(CheckNodeType(currentNode) == NodeType.FunctionCallBase)
{
FunctionCallBase functionCall = currentNode.GetComponent<FunctionCallBase>();
// Is it not a base function?
if (ControllerFunctions().ContainsKey(functionCall.functionName))
{
// Platform index
int index = -1;
string val = (string)functionCall.GetRawParameters(symbolTable)[0];
// Get the platform index from command node in the user program
if (int.TryParse(val, out index) || int.TryParse(symbolTable[val].Value, out index))
{
// If the index value is valid, call raisePlatform/lowerPlatform with the platform index as a parameter.
if (index >= 0)
{
//Logger.Log($"Calling {functionCall.functionName}({index})");
Expand All @@ -155,6 +173,7 @@ public override bool ExecuteFrame()
}

// Finally, check if the timer overran raisingTime - if yes, mark processingDone as true.
// Then, the execution can move to next node.
if(currentRaiseTime >= raisingTime)
{
Logger.Log("Platform raised!");
Expand All @@ -165,6 +184,7 @@ public override bool ExecuteFrame()
return true;
}

// Raise the platform marked by index by one step.
protected void RaisePlatform(string index)
{
int nIndex = -1;
Expand All @@ -176,6 +196,8 @@ protected void RaisePlatform(string index)
Programmable platform = GetChildProgrammable(PlatformContainer, nIndex).GetComponent<Programmable>();
platform.transform.localPosition = Vector3.Lerp(platform.transform.localPosition, originalPositions[nIndex] + new Vector3(0.0f, stepHeight, 0.0f), currentRaiseTime / raisingTime);
}

// Lower the platform marked by index by one step.
protected void LowerPlatform(string index)
{
int nIndex = -1;
Expand Down
2 changes: 2 additions & 0 deletions Assets/Code/Levels/UnlockableDoorWithLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ public class UnlockableDoorWithLock : UnlockableDoor
{
public enum LockType { BasicLock = 0, LogicGate, KeyCode, FizzBuzz };

// For FizzBuzz puzzle: for how many numbers should the fizzbuzz game be played?
public int fizzBuzzCount = 20;

// For BasicLock: the door's current lock state (true = locked)
protected bool basicLockState = true;

// Start is called before the first frame update
Expand Down
Loading

0 comments on commit a6a40da

Please sign in to comment.