Skip to content

Commit d48230b

Browse files
author
Paavo Bennett
authored
Merge pull request #1 from eliw00d/1.1.0
2.0.0
2 parents aacddaf + 5d8cbf2 commit d48230b

File tree

10 files changed

+362
-563
lines changed

10 files changed

+362
-563
lines changed

data/scar/winconditions/class.scar

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
1-
function Class(superClass, constructor)
2-
local class = {};
3-
4-
if superClass then
5-
for k,v in pairs(superClass)
6-
do
7-
class[k] = v;
8-
end
9-
10-
class._superClass = superClass;
11-
end
12-
13-
class.__index = class;
14-
15-
local metatable = {};
1+
function Class()
2+
local class = {}
3+
class.__index = class
4+
5+
function class:extends(super)
6+
self.__super = super
7+
for k,v in pairs(super) do
8+
if (k ~= '__index'
9+
and k ~= 'constructor'
10+
and k ~= 'new') then
11+
self[k] = v
12+
end
13+
end
14+
15+
return super
16+
end
17+
18+
function class:new(...)
19+
local instance = {}
20+
setmetatable(instance, self)
21+
22+
if (self.constructor) then
23+
self.constructor(instance, ...)
24+
end
25+
26+
return instance
27+
end
28+
29+
function class:clone()
30+
local clone = {}
31+
32+
for k,v in pairs(self) do
33+
clone[k] = v
34+
end
35+
36+
return clone
37+
end
38+
39+
function class:isA(other)
40+
local metatable = getmetatable(self)
41+
42+
while (metatable) do
43+
if (metatable == other) then
44+
return true
45+
end
46+
metatable = metatable.__super
47+
end
48+
49+
return false
50+
end
51+
52+
local metatable = {}
53+
setmetatable(class, metatable)
1654

17-
function metatable:__call(...)
18-
local instance = {};
19-
setmetatable(instance, class);
20-
21-
if class._constructor then
22-
class._constructor(instance, ...);
55+
function metatable:__call(instance, ...)
56+
if (class.constructor) then
57+
class.constructor(instance, ...)
2358
end
24-
25-
return instance;
2659
end
27-
28-
class._constructor = constructor;
29-
30-
function class:isA(otherClass)
31-
local mt = getmetatable(self);
32-
33-
while mt
34-
do
35-
if mt == otherClass then
36-
return true;
37-
end
38-
mt = mt._superClass;
39-
end
40-
41-
return false;
60+
61+
function metatable:__tostring()
62+
return self.name
4263
end
43-
44-
setmetatable(class, metatable);
45-
46-
return class;
47-
end
64+
65+
return class
66+
end
Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,36 @@
1-
import("WinConditions/Class.scar")
1+
Color = Class()
22

3-
--[[ The Color class contains the red, green, blue, and alpha values needed to create Colors.
4-
--
5-
-- Constructor: Color([r], [g], [b], [a])
6-
--]]
7-
Color = Class(nil, function(self, red, green, blue, alpha)
8-
self._red = red or 0;
9-
self._green = green or 0;
10-
self._blue = blue or 0;
11-
self._alpha = alpha or 255;
12-
end)
3+
function Color:constructor(red, green, blue, alpha)
4+
self._red = red or 0
5+
self._green = green or 0
6+
self._blue = blue or 0
7+
self._alpha = alpha or 255
8+
end
139

1410
-- Primary Colors
15-
Color.Red = Color(255, 0, 0);
16-
Color.Green = Color(0, 255, 0);
17-
Color.Blue = Color(0, 0, 255);
11+
Color.Red = Color:new(255, 0, 0)
12+
Color.Green = Color:new(0, 255, 0)
13+
Color.Blue = Color:new(0, 0, 255)
1814

1915
-- Other Colors
20-
Color.Black = Color(0, 0, 0);
21-
Color.White = Color(255, 255, 255);
16+
Color.Black = Color:new(0, 0, 0)
17+
Color.White = Color:new(255, 255, 255)
2218

2319
-- Custom Colors
2420
-- TODO: Add your own colors here.
25-
26-
--[[ Gets the Red value of this Color.
27-
--
28-
-- @return Red value
29-
--]]
21+
3022
function Color:getRed()
31-
return self._red;
23+
return self._red
3224
end
3325

34-
--[[ Gets the Green value in this Color.
35-
--
36-
-- @return Green value
37-
--]]
3826
function Color:getGreen()
39-
return self._green;
27+
return self._green
4028
end
4129

42-
--[[ Gets the Blue value in this Color.
43-
--
44-
-- @return Blue value
45-
--]]
4630
function Color:getBlue()
47-
return self._blue;
31+
return self._blue
4832
end
4933

50-
--[[ Gets the Alpha value in this Color.
51-
--
52-
-- @return Alpha value
53-
--]]
5434
function Color:getAlpha()
55-
return self._alpha;
56-
end
35+
return self._alpha
36+
end

0 commit comments

Comments
 (0)