Skip to content

Instantly share code, notes, and snippets.

@PJensen
Created October 21, 2015 22:53
Show Gist options
  • Save PJensen/6d225f8ec801d62d2202 to your computer and use it in GitHub Desktop.
Save PJensen/6d225f8ec801d62d2202 to your computer and use it in GitHub Desktop.
The following demonstrates a basic idea for socket-able weapons.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SocketableWeapons
{
internal class Program
{
private static void Main(string[] args)
{
var random = new Random();
var shortsword = new Weapon(2, "Iron Shortword")
{
Damage = 23
};
// 50% chance to drain mana
shortsword.SocketedGems[0] = new Gem("Midnight Sapphire")
{
HitAction = delegate(Weapon self, Actor target)
{
if (random.Next() % 2 == 0)
{
target.Mana -= 1;
}
}
};
// 50% chace to deal an additional 50% damage
shortsword.SocketedGems[1] = new Gem("Rage Ruby")
{
HitAction = delegate(Weapon self, Actor target)
{
if (random.Next() % 2 == 0)
{
target.Health -= self.Damage / 2;
}
}
};
var baddy = new Actor
{
Health = 100,
Mana = 100,
Name = "Badie"
};
shortsword.OnHit(baddy);
Console.WriteLine(baddy);
Console.ReadKey();
}
public class Actor
{
public string Name { get; set; }
public int Health { get; set; }
public int Mana { get; set; }
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
return string.Format("Name: {0}, Health: {1}, Mana: {2}", Name, Health, Mana);
}
}
public class Gem
{
public string Name { get; set; }
public Gem(string name)
{
Name = name;
}
public WeaponAction HitAction { get; set; }
}
internal delegate void WeaponAction(Weapon self, Actor target);
public class Weapon : Socketable
{
public string Name { get; set; }
public int Damage { get; set; }
public Weapon(int sockets, string name)
: base(sockets)
{
Name = name;
}
public void OnHit(Actor target)
{
foreach (var socketedGem in SocketedGems)
{
socketedGem.HitAction(this, target);
}
}
}
public class Socketable : IEnumerable<Gem>
{
public Socketable(int count)
{
_socketedGems = new Gem[count];
}
private readonly Gem[] _socketedGems;
public Gem[] SocketedGems { get { return _socketedGems; } }
public IEnumerator<Gem> GetEnumerator()
{
return ((IEnumerable<Gem>) SocketedGems).GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment