Skip to content

Commit

Permalink
Merge pull request #106 from benpollarduk/visual-frames
Browse files Browse the repository at this point in the history
Added visual frames
  • Loading branch information
benpollarduk authored Nov 26, 2024
2 parents 59e91d0 + 2bc724a commit f5d1885
Show file tree
Hide file tree
Showing 88 changed files with 1,891 additions and 497 deletions.
1 change: 1 addition & 0 deletions .nuget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ NetAF provides frames for rendering the various game screens. These are fully ex
* Game over frame.
* Transition frame.
* Conversation frame.
* Visual frame.

### Maps
Maps are automatically generated for regions and rooms, and can be viewed with the **map** command:
Expand Down
14 changes: 13 additions & 1 deletion NetAF.Examples/Assets/Regions/Everglades/Rooms/ForestEntrance.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using NetAF.Assets.Locations;
using NetAF.Commands;
using NetAF.Examples.Assets.Regions.Everglades.Visuals;
using NetAF.Logic.Modes;
using NetAF.Utilities;

namespace NetAF.Examples.Assets.Regions.Everglades.Rooms
Expand All @@ -20,7 +23,16 @@ internal class ForestEntrance : IAssetTemplate<Room>
/// <returns>The asset.</returns>
public Room Instantiate()
{
return new(Name, Description, [new Exit(Direction.North)]);
return new(Name, Description, [new Exit(Direction.North)], commands:
[
new(new("Look", "Look around the area."), true, true, (g, a) =>
{
var frame = new ForestEntranceVisualFrame(Name, g.Configuration.DisplaySize).Instantiate();
g.ChangeMode(new VisualMode(frame));
return new(ReactionResult.GameModeChanged, string.Empty);
})
]);

}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using NetAF.Assets;
using NetAF.Extensions;
using NetAF.Rendering;
using NetAF.Rendering.Console;
using NetAF.Rendering.Console.FrameBuilders;
using NetAF.Utilities;

namespace NetAF.Examples.Assets.Regions.Everglades.Visuals
{
internal class ForestEntranceVisualFrame(string name, Size size) : IAssetTemplate<IFrame>
{
#region StaticProperties

public static readonly AnsiColor Sky = new(20, 20, 125);
public static readonly AnsiColor Trunk = new(127, 50, 50);
public static readonly AnsiColor DarkTrunk = new(120, 40, 40);
public static readonly AnsiColor Canopy = new(50, 200, 50);
public static readonly AnsiColor DarkCanopy = new(30, 150, 20);
public static readonly AnsiColor Grass = new(20, 220, 50);
public static readonly AnsiColor GrassHighlights = new(0, 235, 0);
public static readonly AnsiColor Path = new(130, 20, 20);
public static readonly AnsiColor PathHighlights = new(130, 130, 130);

#endregion

#region StaticMethods

private static void DrawSun(GridVisualBuilder builder)
{
builder.SetCell(8, 5, AnsiColor.BrightYellow);
builder.SetCell(4, 7, AnsiColor.BrightYellow);
builder.SetCell(5, 7, AnsiColor.BrightYellow);
builder.SetCell(11, 7, AnsiColor.BrightYellow);
builder.SetCell(12, 7, AnsiColor.BrightYellow);
builder.SetCell(8, 9, AnsiColor.BrightYellow);
builder.DrawRectangle(6, 6, 5, 3, AnsiColor.BrightYellow, AnsiColor.BrightYellow);
builder.DrawTexture(6, 6, 5, 3, ":".ToTexture(), AnsiColor.Yellow);
}

private static void DrawGrass(GridVisualBuilder builder)
{
builder.DrawRectangle(0, 25, 80, 25, Grass, Grass);
builder.DrawTextureOverBackgroundColor(0, 25, 80, 25, Grass, "^ v . ' :\n # ~ ^ . ~ '".ToTexture(), GrassHighlights);
}

private static void DrawTree1(GridVisualBuilder builder, int x, int y)
{
builder.SetCell(x + 5, y, Canopy);
builder.DrawRectangle(x + 4, y + 1, 3, 1, Canopy, Canopy);
builder.DrawRectangle(x + 3, y + 2, 5, 1, Canopy, Canopy);
builder.DrawRectangle(x + 2, y + 3, 7, 1, Canopy, Canopy);
builder.DrawRectangle(x + 1, y + 4, 9, 1, Canopy, Canopy);
builder.DrawRectangle(x, y + 5, 11, 1, Canopy, Canopy);
builder.DrawRectangle(x + 5, y + 5, 1, 6, Trunk, Trunk);
}

private static void DrawTree2(GridVisualBuilder builder, int x, int y)
{
builder.SetCell(x + 5, y, DarkCanopy);
builder.DrawRectangle(x + 3, y + 1, 5, 1, DarkCanopy, DarkCanopy);
builder.DrawRectangle(x + 3, y + 2, 6, 1, DarkCanopy, DarkCanopy);
builder.DrawRectangle(x + 2, y + 3, 7, 1, DarkCanopy, DarkCanopy);
builder.DrawRectangle(x + 1, y + 4, 9, 1, DarkCanopy, DarkCanopy);
builder.DrawRectangle(x, y + 5, 11, 1, DarkCanopy, DarkCanopy);
builder.DrawRectangle(x + 5, y + 5, 1, 6, DarkTrunk, DarkTrunk);
}

private static void DrawPath(GridVisualBuilder builder)
{
builder.DrawRectangle(35, 25, 10, 25, Path, Path);
builder.DrawRectangle(33, 28, 2, 10, Path, Path);
builder.DrawRectangle(32, 31, 3, 10, Path, Path);
builder.DrawRectangle(43, 29, 4, 11, Path, Path);
builder.DrawTextureOverBackgroundColor(30, 25, 20, 25, Path, "@ , } ., @~.+\n .% :; @\n+- { $ '#".ToTexture(), PathHighlights);
}

#endregion

#region Implementation of IAssetTemplate<IFrame>

/// <summary>
/// Instantiate a new instance of the asset.
/// </summary>
/// <returns>The asset.</returns>
public IFrame Instantiate()
{
var builder = new GridVisualBuilder(Sky, AnsiColor.BrightWhite);
builder.Resize(new(size.Width - 4, size.Height - 10));
DrawSun(builder);
DrawGrass(builder);
DrawPath(builder);
DrawTree1(builder, 5, 15);
DrawTree2(builder, 20, 15);
DrawTree1(builder, 35, 15);
DrawTree1(builder, 50, 15);
DrawTree2(builder, 65, 15);
DrawTree1(builder, 2, 16);
DrawTree2(builder, 12, 18);
DrawTree1(builder, 28, 17);
DrawTree1(builder, 39, 16);
DrawTree2(builder, 56, 18);
DrawTree1(builder, 70, 17);

var frameBuilder = new ConsoleVisualFrameBuilder(new GridStringBuilder());
return frameBuilder.Build(name, string.Empty, builder, size);
}

#endregion
}
}
3 changes: 2 additions & 1 deletion NetAF.Examples/Assets/Regions/Hub/Rooms/Clearing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public Room Instantiate()
var room = new Room(Name, Description);

