Skip to content

Instantly share code, notes, and snippets.

@JettMonstersGoBoom
Last active February 7, 2018 21:46
Show Gist options
  • Save JettMonstersGoBoom/4e9a3d5b672a5ad4c44082d53ccb407e to your computer and use it in GitHub Desktop.
Save JettMonstersGoBoom/4e9a3d5b672a5ad4c44082d53ccb407e to your computer and use it in GitHub Desktop.
-- title: game title
-- author: game developer
-- desc: short description
-- script: lua
function table.remove(a,b)
for c,d in ipairs(a) do
if d==b then
a[c]=a[#a]
a[#a]=nil
return
end
end
end
----------------------------------------
-- Classic (Rxi) : https://github.com/rxi/classic
----------------------------------------
local Class = {}
Class.__index = Class
function Class:new()end
function Class:extend()local a={}for b,c in pairs(self)do if b:find("__")==1 then a[b]=c end end;a.__index=a;a.super=self;setmetatable(a,self)return a end
function Class:__call(...)local a=setmetatable({},self)a:new(...)return a end
function Class:__tostring()return ""end
function Class:implement(...)for a,b in pairs({...})do for c,d in pairs(b)do if self[c]==nil and type(d)=="function"then self[c]=d end end end end
function Class:is(a)local b=getmetatable(self)while b do if b==a then return true end;b=getmetatable(b)end;return false end
----------------------------------------
-- Simple Entity
----------------------------------------
Entity = Class:extend()
Entity.list = {}
function Entity:new()
Entity.super.new(self)
self.x=0
self.y=0
table.insert(Entity.list,self)
end
function Entity:think() end
function Entity:display() end
function Entity:__tostring()
return":"..self.x..","..self.y
end
function Entity:del()
table.remove(Entity.list,self)
end
----------------------------------------
-- Simple Particle
-- subclass of Entity
-- note we use Entity for anything drawn
----------------------------------------
Particle = Entity:extend()
function Particle:new(x,y,direction,speed)
Particle.super.new(self)
direction = direction or 0
speed = speed or 1
self.color =15.9
self.x = x or math.random(240)
self.y = y or math.random(136)
self.vx = math.sin(math.rad(direction))*speed
self.vy = math.cos(math.rad(direction))*speed
end
-- thinking
function Particle:think()
self.color = self.color - 0.12
if self.color//1==-1 then
self:del()
end
self.x = self.x + self.vx
self.y = self.y + self.vy
self.vy = self.vy + 0.1
if self.y>130 then
self.y = 130
self.vy = -self.vy
end
end
-- rendering
function Particle:display()
circ(self.x,self.y,2,self.color//1)
end
----------------------------------------
-- Simple "Hero"
-- subclass of Entity
----------------------------------------
Player = Entity:extend()
function Player:new()
Player.super.new(self)
self.x = 120
self.y = 68
end
-- use the buttons to move me around
function Player:think()
if btn(0) then self.y=self.y-1 end
if btn(1) then self.y=self.y+1 end
if btn(2) then self.x=self.x-1 end
if btn(3) then self.x=self.x+1 end
-- spawn some particles
for r=0,5 do
Particle(self.x+math.random(10)-5,
self.y+math.random(10)-5,
math.random(360),
math.random(4)/4.0)
end
end
-- draw the hero.
function Player:display()
circ(self.x,self.y,8,15)
end
hero = Player()
function TIC()
cls(0)
for index,ent in ipairs(Entity.list) do
ent:display()
ent:think()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment