Preserve immutable decoration layouts in stage definitions
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MagnetismHeroes.Core;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace MagnetismHeroes.Tests.EditMode.Core
|
||||
{
|
||||
public sealed class StageLayoutTests
|
||||
{
|
||||
private static readonly Rotation4 Identity = new Rotation4(0, 0, 0, 1);
|
||||
private static readonly Position3 UnitScale = new Position3(1, 1, 1);
|
||||
|
||||
[Test]
|
||||
public void SameBallPositionsWithDifferentDecorationsRemainDistinctAndDoNotChangeTheMath()
|
||||
{
|
||||
var positions = new[] { new Position3(1, 2, 3), new Position3(4, 5, 6) };
|
||||
var garden = new StageDefinition(6, StageKind.Composition, new[] { 1, 1 }, new[] { 2 }, positions,
|
||||
new[] { new DecorationPlacement("garden/arch", new Position3(3, 0, 1), Identity, UnitScale) });
|
||||
var cloud = new StageDefinition(7, StageKind.Composition, new[] { 1, 1 }, new[] { 2 }, positions,
|
||||
new[] { new DecorationPlacement("cloud/platform", new Position3(3, 4, 1), Identity, new Position3(2, 1, 2)) });
|
||||
Assert.That(garden.Layout.Decorations.Count, Is.EqualTo(1));
|
||||
Assert.That(cloud.Layout.Decorations.Count, Is.EqualTo(1));
|
||||
Assert.That(garden.Layout.Decorations[0].AssetKey, Is.EqualTo("garden/arch"));
|
||||
Assert.That(cloud.Layout.Decorations[0].AssetKey, Is.EqualTo("cloud/platform"));
|
||||
Assert.That(garden.Layout.BallPositions, Is.EqualTo(cloud.Layout.BallPositions));
|
||||
foreach (var definition in new[] { garden, cloud })
|
||||
{
|
||||
var board = new BoardRules(definition);
|
||||
Assert.That(board.BallCount, Is.EqualTo(2));
|
||||
Assert.That(board.IsSolved, Is.False);
|
||||
Assert.That(board.TryMerge(board.Groups[0].id, board.Groups[1].id).Accepted, Is.True);
|
||||
Assert.That(board.IsSolved, Is.True);
|
||||
Assert.That(board.BallCount, Is.EqualTo(2));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InputArraysAndReturnedTransformCopiesCannotChangeFixedLayout()
|
||||
{
|
||||
var positions = new[] { new Position3(1, 2, 3) };
|
||||
var decorations = new[] { new DecorationPlacement("workshop/bench", new Position3(4, 5, 6), Identity, UnitScale) };
|
||||
var definition = new StageDefinition(1, StageKind.Counting, new[] { 1 }, new[] { 1 }, positions, decorations);
|
||||
positions[0] = new Position3(100, 0, 0);
|
||||
decorations[0] = new DecorationPlacement("changed", new Position3(), Identity, UnitScale);
|
||||
Assert.That(definition.Layout.Decorations.Count, Is.EqualTo(1));
|
||||
Assert.That(definition.BallPositions[0].x, Is.EqualTo(1));
|
||||
Assert.That(definition.Layout.Decorations[0].AssetKey, Is.EqualTo("workshop/bench"));
|
||||
var positionCopy = definition.Layout.Decorations[0].Position;
|
||||
var rotationCopy = definition.Layout.Decorations[0].Rotation;
|
||||
var scaleCopy = definition.Layout.Decorations[0].Scale;
|
||||
positionCopy.x = 100; rotationCopy.w = 0; scaleCopy.x = 100;
|
||||
Assert.That(definition.Layout.Decorations[0].Position.x, Is.EqualTo(4));
|
||||
Assert.That(definition.Layout.Decorations[0].Rotation.w, Is.EqualTo(1));
|
||||
Assert.That(definition.Layout.Decorations[0].Scale.x, Is.EqualTo(1));
|
||||
Assert.Throws<NotSupportedException>(() => ((IList<DecorationPlacement>)definition.Layout.Decorations)[0] = decorations[0]);
|
||||
Assert.Throws<NotSupportedException>(() => ((IList<Position3>)definition.Layout.BallPositions)[0] = positions[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PuzzleAndFreePlaySourceIdsSelectOriginalFixedDecorationLayoutOnRestore()
|
||||
{
|
||||
var definition = new StageDefinition(6, StageKind.Composition, new[] { 1, 1 }, new[] { 2 },
|
||||
new[] { new Position3(), new Position3(1, 0, 0) },
|
||||
new[] { new DecorationPlacement("garden/slide", new Position3(0, 0, 8), Identity, UnitScale) });
|
||||
var catalog = new[] { definition };
|
||||
var board = new BoardRules(definition);
|
||||
board.TryMoveGroup(board.Groups[0].id, new Position3(5, 0, 0));
|
||||
var puzzle = board.ToPuzzleState(new CameraState());
|
||||
var restoredDefinition = catalog.Single(stage => stage.Id == puzzle.stageId);
|
||||
var restored = new BoardRules(restoredDefinition, puzzle);
|
||||
Assert.That(restoredDefinition.Layout.Decorations.Count, Is.EqualTo(1));
|
||||
Assert.That(restoredDefinition.Layout.Decorations[0].AssetKey, Is.EqualTo("garden/slide"));
|
||||
Assert.That(restored.Balls[0].position.x, Is.EqualTo(5));
|
||||
Assert.That(restoredDefinition.BallPositions[0].x, Is.Zero);
|
||||
var free = restored.ToFreePlayState(new CameraState());
|
||||
var freeDefinition = catalog.Single(stage => stage.Id == free.sourceStageId);
|
||||
Assert.That(freeDefinition.Layout.Decorations[0].Position.z, Is.EqualTo(8));
|
||||
Assert.That(BoardRules.CreateFreePlay(free).BallCount, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidDecorationTransformsAndMissingEntriesAreRejected()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new DecorationPlacement("", new Position3(), Identity, UnitScale));
|
||||
Assert.Throws<ArgumentException>(() => new DecorationPlacement("arch", new Position3(float.NaN, 0, 0), Identity, UnitScale));
|
||||
Assert.Throws<ArgumentException>(() => new DecorationPlacement("arch", new Position3(), new Rotation4(), UnitScale));
|
||||
Assert.Throws<ArgumentException>(() => new DecorationPlacement("arch", new Position3(), Identity, new Position3(0, 1, 1)));
|
||||
Assert.Throws<ArgumentException>(() => new StageDefinition(1, StageKind.Counting, new[] { 1 }, new[] { 1 },
|
||||
decorations: new DecorationPlacement[] { null }));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d13c7f6ddacc35d40a7a177a7749b18c
|
||||
Reference in New Issue
Block a user