-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComponents.lua
More file actions
63 lines (57 loc) · 1.64 KB
/
Components.lua
File metadata and controls
63 lines (57 loc) · 1.64 KB
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
local bit = require 'libraries/bitop.funcs'
require 'utils'
-- Create's a health component
local function ComponentHealth(value)
local health = {
name = 'health',
value = value or 20
}
return health
end
local function ComponentAppearance(args)
local args = args or {}
local colors = args.colors or { r = math.random(), g = math.random(), b = math.random() }
local size = args.size or bit.bor(1 + (math.floor(math.random() * 30)), 0)
local appearance = {
name = 'appearance',
colors = colors,
size = size
}
return appearance
end
local function ComponentPosition(args)
local args = args or {}
-- Generate random values if not passed in
-- NOTE: For the tutorial we're coupling the random values to the canvas'
-- width / height, but ideally this would be decoupled (the component should
-- not need to know the canvas's dimensions)
local position = {
name = 'position',
x = args.x or 20 + bit.bor(math.floor(math.random() * (love.graphics.getWidth() - 20)), 0),
y = args.y or 20 + bit.bor(math.floor(math.random() * (love.graphics.getHeight() - 20)), 0)
}
return position
end
local function ComponentPlayerControlled(_args)
-- local args = args or {}
local playerControlled = {
name = 'playerControlled',
pc = true,
}
return playerControlled
end
local function ComponentCollision(args)
local args = args or {}
local collision = {
name = 'collision',
collides = true
}
return collision
end
return {
Health = ComponentHealth,
Appearance = ComponentAppearance,
Position = ComponentPosition,
PlayerControlled = ComponentPlayerControlled,
Collision = ComponentCollision
}