tommyroyall

Untitled

Nov 29th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. function class(base, init)
  2.    local c = {}    -- a new class instance
  3.    if not init and type(base) == 'function' then
  4.       init = base
  5.       base = nil
  6.    elseif type(base) == 'table' then
  7.     -- our new class is a shallow copy of the base class!
  8.       for i,v in pairs(base) do
  9.          c[i] = v
  10.       end
  11.       c._base = base
  12.    end
  13.    -- the class will be the metatable for all its objects,
  14.    -- and they will look up their methods in it.
  15.    c.__index = c
  16.  
  17.    -- expose a constructor which can be called by <classname>(<args>)
  18.    local mt = {}
  19.    mt.__call = function(class_tbl, ...)
  20.    local obj = {}
  21.    setmetatable(obj,c)
  22.    if init then
  23.       init(obj,...)
  24.    else
  25.       -- make sure that any stuff from the base class is initialized!
  26.       if base and base.init then
  27.       base.init(obj, ...)
  28.       end
  29.    end
  30.    return obj
  31.    end
  32.    c.init = init
  33.    c.is_a = function(self, klass)
  34.       local m = getmetatable(self)
  35.       while m do
  36.          if m == klass then return true end
  37.          m = m._base
  38.       end
  39.       return false
  40.    end
  41.    setmetatable(c, mt)
  42.    return c
  43. end
  44.  
  45.  -- Example game.
  46.   -- By: Sledger721.
  47.   game={};
  48.   new={};
  49.   const={};
  50.   const.WIDTH,const.HEIGHT=50,18;
  51.  
  52. function clear()
  53.     term.clear()
  54.     term.setCursorPos(1,1)
  55. end
  56.  
  57. function new.world() -- World/level constructor.
  58.     self={}
  59.     self.objects={}
  60.     obj_counter=0
  61.    
  62.     function self:add_object(x,y) -- Add a new object.
  63.         obj_counter=obj_counter+1
  64.             self.objects[obj_counter].x,self.objects[obj_counter].y=x,y
  65.             return obj_counter
  66.     end
  67.     function self:move_object(ref,newx,newy) -- Move an object.
  68.         self.objects[ref].x=newx
  69.         self.objects[ref].y=newy
  70.     end
  71.     function self:check_collision(x,y) -- Check for collisions.
  72.         for i=0,#self.objects do
  73.             if self.objects[i].x==x or self.objects[i].y==y then -- If it senses a collision.
  74.                 return false
  75.             else
  76.                 return true -- Otherwise.
  77.             end
  78.         end
  79.     end
  80.    
  81.     return self;
  82. end
  83.  
  84. function new.player() -- The constructor for a new player.
  85.     self={} -- Establish the player's table.
  86.       self.body={}
  87.         self.body.x=1; -- And the player's stats.
  88.         self.body.y=1;
  89.         self.body.color=colors.red;
  90.    
  91.     function self:move_down(amount) -- Move the player up said amount. Amount must be an integer. No return.
  92.         check=world:check_collision(player.body.x,player.body.y+amount)
  93.         if check then
  94.             for i=1,tonumber(amount) do
  95.                 if player.body.y<const.HEIGHT then
  96.                     player.body.y=player.body.y+amount
  97.                     world:move_object(player.body,player.x,player.y)
  98.                 end
  99.             end
  100.         end
  101.     end
  102.     function self:move_up(amount) -- Move the player down said amount. Amount must be an integer. No return.
  103.         check=world:check_collision(player.x,player.y-amount)
  104.         if check then
  105.             for i=1,tonumber(amount) do
  106.                 if player.y>1 then
  107.                     player.y=player.y-amount
  108.                     world:move_object(player.body,player.x,player.y)
  109.                 end
  110.             end
  111.         end
  112.     end
  113.     function self:move_right(amount) -- Move the player right said amount. Amount must be an integer. No return.
  114.         check=world:check_collision(player.body.x+amount,player.body.y)
  115.         if check the
  116.             for i=1,tonumber(amount) do
  117.                 if player.x<const.WIDTH then
  118.                     player.body.x=player.body.x+amount
  119.                     world:move_object(player.body,player.x,player.y)
  120.                 end
  121.             end
  122.         end
  123.     end
  124.     function self:move_left(amount) -- Move the player left said amount. Amount must be an integer. No return.
  125.         check=world:check_collision(player.body.x,player.body.y+amount)
  126.         if check then
  127.             for i=1,tonumber(amount) do
  128.                 if player.body.x>1 then
  129.                     player.body.x=player.body.x-amount
  130.                     world:move_object(player.body,player.x,player.y)
  131.                 end
  132.             end
  133.         end
  134.     end
  135.     function self:change_color(newcolor) -- Change the color of the player. Amount must be an integer, but will most likely be a variable from the colors api. No return.
  136.         self.body.color=newcolor
  137.     end
  138.  
  139.     return self
  140. end
  141.  
  142. function create_objects() -- Create all of the objects for initiation and bind them to physics.
  143.     world=new.world()
  144.     player=new.player()
  145.     player=world:add_object(player.x,player.y)
  146. end
  147.  
  148. function game_init() -- The beginning function for the game.
  149.  term.setTextColor(colors.red)
  150.  term.setBackgroundColor(colors.black)
  151.   print('Welcome to Legion!!!')
  152.     sleep(2);
  153.   print('Beginning game.')
  154.     sleep(1);
  155.         game_state=1;
  156.         player=new.player()
  157.             game_loop() -- Call the main loop.
  158. end
  159.  
  160. function game_loop() -- The main loop for the game.
  161.     while game_state~=0 do
  162.  
  163.         -- Graphics loop.
  164.     GraphicsAPI.drawRect({1,1},{50,18},colors.black,true)
  165.     GraphicsAPI.drawPoint(player.body.x,player.body.y,player.body.color)
  166.     GraphicsAPI.update()
  167.  
  168.         -- Input loop.
  169.     ev,char=os.pullEvent('char')
  170.         if ev=='char' then
  171.             if char=='w' then
  172.                 player:move_up(1)
  173.             elseif char=='a' then
  174.                 player:move_left(1)
  175.             elseif char=='s' then
  176.                 player:move_down(1)
  177.             elseif char=='d' then
  178.                 player:move_right(1)
  179.             end
  180.         end
  181.  
  182.     end
  183. end
  184.  
  185. clear()
  186. game_init()
Advertisement
Add Comment
Please, Sign In to add comment