Skip to content

Commit

Permalink
Added functionality to prevent all playable characters being able to …
Browse files Browse the repository at this point in the history
…take or drop items
  • Loading branch information
ben_pollard committed Jan 10, 2025
1 parent 87113da commit 5aff9c6
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 7 deletions.
19 changes: 19 additions & 0 deletions NetAF.Tests/Commands/Scene/Drop_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ public void GivenNoCharacter_WhenInvoke_ThenError()
Assert.AreEqual(ReactionResult.Error, result.Result);
}

[TestMethod]
public void GivenCannotTakeOrDropItems_WhenInvoke_ThenError()
{
var room = new Room(Identifier.Empty, Description.Empty);
var region = new Region(string.Empty, string.Empty);
region.AddRoom(room, 0, 0, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var character = new PlayableCharacter(Identifier.Empty, Description.Empty, false, false);
var item = new Item(new Identifier("A"), Description.Empty, true);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, character), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
game.Overworld.CurrentRegion.Enter();
var command = new Drop(item);

var result = command.Invoke(game);

Assert.AreEqual(ReactionResult.Error, result.Result);
}

[TestMethod]
public void GivenNoItem_WhenInvoke_ThenError()
{
Expand Down
36 changes: 36 additions & 0 deletions NetAF.Tests/Commands/Scene/TakeAll_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,42 @@ namespace NetAF.Tests.Commands.Scene
[TestClass]
public class TakeAll_Tests
{
[TestMethod]
public void GivenNoCharacter_WhenInvoke_ThenError()
{
var room = new Room(Identifier.Empty, Description.Empty);
var region = new Region(string.Empty, string.Empty);
region.AddRoom(room, 0, 0, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, null), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
game.Overworld.CurrentRegion.Enter();
var command = new TakeAll();

var result = command.Invoke(game);

Assert.AreEqual(ReactionResult.Error, result.Result);
}

[TestMethod]
public void GivenCannotTakeOrDropItems_WhenInvoke_ThenError()
{
var room = new Room(Identifier.Empty, Description.Empty);
var region = new Region(string.Empty, string.Empty);
region.AddRoom(room, 0, 0, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var character = new PlayableCharacter(Identifier.Empty, Description.Empty, false, false);
var item = new Item(new Identifier("A"), Description.Empty, true);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, character), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
game.Overworld.CurrentRegion.Enter();
var command = new TakeAll();

var result = command.Invoke(game);

Assert.AreEqual(ReactionResult.Error, result.Result);
}

[TestMethod]
public void GivenNoItem_WhenInvoke_ThenError()
{
Expand Down
19 changes: 19 additions & 0 deletions NetAF.Tests/Commands/Scene/Take_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ public void GivenNoCharacter_WhenInvoke_ThenError()
Assert.AreEqual(ReactionResult.Error, result.Result);
}

[TestMethod]
public void GivenCannotTakeOrDropItems_WhenInvoke_ThenError()
{
var room = new Room(Identifier.Empty, Description.Empty);
var region = new Region(string.Empty, string.Empty);
region.AddRoom(room, 0, 0, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var character = new PlayableCharacter(Identifier.Empty, Description.Empty, false, false);
var item = new Item(new Identifier("A"), Description.Empty, true);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, character), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
game.Overworld.CurrentRegion.Enter();
var command = new Take(item);

var result = command.Invoke(game);

Assert.AreEqual(ReactionResult.Error, result.Result);
}

[TestMethod]
public void GivenNoItem_WhenInvoke_ThenError()
{
Expand Down
18 changes: 13 additions & 5 deletions NetAF/Assets/Characters/PlayableCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public sealed class PlayableCharacter : Character
/// </summary>
public bool CanConverse { get; private set; }

/// <summary>
/// Get if this playable character can take and drop items.
/// </summary>
public bool CanTakeAndDropItems { get; private set; }

#endregion

#region Constructors
Expand All @@ -40,7 +45,7 @@ public sealed class PlayableCharacter : Character
/// <param name="commands">This objects commands.</param>
/// <param name="interaction">The interaction.</param>
/// <param name="examination">The examination.</param>
public PlayableCharacter(Identifier identifier, IDescription description, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null) : this(identifier, description, true, items, commands, interaction, examination)
public PlayableCharacter(Identifier identifier, IDescription description, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null) : this(identifier, description, true, true, items, commands, interaction, examination)
{
}

Expand All @@ -49,12 +54,13 @@ public PlayableCharacter(Identifier identifier, IDescription description, Item[]
/// </summary>
/// <param name="identifier">The identifier.</param>
/// <param name="description">The description.</param>
/// <param name="canConverse">If this object can converse with an IConverser.</param>
/// <param name="canConverse">If this playable character can converse with an IConverser.</param>
/// <param name="canTakeAndDropItems">If this playable character can take and drop items.</param>
/// <param name="items">The items.</param>
/// <param name="commands">This objects commands.</param>
/// <param name="interaction">The interaction.</param>
/// <param name="examination">The examination.</param>
public PlayableCharacter(string identifier, string description, bool canConverse, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null) : this(new Identifier(identifier), new Description(description), canConverse, items, commands, interaction, examination)
public PlayableCharacter(string identifier, string description, bool canConverse, bool canTakeAndDropItems = true, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null) : this(new Identifier(identifier), new Description(description), canConverse, canTakeAndDropItems, items, commands, interaction, examination)
{
}

Expand All @@ -63,16 +69,18 @@ public PlayableCharacter(Identifier identifier, IDescription description, Item[]
/// </summary>
/// <param name="identifier">The identifier.</param>
/// <param name="description">The description.</param>
/// <param name="canConverse">If this object can converse with an IConverser.</param>
/// <param name="canConverse">If this playable character can converse with an IConverser.</param>
/// <param name="canTakeAndDropItems">If this playable character can take and drop items.</param>
/// <param name="items">The items.</param>
/// <param name="commands">This objects commands.</param>
/// <param name="interaction">The interaction.</param>
/// <param name="examination">The examination.</param>
public PlayableCharacter(Identifier identifier, IDescription description, bool canConverse, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null)
public PlayableCharacter(Identifier identifier, IDescription description, bool canConverse, bool canTakeAndDropItems = true, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null)
{
Identifier = identifier;
Description = description;
CanConverse = canConverse;
CanTakeAndDropItems = canTakeAndDropItems;
Items = items ?? [];
Commands = commands ?? [];
Interaction = interaction ?? (i => new(InteractionResult.NoChange, i));
Expand Down
3 changes: 3 additions & 0 deletions NetAF/Commands/Scene/Drop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public Reaction Invoke(Logic.Game game)
if (game.Player == null)
return new(ReactionResult.Error, "You must specify a character.");

if (!game.Player.CanTakeAndDropItems)
return new(ReactionResult.Error, "The player cannot drop items.");

if (item == null)
return new(ReactionResult.Error, "You must specify what to drop.");

Expand Down
3 changes: 3 additions & 0 deletions NetAF/Commands/Scene/Take.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public Reaction Invoke(Logic.Game game)
if (game.Player == null)
return new(ReactionResult.Error, "You must specify a character.");

if (!game.Player.CanTakeAndDropItems)
return new(ReactionResult.Error, "The player cannot take items.");

if (item == null)
return new(ReactionResult.Error, "You must specify what to take.");

Expand Down
3 changes: 3 additions & 0 deletions NetAF/Commands/Scene/TakeAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public Reaction Invoke(Logic.Game game)
if (game.Player == null)
return new(ReactionResult.Error, "You must specify a character.");

if (!game.Player.CanTakeAndDropItems)
return new(ReactionResult.Error, "The player cannot take items.");

if (game.Overworld.CurrentRegion.CurrentRoom == null)
return new(ReactionResult.Error, "Not in a room.");

Expand Down
4 changes: 2 additions & 2 deletions NetAF/Interpretation/SceneCommandInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,10 @@ public CommandHelp[] GetContextualCommandHelp(Game game)

commands.Add(Examine.CommandHelp);

if (game.Player.Items.Any())
if (game.Player.Items.Any() && game.Player.CanTakeAndDropItems)
commands.Add(Drop.CommandHelp);

if (game.Overworld.CurrentRegion.CurrentRoom.Items.Any())
if (game.Overworld.CurrentRegion.CurrentRoom.Items.Any() && game.Player.CanTakeAndDropItems)
{
commands.Add(Take.CommandHelp);
commands.Add(TakeAll.CommandHelp);
Expand Down

0 comments on commit 5aff9c6

Please sign in to comment.