var conversation = new Conversation(
[
new("Squarrrkkk!"),
new("Would you like to change modes?", "ModeQuestion")
{
Expand All @@ -43,7 +44,7 @@ public Room Instantiate()
new("Eeek, simple be fine too! Shame it's been deleted. Maybe it will be implemented again one day! Eeek!", new ToName("ModeQuestion")),
new("Squarrk! Legacy, looks old. Shame it's been deleted. Maybe it will be implemented again one day! Arrk!", new ToName("ModeQuestion")),
new("Fine, suit yourself! Squarrk!", new ToName("ModeQuestion"))
);
]);

room.AddCharacter(new NonPlayableCharacter(new Identifier("Parrot"), new Description("A brightly colored parrot."), conversation: conversation));

Expand Down
1 change: 1 addition & 0 deletions NetAF.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using NetAF.Extensions;
using NetAF.Logic;
using NetAF.Logic.Configuration;
using NetAF.Rendering.Console;

namespace NetAF.Examples
{
Expand Down
10 changes: 10 additions & 0 deletions NetAF.Tests/Extensions/StringExtensions_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,15 @@ public void GivenAA_WhenInsensitiveEquals_ThenReturnaA()

Assert.AreEqual("aA", result);
}

[TestMethod]
public void GivenAA_WhenToTexture_ThenReturnATextureWithWidth2Height1()
{
var value = "AA";
var result = value.ToTexture();

Assert.AreEqual(2, result.Width);
Assert.AreEqual(1, result.Height);
}
}
}
30 changes: 30 additions & 0 deletions NetAF.Tests/Logic/Modes/DirectRenderMode_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NetAF.Logic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets.Characters;
using NetAF.Assets.Locations;
using NetAF.Utilities;
using NetAF.Logic.Modes;
using NetAF.Rendering.Console;

