I use Codea on my iPad, its a Lua scripting app which is built with C i think. Im nokt very good st it
class() is actually a special function that I don't understand because I still don't get how metatables worl. But here is the code for that
I do this kind of stuff on my ipad more as a curiosity and practicing to pass time, not intending to actually make anything serious. But is this looking right to any Lua programmers here?
This is nowhere near finished btw, I was just wondering how would be best to actually make a barracks. Its surprisingly complex for me, since the most complex thing ive done up to this point was making a scrolling star map
USA
katsup or mustard
Barracks = class()
Barracks.darkAge = readImage("Dropbox:Barracks Dark age")
Barracks.feGoth = readImage("Dropbox:Barracks1")
Barracks.feArab = readImage("Dropbox:Barracks3")
Barracks.feCelt = readImage("Dropbox:Barracks4")
Barracks.feEast = readImage("Dropbox:Barracks2")
Barracks.feMeso = readImage("Dropbox:Barracks9")
Barracks.caGoth = readImage("Dropbox:Barracks5")
Barracks.caArab = readImage("Dropbox:Barracks7")
Barracks.caCelt = readImage("Dropbox:Barracks8")
Barracks.caEast = readImage("Dropbox:Barracks6")
Barracks.caMeso = readImage("Dropbox:Barracks10")
function Barracks:init(x,y,hp,player)
self.pos = vec2(x,y)
self.hp1 = hp
self.hp2 = hp --max Hit Points
self.player = player
self.armor1 = 5
self.armor2 = 10
self.attack = nil
self.range = nil
end
function Barracks:addHP(add1,add2)
-- add1 or add2 can be negative
-- Call this method when a unit attacks/repairs, technology changes maximum Hit Points or trigger is run
if add2 then
local m = get_percent(hp1,hp2)
self.hp1 = self.hp1 + hp2 * m/100
self.hp2 = self.hp2 + add2
end
if add1 then
self.hp = self.hp + hp1
end
end
function Barracks:touched(touch)
--[[
If no unit is currently selected when the Barracks is touched it will be
selected, the GUI is updated and it plays a sound
--]]
end
class() is actually a special function that I don't understand because I still don't get how metatables worl. But here is the code for that
-- Class.lua
-- Compatible with Lua 5.1 (not 5.0).
function class(base)
local c = {} -- a new class instance
if type(base) == 'table' then
-- our new class is a shallow copy of the base class!
for i,v in pairs(base) do
c[i] = v
end
c._base = base
end
-- the class will be the metatable for all its objects,
-- and they will look up their methods in it.
c.__index = c
-- expose a constructor which can be called by <classname> (< args> )
local mt = {}
mt.__call = function(class_tbl, ...)
local obj = {}
setmetatable(obj,c)
if class_tbl.init then
class_tbl.init(obj,...)
else
-- make sure that any stuff from the base class is initialized!
if base and base.init then
base.init(obj, ...)
end
end
return obj
end
c.is_a = function(self, klass)
local m = getmetatable(self)
while m do
if m == klass then return true end
m = m._base
end
return false
end
setmetatable(c, mt)
return c
end
I do this kind of stuff on my ipad more as a curiosity and practicing to pass time, not intending to actually make anything serious. But is this looking right to any Lua programmers here?
This is nowhere near finished btw, I was just wondering how would be best to actually make a barracks. Its surprisingly complex for me, since the most complex thing ive done up to this point was making a scrolling star map
USA
katsup or mustard
[This message has been edited by local boi (edited 01-28-2017 @ 02:20 AM).]