game = {
add = function( x, y, w, h, image, name, actions, desc )
local actions = actions or { }
local desc = desc or { }
local t = { oldX = x, oldY = y, dimension = "default", x = x, y = y, w = w, h = h, image = ( type( image ) == "string" and game.loadImage( image ) or image ), actions = actions, desc = desc, name = name }
return setmetatable( t, { __index = game } )
end;
loadImage = function( path )
--loads the image in a specific format
local t = { }
local tImage = paintutils.loadImage( path )
for y=1,#tImage do
local tLine = tImage[y]
for x=1,#tLine do
if tLine[x] > 0 then
table.insert( t, { x = x, y = y, bc = tLine[x], text = " ", tc = 1 } )
end
end
end
local maxX = 0
local maxY = 0
for i = 1,#t do
if t[i].x > maxX then
maxX = t[i].x
end
if t[i].y > maxY then
maxY = t[i].y
end
end
local r = { }
for i = 1,maxY do
r[i] = { }
for k = 1,maxX do
r[i][k] = { text = false, bc = 2, tc = 2 }
end
end
for i = 1,#t do
r[t[i].y][t[i].x] = { text = t[i].text, bc = t[i].bc, tc = t[i].tc }
end
return r
end;
setDimension = function( self, dim )
--changes the entities dimension ( for collision )
self.dimension = dim
return self.dimension
end;
camera = { entity = { x = 1, y = 1, w = 51, h = 19 } };
lock = function( self )
game.camera.entity = self
end;
draw = function( self )
--draws the entity at the entities x and y
for y = 1,#self.image do
for x = 1,#self.image[y] do
im = self.image[y][x]
if im.text ~= false then
local xdiff = ( self.x - game.camera.entity.x ) + ( 26 - ( game.camera.entity.w / 2 ) )
local ydiff = ( self.y - game.camera.entity.y ) + ( 10 - ( game.camera.entity.h / 2 ) )
term.native.setCursorPos( x + xdiff, y + ydiff )
term.native.setBackgroundColour( im.bc )
term.native.setTextColour( im.tc )
term.native.write( im.text )
end
end
end
end;
isLockedEntity = function( self )
if game.camera.entity == self then
return true
end
return false
end;
loadAddons = function( path )
path = path or ""
local f = fs.list( path )
for i = 1,#f do
if fs.isDir( f[i] ) then
game.loadAddons( f[i] )
elseif string.sub( f[i], string.len( f[i] )-3, string.len( f[i] ) ) == ".gma" then
game.loadAddon( f[i] )
end
end
end;
move = function( self, x, y )
--changes the entities x and y to x+x and y+y
self.oldX, self.oldY = self.x, self.y
self.x = self.x + math.floor( x )
self.y = self.y + math.floor( y )
end;
teleport = function( self, x, y )
--sets the entities x and y to x and y
self.oldX, self.oldY = self.x, self.y
self.x, self.y = x, y
end;
pixel = function( self, x, y, text, bc, tc )
if not self.image[y][x] then return end
bc = bc or self.image[y][x].bc-- or colours.black
tc = tc or self.image[y][x].tc-- or colours.white
text = text or self.image[y][x].item-- or " "
self.image[y][x] = { text = text, bc = bc, tc = tc }
self:draw( )
end;
remove = function( self, x, y )
if not self.image[y][x] then return end
self.image[y][x] =
{ text = false, bc = colours.white, tc = colours.white }
end;
getOppositeEdge = function( self, y )
if y then
return self.y+( self.h-1 )
end
return self.x+( self.w-1 )
end;
loadAddon = function( path )
--will load addons
local loaded = true
parallel.waitForAny( function( ) shell.run( path, "game_API_is_adding_you" ) end, function( ) sleep( 1 ) loaded = false end )
if loaded and game.addon then
for k, v in pairs( game.addon ) do
game[k] = v
end
end
game.addon = nil
return loaded
end;
back = function( self )
self:teleport( self.oldX, self.oldY )
end;
collide = function( r1, r2 )
end;
isColliding = function( r1, r2, detail )
if r2 == "screen" then
if r1.x < 1 or
r1.y < 1 or
r1:getOppositeEdge( ) > 51 or
r1:getOppositeEdge( true ) > 19 then
return true
end
return false
end
if r1.dimension ~= r2.dimension then
return false, false
end
local left = false
local top = false
local l, r, t, b = nil, nil, nil, nil
if r1.x < r2.x then
l, r = r2.x, r1:getOppositeEdge( )
left = true
elseif r1.x >= r2.x then
l, r = r1.x, r2:getOppositeEdge( )
end
if r1.y < r2.y then
t, b = r2.y, r1:getOppositeEdge( true )
top = true
elseif r1.y >= r2.y then
t, b = r1.y, r2:getOppositeEdge( true )
end
if l > r or t > b then
return false, false
end
if not detail then game.collide( r1, r2 ) return true, false end
for x = l, r do
for y = t, b do
local r1x, r2x, r1y, r2y = nil, nil, nil, nil
r1x, r2x = x-r1.x+1, x-r2.x+1
r1y, r2y = y-r1.y+1, y-r2.y+1
if r1.image[r1y] and r1.image[r1y][r1x] and r1.image[r1y][r1x].text ~= false and
r2.image[r2y] and r2.image[r2y][r2x] and r2.image[r2y][r2x].text ~= false then
game.collide( r1, r2 )
return true, true
end
end
end
game.collide( r1, r2 )
return true, false
end;
}