namespace NetAF.Tests.Logic.Modes
{
[TestClass]
public class DirectRenderMode_Tests
{
[TestMethod]
public void GivenNew_WhenRender_ThenNoExceptionThrown()
{
Assertions.NoExceptionThrown(() =>
{
RegionMaker regionMaker = new(string.Empty, string.Empty);
Room room = new(string.Empty, string.Empty);
regionMaker[0, 0, 0] = room;
OverworldMaker overworldMaker = new(string.Empty, string.Empty, regionMaker);
var game = Game.Create(new(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
var mode = new VisualMode(new GridTextFrame(new GridStringBuilder(), 0, 0, AnsiColor.Red));

mode.Render(game);
});
}
}
}
53 changes: 53 additions & 0 deletions NetAF.Tests/Rendering/Console/AnsiColor_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Rendering.Console;

namespace NetAF.Tests.Rendering.Console
{
[TestClass]
public class AnsiColor_Tests
{
[TestMethod]
public void GivenBlackAndBlack_WhenEquals_ThenReturnTrue()
{
var a = AnsiColor.Black;
var b = AnsiColor.Black;

var result = a.Equals(b);

Assert.IsTrue(result);
}

[TestMethod]
public void GivenBlackAndWhite_WhenEquals_ThenReturnFalse()
{
var a = AnsiColor.Black;
var b = AnsiColor.White;

var result = a.Equals(b);

Assert.IsFalse(result);
}

[TestMethod]
public void GivenBlackAndWhite_WhenEquality_ThenReturnFalse()
{
var a = AnsiColor.Black;
var b = AnsiColor.White;

var result = a == b;

Assert.IsFalse(result);
}

[TestMethod]
public void GivenBlackAndWhite_WhenNotEquality_ThenReturnTrue()
{
var a = AnsiColor.Black;
var b = AnsiColor.White;

var result = a != b;

Assert.IsTrue(result);
}
}
}
60 changes: 60 additions & 0 deletions NetAF.Tests/Rendering/Console/Ansi_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Rendering.Console;
using System;

namespace NetAF.Tests.Rendering.Console
{
[TestClass]
public class Ansi_Tests
{
[TestMethod]
public void GivenNoColorEnvironmentVariableSetToEmptyString_WhenIsColorSuppressed_ThenReturnFalse()
{
Environment.SetEnvironmentVariable(Ansi.NO_COLOR, "");

var result = Ansi.IsColorSuppressed();

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetTo0_WhenIsColorSuppressed_ThenReturnFalse()
{
Environment.SetEnvironmentVariable(Ansi.NO_COLOR, "0");

var result = Ansi.IsColorSuppressed();

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetToFalse_WhenIsColorSuppressed_ThenReturnFalse()
{
Environment.SetEnvironmentVariable(Ansi.NO_COLOR, "False");

var result = Ansi.IsColorSuppressed();

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetTo1_WhenIsColorSuppressed_ThenReturnTrue()
{
Environment.SetEnvironmentVariable(Ansi.NO_COLOR, "1");

var result = Ansi.IsColorSuppressed();

Assert.IsTrue(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetToTrue_WhenIsColorSuppressed_ThenReturnTrue()
{
Environment.SetEnvironmentVariable(Ansi.NO_COLOR, "True");

var result = Ansi.IsColorSuppressed();

Assert.IsTrue(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using NetAF.Logic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Rendering.FrameBuilders;
using NetAF.Rendering.FrameBuilders.Console;
using NetAF.Assets;
using NetAF.Rendering.Console.FrameBuilders;
using NetAF.Rendering.Console;

namespace NetAF.Tests.Rendering.FrameBuilders.Console
namespace NetAF.Tests.Rendering.Console.FrameBuilders
{
[TestClass]
public class ConsoleAboutFrameBuilder_Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets;
using NetAF.Commands.Scene;
using NetAF.Rendering.FrameBuilders;
using NetAF.Rendering.FrameBuilders.Console;
using NetAF.Rendering.Console;
using NetAF.Rendering.Console.FrameBuilders;

namespace NetAF.Tests.Rendering.FrameBuilders.Console
namespace NetAF.Tests.Rendering.Console.FrameBuilders
{
[TestClass]
public class ConsoleCommandListFrameBuilder_Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Assets;
using NetAF.Rendering.FrameBuilders;
using NetAF.Rendering.FrameBuilders.Console;
using NetAF.Rendering.Console;
using NetAF.Rendering.Console.FrameBuilders;

namespace NetAF.Tests.Rendering.FrameBuilders.Console
namespace NetAF.Tests.Rendering.Console.FrameBuilders
{
[TestClass]
public class ConsoleCompletionFrameBuilder_Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using NetAF.Assets.Characters;
using NetAF.Conversations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Rendering.FrameBuilders.Console;
using NetAF.Rendering.FrameBuilders;
using NetAF.Commands;
using NetAF.Assets;
using NetAF.Rendering.Console.FrameBuilders;
using NetAF.Rendering.Console;

namespace NetAF.Tests.Rendering.FrameBuilders.Console
namespace NetAF.Tests.Rendering.Console.FrameBuilders
{
[TestClass]
public class ConsoleConversationFrameBuilder_Tests
Expand Down
Loading

0 comments on commit f5d1885

Please sign in to comment.