Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Derives Being from EntityID.
Browse files Browse the repository at this point in the history
  • Loading branch information
mxashlynn committed Apr 15, 2019
1 parent 8747f37 commit 0667239
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 117 deletions.
119 changes: 20 additions & 99 deletions ParquetClassLibrary/Characters/Being.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using ParquetClassLibrary.Sandbox;
using ParquetClassLibrary.Sandbox.IDs;
using ParquetClassLibrary.Utilities;
using System;

namespace ParquetClassLibrary.Characters
{
/// <summary>
/// Models a character in the game.
/// Models the basic definitions shared by any in-game actor.
/// </summary>
public abstract class Being : IEquatable<Being>
public abstract class Being : Entity
{
#region Characteristics
/// <summary>
Expand All @@ -19,9 +20,6 @@ public abstract class Being : IEquatable<Being>
/// <summary>Tracks how many times the data structure has been serialized.</summary>
public int Revision { get; private set; }

/// <summary>The character's unique identifier.</summary>
public readonly Guid ID;

/// <summary>The <see cref="Biome"/> in which this character is at home.</summary>
public Biome NativeBiome { get; set; }

Expand All @@ -36,104 +34,27 @@ public abstract class Being : IEquatable<Being>
/// <summary>
/// Used by <see cref="Being"/> subtypes.
/// </summary>
/// <param name="in_nativeBiome">The <see cref="Biome"/> in which this character is most comfortable.</param>
/// <param name="in_currentBehavior">The rules that govern how this character acts. Cannot be null.</param>
/// <param name="in_currentLocation">Where this character currently is.</param>
/// <param name="in_id">Unique identifier for the character. If null, a new ID will be created.</param>
protected Being(Biome in_nativeBiome, Behavior in_currentBehavior, Location in_currentLocation,
Guid? in_id = null)
/// <param name="in_bounds">
/// The bounds within which the <see cref="Being"/>'s <see cref="EntityID"/> is defined.
/// Must be one of <see cref="AssemblyInfo.BeingIDs"/>.
/// </param>
/// <param name="in_id">Unique identifier for the <see cref="Being"/>. Cannot be null.</param>
/// <param name="in_name">Player-friendly name of the <see cref="Being"/>. Cannot be null or empty.</param>
/// <param name="in_nativeBiome">The <see cref="Biome"/> in which this <see cref="Being"/> is most comfortable.</param>
/// <param name="in_currentBehavior">The rules that govern how this <see cref="Being"/> acts. Cannot be null.</param>
/// <param name="in_currentLocation">Where this <see cref="Being"/> currently is.</param>
protected Being(Range<EntityID> in_bounds, EntityID in_id, string in_name, Biome in_nativeBiome,
Behavior in_currentBehavior, Location in_currentLocation)
: base(in_bounds, in_id, in_name)
{
ID = in_id ?? Guid.NewGuid();
NativeBiome = in_nativeBiome;
CurrentBehavior = in_currentBehavior;
CurrentLocation = in_currentLocation;
}
#endregion

#region IEquatable Implementation
/// <summary>
/// Serves as a hash function for a <see cref="T:ParquetClassLibrary.Sandbox.Parquets.ParquetParent"/> struct.
/// </summary>
/// <returns>
/// A hash code for this instance that is suitable for use in hashing algorithms and data structures.
/// </returns>
public override int GetHashCode()
{
return ID.GetHashCode();
}

/// <summary>
/// Determines whether the specified <see cref="Being"/> is equal to the current
/// <see cref="Being"/>.
/// </summary>
/// <param name="in_character">The <see cref="Being"/> to compare against this one.</param>
/// <returns><c>true</c> if the <see cref="Being"/>s are equal.</returns>
public bool Equals(Being in_character)
{
return null != in_character && ID == in_character.ID;
}

/// <summary>
/// Determines whether the specified <see cref="object"/> is equal to the current <see cref="Being"/>.
/// </summary>
/// <param name="obj">The <see cref="object"/> to compare with this <see cref="Being"/>.</param>
/// <returns>
/// <c>true</c> if the given <see cref="object"/> is equal to this <see cref="Being"/>; otherwise, <c>false</c>.
/// </returns>
// ReSharper disable once InconsistentNaming
public override bool Equals(object obj)
{
var result = false;

if (obj is Being character)
if (!AssemblyInfo.BeingIDs.ContainsRange(in_bounds))
{
result = Equals(character);
throw new ArgumentOutOfRangeException(nameof(in_bounds));
}

return result;
}

/// <summary>
/// Determines whether a specified instance of <see cref="Being"/> is equal to
/// another specified instance of <see cref="Being"/>.
/// </summary>
/// <param name="in_character1">The first <see cref="Being"/> to compare.</param>
/// <param name="in_character2">The second <see cref="Being"/> to compare.</param>
/// <returns><c>true</c> if they are equal; otherwise, <c>false</c>.</returns>
public static bool operator ==(Being in_character1, Being in_character2)
{
if (ReferenceEquals(in_character1, in_character2)) return true;
if (ReferenceEquals(in_character1, null)) return false;
if (ReferenceEquals(in_character2, null)) return false;

return in_character1.ID == in_character2.ID;
}

/// <summary>
/// Determines whether a specified instance of <see cref="Being"/> is unequal
/// to another specified instance of <see cref="Being"/>.
/// </summary>
/// <param name="in_character1">The first <see cref="ParquetClassLibrary.Sandbox.Parquets.ParquetParent"/> to compare.</param>
/// <param name="in_character2">The second <see cref="ParquetClassLibrary.Sandbox.Parquets.ParquetParent"/> to compare.</param>
/// <returns><c>true</c> if <c>in_vector1</c> and <c>in_vector2</c> are NOT equal; otherwise, <c>false</c>.</returns>
public static bool operator !=(Being in_character1, Being in_character2)
{
if (ReferenceEquals(in_character1, in_character2)) return false;
if (ReferenceEquals(in_character1, null)) return true;
if (ReferenceEquals(in_character2, null)) return true;

return in_character1.ID != in_character2.ID;
}
#endregion

#region Utility Methods
/// <summary>
/// Returns a <see cref="string"/> that represents this <see cref="Being"/>.
/// </summary>
/// <returns>The representation.</returns>
public override string ToString()
{
return ID.ToString();
NativeBiome = in_nativeBiome;
CurrentBehavior = in_currentBehavior;
CurrentLocation = in_currentLocation;
}
#endregion
}
Expand Down
41 changes: 23 additions & 18 deletions ParquetClassLibrary/Characters/Critter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
using System.Collections.Generic;
using ParquetClassLibrary.Sandbox;
using ParquetClassLibrary.Sandbox.IDs;
using ParquetClassLibrary.Utilities;

namespace ParquetClassLibrary.Characters
{
public class Critter : Being
// IDEA
// Move these content into a Being class and change Critter to only check that the EntityID is in fact a CritterID.
/// <summary>
/// Models the definition for a simple in-game actor, such as a friendly mob with limited interaction.
/// </summary>
public sealed class Critter : Being
{
#region Characteristics
/// <summary>The type of critter this is.</summary>
public EntityID CritterType { get; private set; }

/// <summary>Types of parquets this critter avoids.</summary>
public readonly List<EntityID> Avoids = new List<EntityID>();

Expand All @@ -20,24 +23,27 @@ public class Critter : Being

#region Initialization
/// <summary>
/// Initializes a new instance of the <see cref="T:ParquetClassLibrary.Characters.Critter"/> class.
/// Initializes a new instance of the <see cref="Critter"/> class.
/// </summary>
/// <param name="in_nativeBiome">The <see cref="Biome"/> in which this character is most comfortable.</param>
/// <param name="in_currentBehavior">The rules that govern how this character acts. Cannot be null.</param>
/// <param name="in_currentLocation">Where this character currently is.</param>
/// <param name="in_id">Unique identifier for the character. If null, a new ID will be created.</param>
/// <param name="in_critterType">The type of critter this is.</param>
/// <param name="in_bounds">
/// The bounds within which the <see cref="Critter"/>'s <see cref="EntityID"/> is defined.
/// Must be a <see cref="AssemblyInfo.CritterIDs"/>.
/// </param>
/// <param name="in_id">Unique identifier for the <see cref="Being"/>. Cannot be null.</param>
/// <param name="in_name">Player-friendly name of the <see cref="Being"/>. Cannot be null or empty.</param>
/// <param name="in_nativeBiome">The <see cref="Biome"/> in which this <see cref="Being"/> is most comfortable.</param>
/// <param name="in_currentBehavior">The rules that govern how this <see cref="Being"/> acts. Cannot be null.</param>
/// <param name="in_currentLocation">Where this <see cref="Being"/> currently is.</param>
/// <param name="in_avoids">Any parquets this critter avoids.</param>
/// <param name="in_seeks">Any parquets this critter seeks.</param>
protected Critter(Biome in_nativeBiome, Behavior in_currentBehavior, Location in_currentLocation,
Guid? in_id = null, EntityID? in_critterType = null,
List<EntityID> in_avoids = null, List<EntityID> in_seeks = null)
: base(in_nativeBiome, in_currentBehavior, in_currentLocation, in_id)
public Critter(Range<EntityID> in_bounds, EntityID in_id, string in_name, Biome in_nativeBiome,
Behavior in_currentBehavior, Location in_currentLocation,
List<EntityID> in_avoids = null, List<EntityID> in_seeks = null)
: base(in_bounds, in_id, in_name, in_nativeBiome, in_currentBehavior, in_currentLocation)
{
var nonNullCritterType = in_critterType ?? EntityID.None;
if (!nonNullCritterType.IsValidForRange(AssemblyInfo.CritterIDs))
if (!AssemblyInfo.CritterIDs.ContainsRange(in_bounds))
{
throw new ArgumentOutOfRangeException(nameof(in_critterType));
throw new ArgumentOutOfRangeException(nameof(in_bounds));
}
foreach (var parquetID in in_avoids)
{
Expand All @@ -60,7 +66,6 @@ public class Critter : Being
}
}

CritterType = nonNullCritterType;
Avoids.AddRange(in_avoids);
Seeks.AddRange(in_seeks);
}
Expand Down

0 comments on commit 0667239

Please sign in to comment.