types.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import "fmt"
  3. // Identification Number
  4. //
  5. // Used to separate things into a unique thing
  6. type Id uint64
  7. // An Id is considered valid so long as it's not 0
  8. func (i Id) valid() bool {
  9. return i != 0
  10. }
  11. // Render of an Id is simply prefixing it with #
  12. //
  13. // i.e. Id=5 .render() "#5"
  14. func (i Id) render() string {
  15. return fmt.Sprintf("#%d", i)
  16. }
  17. // Kind is used to separate a general Object into specific categories
  18. //
  19. // Players, Guilds, Rooms, Items, Creatures, Vehicles
  20. //
  21. // e.g. Players, Items, Creatures and Vehicles have a "health" (Items and Vehicles use the same name for their durability)
  22. //
  23. // But a Room won't use "health" (Despite it existing)
  24. type Kind uint8
  25. // A Kind is valid so long as it's not 0 or None
  26. func (k Kind) valid() bool {
  27. return k != KNone
  28. }
  29. // Renders the Kind as a name
  30. //
  31. // i.e. Kind=Guild (2) .render() "Guild"
  32. func (k Kind) render() string {
  33. switch k {
  34. case KPlayer:
  35. return "Player"
  36. case KGuild:
  37. return "Guild"
  38. case KRoom:
  39. return "Room"
  40. case KItem:
  41. return "Item"
  42. case KCreature:
  43. return "Creature"
  44. case KVehicle:
  45. return "Vehicle"
  46. default:
  47. return "Unknown"
  48. }
  49. }
  50. const (
  51. // An Invalid Kind
  52. KNone Kind = iota
  53. // A Creature, but controlled by a Human
  54. KPlayer
  55. // A Collection of 1 or more Players (Inventory is shared among Players, etc.)
  56. KGuild
  57. // A Room, which can hold Players, Items, Creatures, Vehicles and Containers
  58. KRoom
  59. // A Item, This is broad, but most Items should specify a specific thing (Weapon's for instance would have a "damage", Armor "defense", Gold on the other hand might have "value")
  60. KItem
  61. // A Entity that is controlled by either a Human's Logic (Scripted in some manner), or by the computer (Scripted internally)
  62. KCreature
  63. // A Entity / Room Hybrid, it behaves like both a Room and a Creature, Players and Creatures can enter a Vehicle, then where ever the Vehicle goes they are moved along with it
  64. KVehicle
  65. )
  66. // A ValueRange is something which has a current Value and a Max
  67. //
  68. // Some ValueRanges are Health, Magic, and Build Points
  69. type ValueRange struct {
  70. Value uint64
  71. Max uint64
  72. }
  73. // ValueRanges are invalid when Max is 0, or Value is greater than Max
  74. func (v ValueRange) valid() bool {
  75. if v.Max == 0 {
  76. return false
  77. }
  78. return v.Value <= v.Max
  79. }
  80. // A 0 to 100 of the percentage the Range is at based on Value and Max
  81. //
  82. // This method will return 0 if the ValueRange is invalid
  83. func (v *ValueRange) Percent() uint8 {
  84. if IsInvalid(v) {
  85. return 0
  86. }
  87. return uint8((float32(v.Value) / float32(v.Max)) * 100.0)
  88. }
  89. // Forms a good default for a ValueRange
  90. //
  91. // i.e. ValueRange{Value: 5, Max: 10} .render() "5/10"
  92. func (v ValueRange) render() string {
  93. if IsInvalid(v) {
  94. return ""
  95. }
  96. return fmt.Sprintf("%d/%d", v.Value, v.Max)
  97. }