Guest User

Untitled

a guest
Nov 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.36 KB | None | 0 0
  1. Robot = class()
  2.     
  3. function Robot:init(x, y)
  4.     -- position and bounds
  5.     self.penStart = false
  6.     self.penStatus = false
  7.     self.pen = {}
  8.     self.x = x
  9.     self.y = y
  10.     self.oldX = x
  11.     self.oldY = y
  12.     self.bounds = Frame(0,0,0,0)
  13.     self.direction = 0
  14.     self.fromDir = 0
  15.     self.toDir = 0
  16.     
  17.     -- code related values
  18.     self.program = {}
  19.     for i = 1, 30 do self.program[i] = nil end
  20.     self.step = 1
  21.     self.aEntry = 1
  22.     self.bEntry = 1
  23.     
  24.     -- sensor values
  25.     self.bump = false
  26.     self.radar = false
  27.     self.treadTick = 0
  28.     self.fireLaser = false
  29.     self.fireRadar = false
  30.     self.delay = 0
  31.     
  32.     -- tourney-telated values
  33.     self.hp = 10
  34.     self.damage = self.hp
  35.     self.wins = 0
  36.     self.losses = 0
  37.     self.heat = 0
  38.     self.repeatCounter = 1
  39.     self.points = 0
  40.     self.redirectValue = 0
  41.     self.rf = Frame(0,0,0,0)
  42.     self.limits = Frame(0,0,0,0)
  43.     
  44.     -- design-related values
  45.     self.name = "Generobot"
  46.     self.treadColor = 14
  47.     self.bodyColor = 14
  48.     self.headColor = 15
  49.     self.dishColor = 4
  50.     self.head = 1
  51.     self.dish = 1
  52.     self.body = 2
  53.     self.tread = 2
  54.     self.range = 20
  55.     self.speed = 5
  56.     self.turnDelay = 5
  57.     self.turnTarget = 1
  58.     self.flameTimer = 0
  59.     self.shield = 0
  60.     self.repairRate = 0
  61.  
  62.     self.img = image(60, 60)
  63.     
  64. end
  65.  
  66. function Robot:createImage()
  67.     -- create an image of the bot for use in menus, etc
  68.     pushMatrix()
  69.     strokeWidth(2)
  70.     translate(30,30)
  71.     setContext(self.img)
  72.     self:drawBase()
  73.     setContext()
  74.     popMatrix()
  75. end
  76.  
  77. function Robot:redirect(v)
  78.     -- set correct step number depending on board
  79.     if self.step > 60 then
  80.         return v + 60
  81.     elseif self.step > 30 then
  82.         return v + 30
  83.     else
  84.         return v
  85.     end
  86. end
  87.  
  88. function Robot:createDetectionFrame(dir)
  89.     -- used by radar and laser
  90.     if dir == 1 then
  91.         -- up
  92.         self.rf = Frame(self.x + 24, self.y + 36, 
  93.         self.x + 36, self.range *  60)
  94.         if self.rf.top > self.limits:height() then
  95.             self.rf.top = self.limits:height()
  96.         end
  97.     elseif dir == 2 then
  98.         -- right
  99.         self.rf = Frame(self.x + 33, self.y + 24, 
  100.         self.x + self.range *  60, self.y + 36)
  101.         if self.rf.right > self.limits:width() then
  102.             self.rf.right = self.limits:width()
  103.         end
  104.     elseif dir == 3 then
  105.         -- down
  106.         self.rf = Frame(self.x + 24, self.y - self.range *  60, 
  107.         self.x + 36, self.y + 33)
  108.         if self.rf.bottom < 0 then
  109.             self.rf.bottom = 0
  110.         end
  111.     elseif dir == 4 then
  112.         -- left
  113.         self.rf = Frame(self.x - self.range *  60, self.y + 24, 
  114.         self.x + 33, self.y + 36)
  115.         if self.rf.left < 0 then
  116.             self.rf.left = 0
  117.         end
  118.     end
  119. end
  120.  
  121. function Robot:drawLaser()
  122.     -- create the rectangle for laser fire
  123.     if self.rf == nil then 
  124.         self:createDetectionFrame(self.direction)
  125.     end
  126.     noStroke()
  127.     fill(212, 60, 55, 92)
  128.     self.rf:draw()
  129.     sound(SOUND_SHOOT, 9773)
  130. end
  131.  
  132. function Robot:drawRadar(dir)
  133.     -- create the rectangle for radar detection
  134.     self:createDetectionFrame(dir)
  135.     --noStroke()
  136.     --fill(81, 85, 175, 50)
  137.     --self.rf:draw()
  138.     --sound(SOUND_JUMP, 9772)
  139. end
  140.  
  141. function Robot:run()
  142.     local p, rf, redirected
  143.     --displayMode(STANDARD)
  144.     if self.delay > 0 then
  145.         self.delay = self.delay - 1
  146.         return true
  147.     end
  148.     self.hp = self.hp + self.repairRate / 100
  149.     if self.hp > 10 then self.hp = 10 end
  150.     if self.radar then 
  151.         self.step = self.redirectValue
  152.         --sound(SOUND_HIT, 17131)
  153.         self.radar = false
  154.     end
  155.     -- get executable token
  156.     p = self.program[self.step]
  157.     redirected = false
  158.     if p == nil or p.code == nil then
  159.         if self.step > 60 then 
  160.             self.step = self.bEntry + 1
  161.         elseif self.step > 30 then
  162.             self.step = self.aEntry + 1
  163.         else   
  164.             self.step = 1
  165.         end
  166.         redirected = true
  167.         return false
  168.     end
  169.     -- execute control
  170.     if p.code.short == "G" then
  171.         -- Goto
  172.         self.step = self:redirect(p.value)
  173.         redirected = true
  174.     elseif p.code.short == "5" then
  175.         -- 50/50 random
  176.         if math.random(2) > 1 then
  177.             self.step = self:redirect(p.value)
  178.             redirected = true   
  179.         end 
  180.     elseif p.code.short == "P" then
  181.         -- repeat
  182.         if self.repeatCounter < p.value then 
  183.             self.repeatCounter = self.repeatCounter + 1
  184.             self.step = self.step - 1
  185.             redirected = true   
  186.         else
  187.             self.repeatCounter = 1
  188.         end 
  189.     -- check sensors
  190.     elseif p.code.short == "D" then
  191.         -- damage sensor
  192.         if self.hp < self. damage then
  193.             self.damage = self.hp
  194.             self.step = self:redirect(p.value)
  195.             redirected = true
  196.         end
  197.     elseif p.code.short == "H" then
  198.         -- bump sensor
  199.         if self.bump then 
  200.             self.step = self:redirect(p.value)
  201.             sound(SOUND_HIT, 17131)
  202.             redirected = true
  203.         end
  204.         self.bump = false
  205.     elseif p.code.short == "A" then
  206.         -- radar sensor
  207.         self:drawRadar(self.direction)
  208.         self.delay = 5
  209.         self.fireRadar = true
  210.         self.redirectValue = self:redirect(p.value)
  211.     elseif p.code.short == "I" then
  212.         -- radar sensor right
  213.         i = self.direction + 1
  214.         if i > 4 then i = 1 end
  215.         self:drawRadar(i)
  216.         self.delay = 5
  217.         self.fireRadar = true
  218.         self.redirectValue = self:redirect(p.value)
  219.     elseif p.code.short == "T" then
  220.         -- radar sensor left
  221.         i = self.direction - 1
  222.         if i < 1 then i = 4 end
  223.         self:drawRadar(i)
  224.         self.delay = 5
  225.         self.fireRadar = true
  226.         self.redirectValue = self:redirect(p.value)
  227.     -- take action
  228.     elseif p.code.short == "F" then
  229.         -- Forward
  230.         self.treadTick = self.treadTick + 1
  231.         if self.treadTick > 9 then
  232.             self.treadTick = 0
  233.         end
  234.         if not self.bump then
  235.             self.oldX = self.x
  236.             self.oldY = self.y
  237.             if self.direction == 1 then
  238.                 self.y = self.y + self.speed
  239.             elseif self.direction == 2 then
  240.                 self.x = self.x + self.speed
  241.             elseif self.direction == 3 then
  242.                 self.y = self.y - self.speed
  243.             elseif self.direction == 4 then
  244.                 self.x = self.x - self.speed
  245.             end
  246.             if self.penStart then
  247.                 self.pen[#self.pen + 1] = 
  248.                 PenPoint(self.x + 30, self.y + 30, self.penStatus)
  249.             end
  250.         end
  251.     elseif p.code.short == "B" then
  252.         -- Reverse
  253.         self.treadTick = self.treadTick - 1
  254.         if self.treadTick < 0 then
  255.             self.treadTick = 9
  256.         end
  257.         if not self.bump then
  258.             self.oldX = self.x
  259.             self.oldY = self.y
  260.             if self.direction == 3 then
  261.                 self.y = self.y + self.speed
  262.             elseif self.direction == 4 then
  263.                 self.x = self.x + self.speed
  264.             elseif self.direction == 1 then
  265.                 self.y = self.y - self.speed
  266.             elseif self.direction == 2 then
  267.                 self.x = self.x - self.speed
  268.             end
  269.             if self.penStart then
  270.                 self.pen[#self.pen + 1] = 
  271.                 PenPoint(self.x + 30, self.y + 30, self.penStatus)
  272.             end
  273.         end
  274.     elseif p.code.short == "L" then
  275.         self.direction = self.direction - 1
  276.         if self.direction == 0 then self.direction = 4 end
  277.         self.delay = self.delay + self.turnDelay
  278.     elseif p.code.short == "R" then
  279.         self.direction = self.direction + 1
  280.         if self.direction == 5 then self.direction = 1 end
  281.         self.delay = self.delay + self.turnDelay
  282.     elseif p.code.short == "W" then
  283.         -- fire laser
  284.         self.delay = self.dish * 15
  285.         self.fireLaser = true
  286.         self:drawLaser()
  287.     elseif p.code.short == "1" then
  288.         -- Daughterboard A
  289.         if self.step < 31 then
  290.             self.aEntry = self.step
  291.         end
  292.         self.step = 31
  293.         redirected = true
  294.     elseif p.code.short == "2" then
  295.         -- Daughterboard B
  296.         if self.step < 61 then 
  297.             self.bEntry = self.step
  298.         end
  299.         self.step = 61
  300.         redirected = true
  301.     elseif p.code.short == "S" then
  302.         -- Sound Horn
  303.         sound(SOUND_RANDOM, 19024)
  304.     elseif p.code.short == "v" then
  305.         -- pen down
  306.         self.penStart = true
  307.         self.penStatus = true
  308.     elseif p.code.short == "^" then
  309.         -- pen up
  310.         self.penStart = true
  311.         self.penStatus = false
  312.     end
  313.     -- step forward
  314.     if not redirected then
  315.         self.step = self.step + 1
  316.     end
  317.     self.treadTick = self.treadTick + 1
  318.     if self.treadTick > 9 then
  319.         self.treadTick = 0
  320.     end
  321. end
  322.  
  323. function Robot:draw(size)
  324.     local i
  325.     -- draw
  326.     pushMatrix()
  327.     translate(self.x + 30, self.y + 30)
  328.     scale(size)
  329.     if self.direction == 2 then
  330.         rotate(270)
  331.     elseif self.direction == 3 then
  332.         rotate(180)
  333.     elseif self.direction == 4 then
  334.         rotate(90)
  335.     end 
  336.     strokeWidth(1)
  337.     self:drawBase()
  338.     popMatrix()
  339.     self.bounds.left = self.x
  340.     self.bounds.right = self.x + 60
  341.     self.bounds.top = self.y + 60
  342.     self.bounds.bottom = self.y
  343.     if self.heat > 0 then self.heat = self.heat - 0.1 end
  344. end
  345.  
  346. function Robot:drawDish(x, y)
  347.     c1 = colors[self.dishColor]
  348.     c2 = color(c1.r + self.heat * 22, c1.g, c1.b)
  349.     stroke(c2)
  350.     fill(backClr)
  351.     if self.dish == 1 then
  352.         line(x - 3, y, x, y + 15)
  353.         line(x + 3, y, x, y + 15)
  354.         line(x - 20, y + 7, x - 15, y + 3)
  355.         line(x - 15, y + 3, x, y)
  356.         line(x + 20, y + 7, x + 15, y + 3)
  357.         line(x + 15, y + 3, x, y)
  358.         line(x, y + 15, x, y + 20)
  359.         ellipse(x, y + 15, 3)
  360.         self.range = 20
  361.     end
  362.     if self.dish == 2 then
  363.         rect(x - 3, y, x + 3, y + 20)
  364.         line(x - 15, y, x, y)
  365.         line(x + 15, y, x, y)
  366.         line(x - 20, y + 10, x, y)
  367.         line(x + 20, y + 10, x, y)
  368.         self.range = 7
  369.     end
  370.     if self.dish == 3 then
  371.         line(x - 15, y, x + 15, y)
  372.         line(x - 10, y + 3, x + 10, y + 3)
  373.         line(x - 7, y + 7, x + 7, y + 7)
  374.         line(x - 3, y + 10, x + 3, y + 10)
  375.         line(x, y, x, y + 20)
  376.         ellipse(x, y + 20, 7)
  377.         self.range = 3
  378.     end
  379. end
  380.  
  381. function Robot:drawHead(x, y)
  382.     c1 = colors[self.headColor]
  383.     c2 = color(c1.r + self.heat * 22, c1.g, c1.b)
  384.     stroke(c2)
  385.     fill(backClr)
  386.     if self.head == 1 then
  387.         ellipse(x, y, 30)
  388.         ellipse(x, y, self.treadTick * 2)
  389.         rect(x - 15, y - 5, x - 15, y + 5)
  390.         rect(x + 15, y - 5, x + 15, y + 5)
  391.         line(x - 15, y, x - 20, y)
  392.         line(x + 15, y, x + 20, y)
  393.     end
  394.     if self.head == 2 then
  395.         ellipse(x, y, 30)
  396.         ellipse(x, y, 20)
  397.         rotate(self.treadTick * 9)
  398.         line(x+6,y+6,x-6,y-6)
  399.         line(x+6,y-6,x-6,y+6)
  400.         rotate(self.treadTick * -9)
  401.     end
  402.     if self.head == 3 then
  403.         ellipse(x, y, 30)
  404.         ellipse(x, y + 10, 20, 10)
  405.         ellipse(x, y + 10, 15, 5)
  406.         line(x - 15, y - 5, x - 20, y - 15) 
  407.         line(x + 15, y - 5, x + 20, y - 15)
  408.         fill(255, 0, 0, 255)
  409.         ellipse(x, y + 5, 20, self.treadTick * 2)
  410.     end
  411. end
  412.  
  413. function Robot:drawBody(x, y)
  414.     c1 = colors[self.bodyColor]
  415.     c2 = color(c1.r + self.heat * 22, c1.g, c1.b)
  416.     stroke(c2)
  417.     fill(backClr)
  418.     if self.body == 1 then
  419.         ellipse(x, y, 40, 20)
  420.         ellipse(x - 20, y, 15, 15)
  421.         ellipse(x + 20, y, 15, 15)
  422.     end
  423.     if self.body == 2 then
  424.         rect(x - 25, y - 10, x + 25, y + 10)
  425.         ellipse(x - 25, y, 20)
  426.         ellipse(x + 25, y, 20)
  427.         rect(x - 15, y - 15, x + 15, y - 10)
  428.     end
  429.     if self.body == 3 then
  430.         ellipse(x - 20, y, 30, 30)
  431.         ellipse(x + 20, y, 30, 30)
  432.         rect(x - 20, y - 15, x + 20, y + 15)
  433.         ellipse(x, y, 40, 30)
  434.     end
  435. end
  436.  
  437. function Robot:drawTreads(x, y)
  438.     local i
  439.     c1 = colors[self.treadColor]
  440.     c2 = color(c1.r + self.heat * 22, c1.g, c1.b)
  441.     stroke(c2)
  442.     fill(backClr)
  443.     if self.tread == 1 then
  444.         ellipse(20, -20, 20)
  445.         ellipse(20, -20, 20, 5 + self.treadTick)
  446.         ellipse(-20, -20, 20)
  447.         ellipse(-20, -20, 20, 5 + self.treadTick)
  448.         ellipse(-20, 20, 20)
  449.         ellipse(-20, 20, 20, 5 + self.treadTick)
  450.         ellipse(20, 20, 20)
  451.         ellipse(20, 20, 20, 5 + self.treadTick)
  452.         line(-20, -11, 0, 2)
  453.         line(-17, -14, 0, -2)
  454.         line(20, -11, 0, 2)
  455.         line(17, -14, 0, -2)
  456.         line(-20, 11, 0, 2)
  457.         line(-17, 14, 0, -2)
  458.         line(20, 11, 0, 2)
  459.         line(17, 14, 0, -2)
  460.     end
  461.     if self.tread == 2 then
  462.         rect(-30, -30, -15, 30)
  463.         rect(15, -30, 30, 30)
  464.         for i = 0,5 do
  465.             line(-30, i * 10 - 30 + self.treadTick, 
  466.                -15, i * 10 - 30 + self.treadTick)
  467.             line(15, i * 10 -30 + self.treadTick, 
  468.                30, i * 10 - 30 + self.treadTick)
  469.         end
  470.     end
  471.     if self.tread == 3 then
  472.         rect(-30, -30, -15, -15)
  473.         rect(-27, -33, -17, -12)
  474.         rect(15, -30, 30, -15)
  475.         rect(17, -33, 27, -12)
  476.         rect(-30, 15, -15, 30)
  477.         rect(-27, 12, -17, 33)
  478.         rect(15, 15, 30, 30)
  479.         rect(17, 12, 27, 33)
  480.         rect(-15, 20, 15, 25)
  481.         rect(-15, -20, 15, -25)
  482.         rect(-3, -10, 3, -20)
  483.         rect(-3, 10, 3, 20)
  484.         line(-27, self.treadTick - 30 + self.treadTick, 
  485.         -17, self.treadTick - 30 + self.treadTick)
  486.         line(17, self.treadTick - 30 + self.treadTick, 
  487.         27, self.treadTick - 30 + self.treadTick)
  488.         line(17, self.treadTick + 15 + self.treadTick, 
  489.         27, self.treadTick + 15 + self.treadTick)
  490.         line(-27, self.treadTick + 15 + self.treadTick, 
  491.         -17, self.treadTick + 15 + self.treadTick)
  492.     end
  493. end
  494.  
  495. function Robot:drawBase()
  496.     pushStyle()
  497.     self:drawTreads(0, 0)
  498.     self:drawBody(0, 0)
  499.     self:drawHead(0, 0)
  500.     self:drawDish(0, 10)
  501.     popStyle()
  502. end
Add Comment
Please, Sign In to add comment