Created
May 22, 2024 17:24
-
-
Save MalcolmMacDonald/f8bc2b144be509da55be5dfed6bb6619 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #region | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using NUnit.Framework; | |
| using Sokoban; | |
| using Sokoban.Serialization; | |
| using UnityEngine; | |
| #endregion | |
| namespace Tests | |
| { | |
| public class SokobanElementTests : ZenjectUnitTestFixture | |
| { | |
| public SokobanGridService gridService; | |
| [SetUp] | |
| public void CommonInstall() | |
| { | |
| this.Container.BindInterfacesTo<LoggingService>().AsSingle(); | |
| this.Container.Bind<SokobanGridService>().AsSingle(); | |
| this.Container.Bind<GameConfigSO>().AsSingle(); | |
| this.Container.Inject(this); | |
| BindAudioClips(); | |
| gridService = this.Container.Resolve<SokobanGridService>(); | |
| } | |
| void BindAudioClips() | |
| { | |
| PlayableClipSO[] audioClips = Resources.LoadAll<PlayableClipSO>("AudioClips/"); | |
| foreach (var clip in audioClips){ | |
| this.Container.BindInstance(clip).WithId(clip.name); | |
| } | |
| } | |
| protected void ExecuteAction( PlayerController player, PlayerController.PlayerAction action ) | |
| { | |
| player.QueueAction(action); | |
| player.ResolveActions(); | |
| } | |
| protected SokobanElement CreateElement( Vector3Int position, params WalledCell[] cells ) | |
| { | |
| var testElement = CreateElementWithoutAwake(position, cells); | |
| testElement.Awake(); | |
| return testElement; | |
| } | |
| protected SokobanElement CreateElementWithoutAwake( Vector3Int position, params WalledCell[] cells ) | |
| { | |
| var testObject = new GameObject(); | |
| var testElement = testObject.AddComponent<FurnitureController>(); | |
| if(cells.Length==0){ | |
| cells = new[] { new WalledCell(Vector3Int.zero, WallDirection.None) }; | |
| } | |
| testElement.initialRelativeLocations = cells.ToList(); | |
| testObject.transform.position = position; | |
| testObject.name = "TestElement"; | |
| this.Container.Inject(testElement); | |
| return testElement; | |
| } | |
| protected PlayerController CreatePlayer( Vector3Int position ) | |
| { | |
| var testObject = new GameObject(); | |
| var testElement = testObject.AddComponent<PlayerController>(); | |
| testElement.initialRelativeLocations = new List<WalledCell> { Vector3Int.zero, Vector3Int.up }; | |
| testObject.transform.position = position; | |
| testObject.name = "Player"; | |
| this.Container.Inject(testElement); | |
| testElement.Awake(); | |
| return testElement; | |
| } | |
| protected SokobanElement CreateTable( Vector3Int position, Vector3Int upDirection, bool frozen = false ) | |
| { | |
| Dictionary<Vector3Int, WallDirection> directionDict = new Dictionary<Vector3Int, WallDirection> | |
| { | |
| { Vector3Int.forward, WallDirection.Forward }, | |
| { Vector3Int.back, WallDirection.Backward }, | |
| { Vector3Int.left, WallDirection.Left }, | |
| { Vector3Int.right, WallDirection.Right }, | |
| { Vector3Int.up, WallDirection.Up }, | |
| { Vector3Int.down, WallDirection.Down } | |
| }; | |
| var newElement = CreateElementWithoutAwake(position, new WalledCell(Vector3Int.zero, directionDict[upDirection], false, true)); | |
| newElement.containablePieceTypeMask = PieceType.Player; | |
| newElement.name = "Table"; | |
| newElement.frozen = frozen; | |
| newElement.Awake(); | |
| return newElement; | |
| } | |
| protected SokobanElement CreateTunnel( Vector3Int originPosition, params Vector3Int[] localPositions ) | |
| { | |
| List<WalledCell> cellWalls = SokobanUtilities.GetTunnelWalls(localPositions.ToList()); | |
| var testObject = new GameObject(); | |
| var testElement = testObject.AddComponent<FurnitureController>(); | |
| testElement.initialRelativeLocations = cellWalls; | |
| testElement.containablePieceTypeMask = PieceType.Player; | |
| testObject.transform.position = originPosition; | |
| testObject.name = "Tunnel"; | |
| this.Container.Inject(testElement); | |
| testElement.Awake(); | |
| return testElement; | |
| } | |
| protected SokobanElement CreateGoalRegion( Vector3Int position, params Vector3Int[] localPositions ) | |
| { | |
| var testObject = new GameObject(); | |
| var testElement = testObject.AddComponent<GoalRegion>(); | |
| testElement.initialRelativeLocations = new List<WalledCell>(localPositions.Select(x => new WalledCell(x, WallDirection.None, false))); | |
| testElement.frozen = true; | |
| testObject.transform.position = position; | |
| testObject.name = "GoalRegion"; | |
| testElement.containablePieceTypeMask = ~PieceType.None; | |
| this.Container.Inject(testElement); | |
| testElement.Awake(); | |
| return testElement; | |
| } | |
| public DoorController CreateDoor( Vector3Int position, Vector3Int direction ) | |
| { | |
| var doorPrefab = Resources.Load<SokobanElementSO>("Elements/Door").prefab; | |
| var doorObject = Object.Instantiate(doorPrefab); | |
| var testElement = doorObject.GetComponent<DoorController>(); | |
| doorObject.transform.rotation = Quaternion.LookRotation(direction); | |
| doorObject.transform.position = position; | |
| doorObject.name = "Door"; | |
| testElement.containablePieceTypeMask = ~PieceType.None; | |
| this.Container.Inject(testElement); | |
| testElement.Awake(); | |
| return testElement; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment