Advertisement
AquaJD

Dodj

Sep 7th, 2017
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.48 KB | None | 0 0
  1. --[[
  2.  
  3.     Dodj
  4.     By Dave-ee Jones
  5.  
  6.     Dodgeball in CC!
  7.  
  8. --]]
  9.  
  10. local t_TERM = {} -- Terminal dimensions and positioning
  11. t_TERM.W, t_TERM.H = term.getSize()
  12.  
  13. local n_VERSION = "BETA"
  14.  
  15. --[[ CONFIGURATION ]]--
  16.  
  17. -- Ball update interval
  18. local n_BALL_UPDATE_INTERVAL = 1 / 30 -- seconds / frames (smaller ratio = faster balls)
  19. -- Player update interval
  20. local n_PLAYER_UPDATE_INTERVAL = 1 / 4 -- seconds / frames (smaller ratio = faster movement)
  21.  
  22. -- Player settings (keybindings, sprites, etc.)
  23. --- This is where you can add more players. Follow the same pattern as 1.
  24. local t_PLAYER_SETTINGS = {
  25.     [1] = {
  26.         LEFT = keys.a, -- Left key (press to move left)
  27.         UP = keys.w, -- Up key (press to move up)
  28.         RIGHT = keys.d, -- Right key (press to move right)
  29.         DOWN = keys.s, -- Down key (press to move down)
  30.         ACTION = keys.leftShift, -- Action key (press to pickup/throw/drop)
  31.         STOP = keys.z, -- Stop key (press while pressing a move key to not move)
  32.         SPRITE = "\1", -- Sprite (1 character)
  33.         TEAM = colors.cyan, -- Team, colour based. Defaults are blue and red.
  34.         X = 3, -- X position at the start of the game and when players are reset
  35.         Y = 3 -- Y position at the start of the game and when players are reset
  36.     },
  37.     [2] = {
  38.         LEFT = keys.left,
  39.         UP = keys.up,
  40.         RIGHT = keys.right,
  41.         DOWN = keys.down,
  42.         ACTION = keys.o,
  43.         STOP = keys.p,
  44.         SPRITE = "\2",
  45.         TEAM = colors.red,
  46.         X = t_TERM.W - 3,
  47.         Y = t_TERM.H - 3
  48.     }
  49. }
  50.  
  51. -- Default sprites for directions
  52. local t_SPRITES = {
  53.     LEFT = "\17",
  54.     UP = "\30",
  55.     RIGHT = "\16",
  56.     DOWN = "\31",
  57.     BALL = "\7"
  58. }
  59.  
  60. local c_MAIN_BG = colors.black -- Background colour
  61. local c_MAIN_FG = colors.white -- Foreground colour
  62. local c_BOARD_BG = colors.gray -- Board's background colour
  63.  
  64. --[[ END CONFIGURATION ]]--
  65.  
  66. -- Metatables
  67. local t_PLAYER = {}
  68. local t_BALL = {}
  69. t_PLAYER.__index = t_PLAYER
  70. t_BALL.__index = t_BALL
  71.  
  72. -- Tables
  73. local t_PLAYERS = {} -- Not to be confused with the metatable
  74. local t_BALLS = {} -- Also not to be confused with the metatable
  75. local t_BOARD = { -- t_BOARD dimensions and positioning, along with scoreboard
  76.     X = 1,
  77.     Y = 1,
  78.     W = t_TERM.W - 1,
  79.     H = t_TERM.H - 1,
  80.     XMID = t_TERM.W / 2 + 1,
  81.     TEAMS = {}
  82. }
  83.  
  84. -- Help
  85. local function f_HELP()
  86.     printError("----------")
  87.     printError("Dodj "..n_VERSION)
  88.     printError("----------")
  89.     printError("Setting up a player:")
  90.     printError("- Edit Dodj")
  91.     printError("- Scroll down to the 't_PLAYER_SETTINGS' table")
  92.     printError("- Make an entry for each player missing")
  93.     error("",0)
  94. end
  95.  
  96. -- Draw all the players
  97. local function f_PLAYERS_DRAW()
  98.     for i=1,#t_PLAYERS do
  99.         t_PLAYERS[i]:draw()
  100.     end
  101. end
  102.  
  103. -- Draw all the balls
  104. local function f_BALLS_DRAW()
  105.     for i=1,#t_BALLS do
  106.         t_BALLS[i]:draw()
  107.     end
  108. end
  109.  
  110. -- Create the teams
  111. local function f_TEAMS_CREATE()
  112.     -- Create a team based on the players' colours
  113.     local n_TEAMS = 0
  114.     for i=1,#t_PLAYERS do
  115.         if not t_BOARD.TEAMS[t_PLAYERS[i].TEAM] then
  116.             -- Team doesn't exist, so let's create it
  117.             t_BOARD.TEAMS[t_PLAYERS[i].TEAM] = {
  118.                 SCORE = 0,
  119.                 X = 0
  120.             }
  121.             n_TEAMS = n_TEAMS + 1
  122.         end
  123.     end
  124.     local n_INDEX = 1
  125.     if n_TEAMS == 2 then
  126.         -- If there's only 2 teams, draw the text on either side of the board
  127.         for k,v in pairs(t_BOARD.TEAMS) do
  128.             if n_INDEX == 1 then
  129.                 t_BOARD.TEAMS[k].X = 2
  130.             else
  131.                 t_BOARD.TEAMS[k].X = t_TERM.W - 2
  132.             end
  133.             n_INDEX = n_INDEX + 1
  134.         end
  135.     else
  136.         -- Calculate distance between written text to calculate where we end up drawing them
  137.         local n_XSTART = 2
  138.         local n_DISTANCE_BETWEEN_TEAMS = (t_TERM.W - (n_XSTART*2))/(n_TEAMS-1)
  139.         for k,v in pairs(t_BOARD.TEAMS) do
  140.             t_BOARD.TEAMS[k].X = n_XSTART+n_DISTANCE_BETWEEN_TEAMS*(n_INDEX-1)
  141.             n_INDEX = n_INDEX + 1
  142.         end
  143.     end
  144. end
  145.  
  146. -- Add one to a team's score
  147. local function f_TEAMS_SCORE(c_TEAM)
  148.     t_BOARD.TEAMS[c_TEAM].SCORE = t_BOARD.TEAMS[c_TEAM].SCORE + 1
  149. end
  150.  
  151. -- Draw the teams' score
  152. local function f_TEAMS_DRAW()
  153.     term.setBackgroundColor(c_BOARD_BG)
  154.     for k,v in pairs(t_BOARD.TEAMS) do
  155.         term.setTextColor(k)
  156.         term.setCursorPos(t_BOARD.TEAMS[k].X,1)
  157.         write(t_BOARD.TEAMS[k].SCORE)
  158.     end
  159. end
  160.  
  161. -- Draw the board
  162. local function f_BOARD_DRAW()
  163.     paintutils.drawBox(t_BOARD.X,t_BOARD.Y,t_BOARD.X+t_BOARD.W,t_BOARD.Y+t_BOARD.H,c_BOARD_BG)
  164.     f_TEAMS_DRAW()
  165. end
  166.  
  167. -- Reset the board
  168. local function f_BOARD_RESET()
  169.     f_BOARD_DRAW()
  170.     -- Reset the players
  171.     for i=1,#t_PLAYERS do
  172.         t_PLAYERS[i]:clear()
  173.         t_PLAYERS[i]:reset()
  174.     end
  175.     -- Reset the balls
  176.     for i=1,#t_BALLS do
  177.         t_BALLS[i]:clear()
  178.         t_BALLS[i]:reset()
  179.     end
  180.     f_BALLS_DRAW()
  181.     f_PLAYERS_DRAW()
  182.     tm_BALL_UPDATE = os.startTimer(1)
  183. end
  184.  
  185. -- Create a new player
  186. function t_PLAYER:new(n_NUMBER)
  187.     if not t_PLAYER_SETTINGS[n_NUMBER] then
  188.         -- No settings for the player found
  189.         printError("[ERROR] No settings for player "..n_NUMBER)
  190.         printError("Type '"..shell.getRunningProgram().." help' for help.")
  191.         error("",0)
  192.     end
  193.     local self = {
  194.         NUMBER = n_NUMBER,
  195.         DIRECTION = t_SPRITES.DOWN,
  196.         ITEM = nil,
  197.         ACTION_CD = false,
  198.         CATCHING = false,
  199.         STOPPED = false,
  200.         KEY_STATES = {
  201.             LEFT = false,
  202.             UP = false,
  203.             RIGHT = false,
  204.             DOWN = false,
  205.             ACTION = false,
  206.             STOP = false
  207.         }
  208.     }
  209.     -- This is where the t_PLAYER_SETTINGS table comes in handy!
  210.     self.LEFT = t_PLAYER_SETTINGS[n_NUMBER].LEFT
  211.     self.UP = t_PLAYER_SETTINGS[n_NUMBER].UP
  212.     self.RIGHT = t_PLAYER_SETTINGS[n_NUMBER].RIGHT
  213.     self.DOWN = t_PLAYER_SETTINGS[n_NUMBER].DOWN
  214.     self.ACTION = t_PLAYER_SETTINGS[n_NUMBER].ACTION
  215.     self.STOP = t_PLAYER_SETTINGS[n_NUMBER].STOP
  216.     self.X = t_PLAYER_SETTINGS[n_NUMBER].X
  217.     self.Y = t_PLAYER_SETTINGS[n_NUMBER].Y
  218.     self.TEAM = t_PLAYER_SETTINGS[n_NUMBER].TEAM
  219.     self.SPRITE = t_PLAYER_SETTINGS[n_NUMBER].SPRITE
  220.     self.XSTART = self.X
  221.     self.YSTART = self.Y
  222.     setmetatable(self,t_PLAYER)
  223.     return self
  224. end
  225.  
  226. -- Move a player left
  227. function t_PLAYER:moveLeft()
  228.     self.DIRECTION = t_SPRITES.LEFT
  229.     if not self.KEY_STATES.STOP and self.X > (t_BOARD.X + 1) then
  230.         self:clear()
  231.         self.X = self.X - 1
  232.         if self.ITEM then
  233.             self.ITEM:clear()
  234.             self.ITEM.X = self.X
  235.             self.ITEM.Y = self.Y + 1
  236.         end
  237.         f_BALLS_DRAW()
  238.         f_PLAYERS_DRAW()
  239.     end
  240.     self:draw()
  241. end
  242.  
  243. -- Move a player up
  244. function t_PLAYER:moveUp()
  245.     self.DIRECTION = t_SPRITES.UP
  246.     if not self.KEY_STATES.STOP and self.Y > (t_BOARD.Y + 1) then
  247.         self:clear()
  248.         self.Y = self.Y - 1
  249.         if self.ITEM then
  250.             self.ITEM:clear()
  251.             self.ITEM.X = self.X
  252.             self.ITEM.Y = self.Y + 1
  253.         end
  254.         f_BALLS_DRAW()
  255.         f_PLAYERS_DRAW()
  256.     end
  257.     self:draw()
  258. end
  259.  
  260. -- Move a player right
  261. function t_PLAYER:moveRight()
  262.     self.DIRECTION = t_SPRITES.RIGHT
  263.     if not self.KEY_STATES.STOP and self.X < (t_BOARD.X + t_BOARD.W - 1) then
  264.         self:clear()
  265.         self.X = self.X + 1
  266.         if self.ITEM then
  267.             self.ITEM:clear()
  268.             self.ITEM.X = self.X
  269.             self.ITEM.Y = self.Y + 1
  270.         end
  271.         f_BALLS_DRAW()
  272.         f_PLAYERS_DRAW()
  273.     end
  274.     self:draw()
  275. end
  276.  
  277. -- Move a player down
  278. function t_PLAYER:moveDown()
  279.     self.DIRECTION = t_SPRITES.DOWN
  280.     if not self.KEY_STATES.STOP and self.Y < (t_BOARD.Y + t_BOARD.H - 2) then
  281.         self:clear()
  282.         self.Y = self.Y + 1
  283.         if self.ITEM then
  284.             self.ITEM:clear()
  285.             self.ITEM.X = self.X
  286.             self.ITEM.Y = self.Y + 1
  287.         end
  288.         f_BALLS_DRAW()
  289.         f_PLAYERS_DRAW()
  290.     end
  291.     self:draw()
  292. end
  293.  
  294. -- Reset a player
  295. function t_PLAYER:reset()
  296.     self.X = self.XSTART
  297.     self.Y = self.YSTART
  298.     self.STOPPED = false
  299.     self.ACTION_CD = false
  300.     self.ITEM = nil
  301.     self.DIRECTION = t_SPRITES.DOWN
  302.     self.CATCHING = false
  303. end
  304.  
  305. -- Clear a player's drawn sprite
  306. function t_PLAYER:clear()
  307.     paintutils.drawPixel(self.X,self.Y,c_MAIN_BG)
  308.     paintutils.drawPixel(self.X,self.Y+1,c_MAIN_BG)
  309. end
  310.  
  311. -- Pickup/Throw/Drop something that a player holds
  312. function t_PLAYER:action()
  313.     if self.ITEM then
  314.         -- Player is holding an item and wants to be rid of it
  315.         if self.ITEM.SPRITE == t_SPRITES.BALL then
  316.             -- It's a ball! THROW IT!
  317.             self.ITEM.DIRECTION = self.DIRECTION
  318.             self.ITEM.IS_THROWN = true
  319.             self.ITEM.IS_CAUGHT = false
  320.             self.ITEM = nil
  321.         elseif self.ITEM.SPRITE == t_SPRITES.BLOCK then
  322.             -- It's a block! (of some kind..) DROP IT!
  323.             self.ITEM.IS_HELD = false
  324.         end
  325.     else
  326.         self.CATCHING = true
  327.     end
  328. end
  329.  
  330. -- Draw a player
  331. function t_PLAYER:draw()
  332.     -- Draw player's sprite
  333.     term.setTextColor(c_MAIN_FG)
  334.     term.setBackgroundColor(self.TEAM)
  335.     term.setCursorPos(self.X,self.Y)
  336.     write(self.SPRITE)
  337.     -- Draw player's direction/item
  338.     term.setTextColor(c_MAIN_FG)
  339.     term.setBackgroundColor(self.TEAM)
  340.     term.setCursorPos(self.X,self.Y+1)
  341.     if not self.ITEM then
  342.         write(self.DIRECTION)
  343.     else
  344.         write(self.ITEM.SPRITE)
  345.     end
  346. end
  347.  
  348. -- Create a new ball
  349. function t_BALL:new(n_NUMBER)
  350.     local self = {
  351.         NUMBER = n_NUMBER,
  352.         DIRECTION = t_SPRITES.DOWN,
  353.         IS_THROWN = false,
  354.         IS_CAUGHT = false,
  355.         TEAM = c_MAIN_FG,
  356.         SPRITE = t_SPRITES.BALL
  357.     }
  358.     local _n_X = 0
  359.     local _n_Y = 0
  360.     local b_LOOKING = true
  361.     while b_LOOKING do
  362.         -- Looking for a spot to put the ball..
  363.         _n_X = math.random(2,t_TERM.W-1)
  364.         _n_Y = math.random(2,t_TERM.H-1)
  365.         local _collision = false
  366.         for i=1,#t_PLAYERS do
  367.             if t_PLAYERS[i].XSTART == _n_X and t_PLAYERS[i].YSTART == _n_Y then
  368.                 -- Ah, can't use this position because it's the same as this player's starting position
  369.                 _collision = true
  370.             end
  371.         end
  372.         for i=1,#t_BALLS do
  373.             if t_BALLS[i].XSTART == _n_X and t_BALLS[i].YSTART == _n_Y then
  374.                 -- Ah, already a ball there
  375.                 _collision = true
  376.             end
  377.         end
  378.         if not _collision then
  379.             -- Okay, we found our spot
  380.             b_LOOKING = false
  381.         end
  382.     end
  383.     self.X = _n_X
  384.     self.Y = _n_Y
  385.     self.XSTART = _n_X
  386.     self.YSTART = _n_Y
  387.     setmetatable(self,t_BALL)
  388.     return self
  389. end
  390.  
  391. -- Calculate if the ball is touching a player
  392. function t_BALL:collidedWithPlayer()
  393.     for i=1,#t_PLAYERS do
  394.         -- Okay, Player i, are you touching the ball?
  395.         if self.X == t_PLAYERS[i].X and self.Y >= t_PLAYERS[i].Y and self.Y <= (t_PLAYERS[i].Y + 1) then
  396.             -- Oh boi, you are!
  397.             return t_PLAYERS[i]
  398.         end
  399.     end
  400.     -- No one touching the ball!
  401.     return false
  402. end
  403.  
  404. -- Move a ball
  405. function t_BALL:move()
  406.     if self.IS_THROWN then
  407.         -- Ahh, the ball is supposed to be moving
  408.         if self.DIRECTION == t_SPRITES.LEFT then
  409.             -- Moving left, eh?
  410.             self:clear()
  411.             if self.X > (t_BOARD.X + 1) then
  412.                 -- Okay, no wall, so let's keep going!
  413.                 self.X = self.X - 1
  414.             else
  415.                 -- Oops, hit a wall, so let's rebound!
  416.                 self.DIRECTION = t_SPRITES.RIGHT
  417.             end
  418.         elseif self.DIRECTION == t_SPRITES.UP then
  419.             -- Moving up in the world
  420.             self:clear()
  421.             if self.Y > (t_BOARD.Y + 1) then
  422.                 -- Okay, no ceiling, so let's keep moving up
  423.                 self.Y = self.Y - 1
  424.             else
  425.                 -- Okay, hit the ceiling, let's fall
  426.                 self.DIRECTION = t_SPRITES.DOWN
  427.             end
  428.         elseif self.DIRECTION == t_SPRITES.RIGHT then
  429.             -- Moving along the right path
  430.             self:clear()
  431.             if self.X < (t_BOARD.X + t_BOARD.W - 1) then
  432.                 -- Okay, it really is the right path
  433.                 self.X = self.X + 1
  434.             else
  435.                 -- It's all gone left (including this ball)
  436.                 self.DIRECTION = t_SPRITES.LEFT
  437.             end
  438.         elseif self.DIRECTION == t_SPRITES.DOWN then
  439.             -- Falling
  440.             self:clear()
  441.             if self.Y < (t_BOARD.Y + t_BOARD.H - 1) then
  442.                 -- We haven't hit the ground
  443.                 self.Y = self.Y + 1
  444.             else
  445.                 -- Balls are bouncy, so they rebound off the ground
  446.                 self.DIRECTION = t_SPRITES.UP
  447.             end
  448.         end
  449.         f_BALLS_DRAW()
  450.         f_PLAYERS_DRAW()
  451.     end
  452.     if not self.IS_CAUGHT then
  453.         local _other = self:collidedWithPlayer()
  454.         if _other then
  455.             -- Ball is sitting on a player
  456.             if self.TEAM == c_MAIN_FG and _other.CATCHING then
  457.                 -- Player is eligible to catch the ball, so they do
  458.                 _other.ITEM = self
  459.                 self.TEAM = _other.TEAM
  460.                 self.IS_CAUGHT = true
  461.                 self.IS_THROWN = false
  462.                 _other:draw()
  463.             elseif _other.CATCHING then
  464.                 -- Ball isn't white, but the player wants to catch the ball
  465.                 if (self.DIRECTION == t_SPRITES.DOWN and _other.DIRECTION == t_SPRITES.UP) or (self.DIRECTION == t_SPRITES.UP and _other.DIRECTION == t_SPRITES.DOWN) then
  466.                     -- Player is eligible to catch the ball, so they do
  467.                     _other.ITEM = self
  468.                     self.TEAM = _other.TEAM
  469.                     self.IS_CAUGHT = true
  470.                     self.IS_THROWN = false
  471.                     _other:draw()
  472.                 elseif (self.DIRECTION == t_SPRITES.LEFT and _other.DIRECTION == t_SPRITES.RIGHT) or (self.DIRECTION == t_SPRITES.RIGHT and _other.DIRECTION == t_SPRITES.LEFT) then
  473.                     -- Player is eligible to catch the ball, so they do
  474.                     _other.ITEM = self
  475.                     self.TEAM = _other.TEAM
  476.                     self.IS_CAUGHT = true
  477.                     self.IS_THROWN = false
  478.                     _other:draw()
  479.                 else
  480.                     -- Player isn't in any position to catch the ball, even though he tried..
  481.                     if self.TEAM ~= _other.TEAM and self.TEAM ~= c_MAIN_FG then
  482.                         -- Player just got hit!
  483.                         f_TEAMS_SCORE(self.TEAM)
  484.                         f_BOARD_RESET()
  485.                     end
  486.                 end
  487.             else
  488.                 -- Player isn't trying to catch it and the ball isn't white
  489.                 if self.TEAM ~= _other.TEAM and self.TEAM ~= c_MAIN_FG then
  490.                     -- AND the ball isn't on the same team as the player, NOT GOOD!
  491.                     f_TEAMS_SCORE(self.TEAM)
  492.                     f_BOARD_RESET()
  493.                 end
  494.             end
  495.         end
  496.     end
  497. end
  498.  
  499. -- Reset a ball
  500. function t_BALL:reset()
  501.     self.X = self.XSTART
  502.     self.Y = self.YSTART
  503.     self.IS_THROWN = false
  504.     self.IS_CAUGHT = false
  505.     self.DIRECTION = t_SPRITES.DOWN
  506.     self.TEAM = c_MAIN_FG
  507. end
  508.  
  509. -- Clear a ball's drawn sprite
  510. function t_BALL:clear()
  511.     paintutils.drawPixel(self.X,self.Y,c_MAIN_BG)
  512. end
  513.  
  514. -- Draw a ball
  515. function t_BALL:draw()
  516.     term.setTextColor(self.TEAM)
  517.     term.setBackgroundColor(c_MAIN_BG)
  518.     term.setCursorPos(self.X,self.Y)
  519.     write(t_SPRITES.BALL)
  520. end
  521.  
  522. -- Creating players based on arguments
  523. local tArgs = { ... }
  524. if tArgs[1] then
  525.     if not tonumber(tArgs[1]) then
  526.         -- Argument isn't a number, therefore we check for keywords
  527.         if tArgs[1] == "help" then
  528.             -- Argument was "help"
  529.             f_HELP()
  530.         else
  531.             -- Unknown argument, error out
  532.             error("[ERROR] Invalid argument: "..tArgs[1],0)
  533.         end
  534.     end
  535.     -- Arguments found, let's create some players
  536.     for i=1,tArgs[1] do
  537.         t_PLAYERS[i] = t_PLAYER:new(i)
  538.     end
  539.     -- Lets create twice the amount of balls (max 10)
  540.     for i=1,(tArgs[1]*2) do
  541.         if #t_BALLS ~= 10 then
  542.             t_BALLS[i] = t_BALL:new(i)
  543.         else
  544.             break
  545.         end
  546.     end
  547. else
  548.     -- No arguments found, default to 2 players
  549.     for i=1,2 do
  550.         t_PLAYERS[i] = t_PLAYER:new(i)
  551.     end
  552.     -- 2 players = 4 balls
  553.     for i=1,4 do
  554.         t_BALLS[i] = t_BALL:new(i)
  555.     end
  556. end
  557.  
  558. -- Setup the teams
  559. f_TEAMS_CREATE()
  560.  
  561. -- Back to the drawing board..
  562. local b_RUNNING = true
  563. term.setBackgroundColor(c_MAIN_BG)
  564. term.clear()
  565. f_BOARD_DRAW()
  566.  
  567. -- Draw all the things before we start looping
  568. f_BALLS_DRAW()
  569. f_PLAYERS_DRAW()
  570.  
  571. -- Starts the timers
  572. local tm_BALL_UPDATE = os.startTimer(1)
  573. local tm_PLAYER_UPDATE = os.startTimer(1)
  574.  
  575. -- The Frightening Loop
  576. while b_RUNNING do
  577.     local e_EVENT, e_BUTTON, e_X, e_Y = os.pullEvent()
  578.     if e_EVENT == "key" then
  579.         -- WHICH PLAYER PRESSED A BUTTON, HUH?!
  580.         for i=1,#t_PLAYERS do
  581.             -- WAS IT YOU, i?!
  582.             if e_BUTTON == t_PLAYERS[i].LEFT then
  583.                 -- AHA, NOW YOU CAN MOVE LEFT FOR WHAT YOU DID!
  584.                 t_PLAYERS[i]:moveLeft()
  585.                 t_PLAYERS[i].KEY_STATES.LEFT = true
  586.             elseif e_BUTTON == t_PLAYERS[i].UP then
  587.                 -- AHA, NOW YOU CAN MOVE UP FOR WHAT YOU DID!
  588.                 t_PLAYERS[i]:moveUp()
  589.                 t_PLAYERS[i].KEY_STATES.UP = true
  590.             elseif e_BUTTON == t_PLAYERS[i].RIGHT then
  591.                 -- AHA, NOW YOU CAN MOVE RIGHT FOR WHAT YOU DID!
  592.                 t_PLAYERS[i]:moveRight()
  593.                 t_PLAYERS[i].KEY_STATES.RIGHT = true
  594.             elseif e_BUTTON == t_PLAYERS[i].DOWN then
  595.                 -- AHA, NOW YOU CAN MOVE DOWN FOR WHAT YOU DID!
  596.                 t_PLAYERS[i]:moveDown()
  597.                 t_PLAYERS[i].KEY_STATES.DOWN = true
  598.             elseif e_BUTTON == t_PLAYERS[i].STOP then
  599.                 -- AHH, YOU WANT TO STOP DO YOU? FINE.
  600.                 t_PLAYERS[i].KEY_STATES.STOP = true
  601.             elseif e_BUTTON == t_PLAYERS[i].ACTION then
  602.                 -- WHAT? YOU WANT TO DO SOMETHING? GO AHEAD.
  603.                 t_PLAYERS[i]:action()
  604.                 t_PLAYERS[i].KEY_STATES.ACTION = true
  605.             end
  606.         end
  607.     elseif e_EVENT == "key_up" then
  608.         -- WHICH PLAYER PRESSED A BUTTON, HUH?!
  609.         for i=1,#t_PLAYERS do
  610.             -- WAS IT YOU, i?!
  611.             if e_BUTTON == t_PLAYERS[i].LEFT then
  612.                 -- AHA, NOW YOU CAN STAY RIGHT THERE!
  613.                 t_PLAYERS[i].KEY_STATES.LEFT = false
  614.             elseif e_BUTTON == t_PLAYERS[i].UP then
  615.                 -- STOP UPPING THEN!
  616.                 t_PLAYERS[i].KEY_STATES.UP = false
  617.             elseif e_BUTTON == t_PLAYERS[i].RIGHT then
  618.                 -- AHA, THE RIGHT CHOICE!
  619.                 t_PLAYERS[i].KEY_STATES.RIGHT = false
  620.             elseif e_BUTTON == t_PLAYERS[i].DOWN then
  621.                 -- AHA, NOW YOU CAN STAY RIGHT THERE!
  622.                 t_PLAYERS[i].KEY_STATES.DOWN = false
  623.             elseif e_BUTTON == t_PLAYERS[i].STOP then
  624.                 -- AHH, YOU DON'T WANT TO STOP DO YOU? FINE.
  625.                 t_PLAYERS[i].KEY_STATES.STOP = false
  626.             elseif e_BUTTON == t_PLAYERS[i].ACTION then
  627.                 -- WHAT? YOU DON'T WANT TO DO SOMETHING?
  628.                 t_PLAYERS[i].CATCHING = false
  629.                 t_PLAYERS[i].KEY_STATES.ACTION = false
  630.             end
  631.         end
  632.     elseif e_EVENT == "timer" then
  633.         if e_BUTTON == tm_BALL_UPDATE then
  634.             -- BALLS ARE BEING MOVED, GET READY..
  635.             for i=1,#t_BALLS do
  636.                 -- WANNA MOVE, i? THIS IS YOUR CHANCE.
  637.                 t_BALLS[i]:move()
  638.             end
  639.             tm_BALL_UPDATE = os.startTimer(n_BALL_UPDATE_INTERVAL)
  640.         elseif e_BUTTON == tm_PLAYER_UPDATE then
  641.             -- PLAYERS ARE BEING MOVED, GET READY..
  642.             for i=1,#t_PLAYERS do
  643.                 if t_PLAYERS[i].KEY_STATES.LEFT then
  644.                     -- WHEN YOU GONNA STOP HEADING LEFT?
  645.                     t_PLAYERS[i]:moveLeft()
  646.                 elseif t_PLAYERS[i].KEY_STATES.UP then
  647.                     -- MOVIN' ON UP!
  648.                     t_PLAYERS[i]:moveUp()
  649.                 elseif t_PLAYERS[i].KEY_STATES.RIGHT then
  650.                     -- RIGHT, RIGHT!
  651.                     t_PLAYERS[i]:moveRight()
  652.                 elseif t_PLAYERS[i].KEY_STATES.DOWN then
  653.                     -- DOWN WE GO!
  654.                     t_PLAYERS[i]:moveDown()
  655.                 end
  656.                 tm_PLAYER_UPDATE = os.startTimer(n_PLAYER_UPDATE_INTERVAL)
  657.             end
  658.         end
  659.     end
  660. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement