-
Notifications
You must be signed in to change notification settings - Fork 26
/
Weapon.ttslua
79 lines (62 loc) · 2.41 KB
/
Weapon.ttslua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local Check = require("Kdm/Util/Check")
local Expansion = require("Kdm/Expansion")
---------------------------------------------------------------------------------------------------
local Weapon = {}
Weapon.__index = Weapon
---------------------------------------------------------------------------------------------------
function Weapon.Init()
Weapon.weapons = {}
for _, expansion in ipairs(Expansion.All()) do
for name, stats in pairs(expansion.weaponStats or {}) do
if Weapon.weapons[name] then
assert(false, string.format("Weapon %s was already registered by expansion %s", name, Weapon.weapons[name].expansion))
end
Weapon.weapons[name] = Weapon.Create(name, expansion.name, stats)
end
end
end
---------------------------------------------------------------------------------------------------
function Weapon.Get(name)
return Weapon.weapons[name]
end
---------------------------------------------------------------------------------------------------
Weapon.statCheckFuncs = {
accuracy = Check.Num,
deadly = Check.Num,
devastating = Check.Num,
earlyIron = Check,
paired = Check,
savage = Check,
sharp = Check,
slow = Check,
speed = Check.Num,
strength = Check.Num,
}
function Weapon.Create(name, expansion, stats)
assert(Check.Str(name))
assert(Check.Str(expansion))
local weapon = {
name = name,
expansion = expansion,
}
setmetatable(weapon, Weapon)
for stat, value in pairs(stats) do
local checkFunc = Weapon.statCheckFuncs[stat]
assert(Check(checkFunc, "Unrecognized stat %s for weapon %s", stat, name))
assert(checkFunc(value, "Stat %s had unexpected value %s", stat, tostring(value)))
weapon[stat] = value
end
assert(Check.Num(weapon.speed, "Weapon %s missing speed", name))
assert(Check.Num(weapon.accuracy, "Weapon %s missing accuracy", name))
assert(Check.Num(weapon.strength, "Weapon %s missing strength", name))
return weapon
end
---------------------------------------------------------------------------------------------------
function Weapon:__tostring()
return string.format("%s (%d/%d/%d)", self.name, self.speed, self.accuracy, self.strength)
end
---------------------------------------------------------------------------------------------------
return {
Init = Weapon.Init,
Get = Weapon.Get,
}