-
Notifications
You must be signed in to change notification settings - Fork 0
/
water.lua
93 lines (80 loc) · 2.49 KB
/
water.lua
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module(..., package.seeall)
gas = require("gas")
--initialize water
waters = {}
waters.size = 0
buckets = {}
buckets.size = 0
--make water containers
--bucket = {}
--function bucket:new(x, y)--constructor
-- local instance = {x=x, y=y}
--a list of all objects in the water
objects_in_water = {}
--make individual water
water = {}
function water:new(x, y)--constructor
local instance = {}
instance.name = "water"
instance.body = display.newImage("WaterSprite1.png")--display.newCircle(x, y, 50)
instance.body.x = x
instance.body.y = y
mainDisplay.mainDisplay:insert(instance.body)
instance.body:toBack()
physics.addBody(instance.body, "kinematic", {radius = 50})
instance.body.isSensor = true
instance.body:addEventListener("collision", instance)
setmetatable(instance, {__index = water})
return instance
end
function water:collision(event)
if event.other.flammable ~= nil then
flammable_obj = event.other.flammable
if(event.phase == "began") then
if getmetatable(flammable_obj) == gas.gas_metatable then
flammable_obj:burn_up()
elseif flammable_obj.current_heat ~= nil then
--use flammable_obj as the key, with the value representing
--the number of pools this object is in
if objects_in_water[flammable_obj] == nil then
objects_in_water[flammable_obj] = 1
else
objects_in_water[flammable_obj] =
objects_in_water[flammable_obj] + 1
end
end
elseif flammable_obj.current_heat ~= nil then
objects_in_water[flammable_obj] =
objects_in_water[flammable_obj] - 1
--remove the reference once the object has left all pools
if objects_in_water[flammable_obj] <= 0 then
objects_in_water[flammable_obj] = nil
end
end
end
end
function spawn_water(x, y)
waters[waters.size + 1] = water:new(x, y)
waters.size = waters.size + 1
end
function on_enter_frame(elapsed_time)
for object, v in pairs(objects_in_water) do
object.current_heat = object.current_heat
- object.heat_increase_rate * 3 * elapsed_time
if object.current_heat < 0 then
object.current_heat = 0
elseif object.current_heat >= object.flash_point then
object.current_heat = object.flash_point - 1
end
end
end
function remove_all_water()
for i, water in ipairs(waters) do
water.body:removeSelf()
waters[i] = nil
end
waters.size = 0
for k, v in pairs(objects_in_water) do
objects_in_water[k] = nil
end
end