jesterxl

Untitled

Jul 1st, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.00 KB | None | 0 0
  1. WarBot = {}
  2.  
  3. function WarBot:new()
  4.  
  5.     if WarBot.spriteSheet == nil then
  6.         local scoutSheet = sprite.newSpriteSheet("scout-sheet.png", 64, 64)
  7.         local scoutSet = sprite.newSpriteSet(scoutSheet, 1, 10)
  8.         sprite.add(scoutSet, "scout", 1, 10, 1000, 0)
  9.  
  10.         local artillerySheet = sprite.newSpriteSheet("artillery-sheet.png", 84, 84)
  11.         local artillerySet = sprite.newSpriteSet(artillerySheet, 1, 17)
  12.         sprite.add(artillerySet, "artillery", 1, 17, 1000, 1)
  13.         sprite.add(artillerySet, "artilleryReverse", 1, 17, 500, -1)
  14.  
  15.         local defendSheet = sprite.newSpriteSheet("defend-sheet.png", 64, 64)
  16.         local defendSet = sprite.newSpriteSet(defendSheet, 1, 1)
  17.         sprite.add(defendSet, "defend", 1, 1, 1, 1)
  18.  
  19.         local sightSheet = sprite.newSpriteSheet("sight-sheet.png", 64, 64)
  20.         local sightSet = sprite.newSpriteSet(sightSheet, 1, 5)
  21.         sprite.add(sightSet, "sight", 1, 5, 500, 1)
  22.         sprite.add(sightSet, "sightReverse", 1, 5, 300, -1)
  23.  
  24.         local sniperSheet = sprite.newSpriteSheet("sniper-sheet.png", 128, 128)
  25.         local sniperSet = sprite.newSpriteSet(sniperSheet, 1, 21)
  26.         sprite.add(sniperSet, "sniper", 1, 21, 1000, 1)
  27.         sprite.add(sniperSet, "sniperReverse", 1, 21, 500, -1)
  28.  
  29.         WarBot.scoutSheet = scoutSheet
  30.         WarBot.scoutSet = scoutSet
  31.  
  32.         WarBot.artillerySheet = artillerySheet
  33.         WarBot.artillerySet = artillerySet
  34.  
  35.         WarBot.defendSheet = defendSheet
  36.         WarBot.defendSet = defendSet
  37.  
  38.         WarBot.sightSheet = sightSheet
  39.         WarBot.sightSet = sightSet
  40.  
  41.         WarBot.sniperSheet = sniperSheet
  42.         WarBot.sniperSet = sniperSet
  43.     end
  44.  
  45.     local bot = display.newGroup()
  46.     bot.WIDTH = 64
  47.     bot.HEIGHT = 64
  48.     bot.image = nil
  49.     bot.speed = 6 -- set externally via StateMachine
  50.     bot.maxSpeed = 10
  51.     bot.defense = 4 -- set externally via StateMachine
  52.     bot.maxDefense = 10
  53.     bot.direction = "right"
  54.     bot.sightSpeed = 0.4
  55.     bot.sightX = nil
  56.     bot.sightY = nil
  57.     bot.currentSightX = nil
  58.     bot.currentSightY = nil
  59.    
  60.     local bg = display.newRect(20, 0, 20, 0)
  61.     bg:setFillColor(255, 0, 0, 255)
  62.     bg:setReferencePoint(display.TopLeftReferencePoint)
  63.     bot:insert(bg)
  64.  
  65.     local wheel1 = display.newImage("wheel.png")
  66.     local wheel2 = display.newImage("wheel.png")
  67.     local wheel3 = display.newImage("wheel.png")
  68.     wheel1:setReferencePoint(display.TopLeftReferencePoint)
  69.     wheel2:setReferencePoint(display.TopLeftReferencePoint)
  70.     wheel3:setReferencePoint(display.TopLeftReferencePoint)
  71.  
  72.     bot:insert(wheel1)
  73.     bot:insert(wheel2)
  74.     bot:insert(wheel3)
  75.     bot.wheel1 = wheel1
  76.     bot.wheel2 = wheel2
  77.     bot.wheel3 = wheel3
  78.  
  79.     -- holds srpites and flips for direction changing
  80.     bot.spriteHolder = display.newGroup()
  81.     bot:insert(bot.spriteHolder)
  82.  
  83.     function bot:showSprite(anime)
  84.         if self.spriteHolder and self.spriteHolder.image then
  85.             self.spriteHolder.image:removeSelf()
  86.             self.spriteHolder.image = nil
  87.         end
  88.  
  89.         local image
  90.         local showTheWheels = false
  91.         if anime == "scout" then
  92.             image = display.newImage("stand.png")
  93.         elseif anime == "assault" then
  94.             image = display.newImage("assault.png")
  95.             showTheWheels = true
  96.         elseif anime == "scoutLeft" or anime == "scoutRight" then
  97.             image = sprite.newSprite(WarBot.scoutSet)
  98.             image:prepare("scout")
  99.             image:play()
  100.         elseif anime == "artillery" then
  101.             image = sprite.newSprite(WarBot.artillerySet)
  102.             image:prepare("artillery")
  103.             image:play()
  104.         elseif anime == "artilleryReverse" then
  105.             image = sprite.newSprite(WarBot.artillerySet)
  106.             image:prepare("artilleryReverse")
  107.             image.currentFrame = 16
  108.             image:play()
  109.         elseif anime == "defend" then
  110.             image = sprite.newSprite(WarBot.defendSet)
  111.             image:prepare("defend")
  112.             image:play()
  113.         elseif anime == "sight" then
  114.             image = sprite.newSprite(WarBot.sightSet)
  115.             image:prepare("sight")
  116.             image:play()
  117.             showTheWheels = true
  118.         elseif anime == "sightReverse" then
  119.             image = sprite.newSprite(WarBot.sightSet)
  120.             image:prepare("sightReverse")
  121.             image.currentFrame = 4
  122.             image:play()
  123.             showTheWheels = true
  124.         elseif anime == "sniper" then
  125.             image = sprite.newSprite(WarBot.sniperSet)
  126.             image:prepare("sniper")
  127.             image:play()
  128.         elseif anime == "sniperReverse" then
  129.             image = sprite.newSprite(WarBot.sniperSet)
  130.             image:prepare("sniperReverse")
  131.             image.currentFrame = 20
  132.             image:play()
  133.         end
  134.  
  135.         if image == nil then error("failed to find an animation for: " .. anime) end
  136.         image:setReferencePoint(display.TopLeftReferencePoint)
  137.         self.spriteHolder:insert(image)
  138.         self.spriteHolder.image = image
  139.         self:showWheels(showTheWheels)
  140.  
  141.         -- now that we're inserted, it's easier to position things... these are the days when I miss Flash
  142.         if anime == "scout" or anime == "scoutRight" or anime == "scoutLeft" then
  143.             image.x = (self.WIDTH / 2) - (image.width / 2)
  144.             image.y = (self.HEIGHT / 2) - (image.height / 2)
  145.         elseif anime == "assault" then
  146.             image.x = self.WIDTH / 2 - image.width / 2
  147.             image.y = 0
  148.         elseif anime == "artillery" or anime == "artilleryReverse" then
  149.             image.x = self.WIDTH / 2 - image.width / 2
  150.             image.y = 2
  151.         elseif anime == "defend" then
  152.             image.x = 0
  153.             image.y = 0
  154.         elseif anime == "sight" or anime == "sightReverse" then
  155.             image.x = self.WIDTH / 2 - image.width / 2
  156.             image.y = -10
  157.         elseif anime == "sniper" or anime == "sniperReverse" then
  158.             image.x = self.WIDTH / 2 - image.width / 2
  159.             image.y = -40
  160.         end
  161.  
  162.         self:setDirection(self.direction)
  163.     end
  164.  
  165.     function bot:setDirection(direction)
  166.         self.direction = direction
  167.         local spriteHolder = self.spriteHolder
  168.         if spriteHolder then
  169.             if direction == "left" then
  170.                 spriteHolder.xScale = -1
  171.                 spriteHolder.x = self.WIDTH
  172.             elseif direction == "right" or direction == nil then
  173.                 spriteHolder.xScale = 1
  174.                 spriteHolder.x = 0
  175.             end
  176.         end
  177.     end
  178.  
  179.     function bot:showWheels(show)
  180.         local wheel1 = self.wheel1
  181.         local wheel2 = self.wheel2
  182.         local wheel3 = self.wheel3
  183.  
  184.         wheel1.isVisible = show
  185.         wheel2.isVisible = show
  186.         wheel3.isVisible = show
  187.  
  188.         wheel1.x = 0
  189.         wheel1.y = self.HEIGHT - wheel1.height - 2
  190.  
  191.         wheel2.x = self.WIDTH / 2 - wheel2.width / 2
  192.         wheel2.y = wheel1.y
  193.  
  194.         wheel3.x = self.WIDTH - wheel3.width
  195.         wheel3.y = wheel2.y
  196.     end
  197.  
  198.     function bot:setSpeed(value)
  199.         assert(value ~= nil, "Value must not be nil.")
  200.         assert(type(value) == "number", "value must be a number.")
  201.         local oldValue = self.speed
  202.         self.speed = value
  203.         self:dispatchEvent({name="onSpeedChanged", target=self, oldValue=oldValue, value=value, max=self.speedMax})
  204.     end
  205.  
  206.     function bot:setDefense(value)
  207.         assert(value ~= nil, "Value must not be nil.")
  208.         assert(type(value) == "number", "value must be a number.")
  209.         local oldValue = self.defense
  210.         self.defense = value
  211.         self:dispatchEvent({name="onDefenseChanged", target=self, oldValue=oldValue, value=value, max=self.defenseMax})
  212.     end
  213.  
  214.     function bot:tick(time)
  215.  
  216.     end
  217.  
  218.     function bot:startTrackingSight()
  219.         if self.sightTouchDelegate == nil then
  220.             self.sightTouchDelegate = function(e)bot:onSightTouch(e)end
  221.         end
  222.         Runtime:addEventListener("touch", self.sightTouchDelegate)
  223.         self.oldTick = self.tick
  224.         self.tick = self.sightTick
  225.         self.currentSightX = self.x + 30
  226.         self.currentSightY = self.y - 10
  227.         self.sightX = self.x
  228.         self.sightY = self.y
  229.     end
  230.  
  231.     function bot:onSightTouch(event)
  232.         self.sightX = event.x
  233.         self.sightY = event.y
  234.         return true
  235.     end
  236.  
  237.     function bot:stopTrackingSight()
  238.         Runtime:removeEventListener("touch", self.sightTouchDelegate)
  239.         self.tick = self.oldTick
  240.         self:destroySight()
  241.     end
  242.  
  243.     function bot:sightTick(time)
  244.         if self.currentSightX == nil or self.sightX == nil then return true end
  245.         if self.currentSightY == nil or self.sightY == nil then return true end
  246.         --print("self.currentSightX: ", self.currentSightX, ", self.sightX: ", self.sightX)
  247.         local deltaX = self.currentSightX - self.sightX
  248.         local deltaY = self.currentSightY - self.sightY
  249.         local dist = math.sqrt((deltaX * deltaX) + (deltaY * deltaY))
  250.         -- HACK: too tired to rmemeber why it divides by zero and dist == 0... anyway
  251.         if dist == 0 then
  252.             self.currentSightX = self.sightX
  253.             self.currentSightY = self.sightY
  254.             self:redrawSight()
  255.             return true
  256.         end
  257.        
  258.         local moveX = self.sightSpeed * (deltaX / dist) * time
  259.         local moveY = self.sightSpeed * (deltaY / dist) * time
  260.         --print("s: ", self.sightSpeed, ", deltaX: ", deltaX, ", dist: ", dist, ", moveX: ", moveX)
  261.         if (math.abs(moveX) > dist or math.abs(moveY) > dist) then
  262.             self.currentSightX = self.sightX
  263.             self.currentSightY = self.sightY
  264.         else
  265.             self.currentSightX = self.currentSightX - moveX
  266.             self.currentSightY = self.currentSightY - moveY
  267.         end
  268.        
  269.         self:redrawSight()
  270.     end
  271.  
  272.     function bot:redrawSight()
  273.         bot:destroySight()
  274.         self.laserSight = display.newLine(self.x + 30, self.y - 10, self.currentSightX, self.currentSightY)
  275.         self.laserSight:setColor(0, 255, 0, 180)
  276.         self.laserSight.width = 2
  277.     end
  278.  
  279.     function bot:destroySight()
  280.         if self.laserSight then
  281.             self.laserSight:removeSelf()
  282.             self.laserSight = nil
  283.         end
  284.     end
  285.  
  286.     function bot:startSniperShooting()
  287.         if self.sniperTouchDelegate == nil then
  288.             self.sniperTouchDelegate = function(e)self:onSniperTouch(e)end
  289.         end
  290.         Runtime:addEventListener("touch", self.sniperTouchDelegate)
  291.     end
  292.  
  293.     function bot:getBusyTerryChildIGuessIDidntKnow(targetA, targetB)
  294.         local dx = targetA.x - targetB.x
  295.         local dy = targetA.y - targetB.y
  296.         local radians = math.atan2(dy, dx)
  297.         local deg = math.deg(radians)
  298.         local theX = math.sin(deg)
  299.         local theY = math.cos(deg)
  300.         return theX, theY
  301.     end
  302.  
  303.     function bot:onSniperTouch(event)
  304.         if event.phase == "began" then
  305.             local bullet = display.newCircle(self.x + 70, self.y + 27, 2)
  306.             -- TODO: figure this out
  307.             local theX, theY = self:getBusyTerryChildIGuessIDidntKnow(bullet, event)
  308.             print("theX: ", theX, ", theY: ", theY)
  309.             physics.addBody(bullet, {density=2, friction=0.2, bounce=0.2, isBullet = true})
  310.             bullet.rot = math.atan2(bullet.y - event.y,  bullet.x - event.y) / math.pi * 180 -90;
  311.             bullet.angle = (bullet.rot -90) * math.pi / 180;
  312.             --self.x = self.x + math.cos(self.angle) * self.speed * millisecondsPassed
  313.             --self.y = self.y + math.sin(self.angle) * self.speed * millisecondsPassed
  314.             local targetX = math.cos(bullet.angle)
  315.             local targetY = math.sin(bullet.angle)
  316.             bullet:applyLinearImpulse( targetX, targetY, bullet.x + (bullet.width / 2), bullet.y + (bullet.height / 2))
  317.         end
  318.     end
  319.  
  320.     function bot:stopSniperShooting()
  321.         Runtime:removeEventListener("touch", self.sniperTouchDelegate)
  322.     end
  323.  
  324.     function bot:getJoint(obj1, obj2, x, y)
  325.         local joint = physics.newJoint("pivot", obj1, obj2, x, y)
  326.         --local joint = physics.newJoint("wheel", obj1, obj2, x, y, 2, 2)
  327.         joint.maxMotorTorque = 1000000
  328.         --joint.maxMotorForce = torque
  329.         joint.motorSpeed = 0
  330.         return joint
  331.     end
  332.  
  333.     function bot:getWheel()
  334.         local radius = 10
  335.         local wheelShape = display.newCircle(0, 0, radius)
  336.         --self:insert(wheelShape)
  337.         wheelShape:setFillColor(0, 255, 0, 0)
  338.         physics.addBody(wheelShape, "dynamic",
  339.             { density=0.3, friction=0.7, bounce=0.2, isBullet=true, radius=radius})
  340.         return wheelShape
  341.     end
  342.  
  343.     function bot:startMoving()
  344.         local direction = self.direction
  345.         local wheelJoint = self.wheelJoint
  346.         if direction == "right" then
  347.             wheelJoint.motorSpeed = self.speed * 10000
  348.         elseif direction == "left" then
  349.             wheelJoint.motorSpeed = -(self.speed * 10000)
  350.         end
  351.     end
  352.  
  353.     function bot:stopMoving()
  354.         self.wheelJoint.motorSpeed = 0
  355.         if self.speed <= 0 then return true end
  356.  
  357.         local force
  358.         if self.direction == "right" then
  359.             force = -self.speed
  360.         else
  361.             force = self.speed
  362.         end
  363.         force = force * 2
  364.         self:applyLinearImpulse(force, 0, 40, 32)
  365.     end
  366.  
  367.  
  368.     bot:showSprite("scout")
  369.  
  370.     local botShape = {10,0, 52,0, 52,64, 10,64}
  371.     physics.addBody(bot, "dynamic",
  372.             { density=1, friction=0.4, bounce=0.2, isBullet=true, shape=botShape})
  373.     bot.isFixedRotation = true
  374.  
  375.    
  376.  
  377.     local wheel1 = bot:getWheel()
  378.     wheel1.x = bot.x + 32
  379.     wheel1.y = bot.y + 60
  380.  
  381.     local cX, cY = bot:localToContent(bot.x + 32, bot.y + 60)
  382.     local wheelJoint = bot:getJoint(bot, wheel1, wheel1.x, wheel1.y)
  383.     --local joint2 = bot:getJoint(bot, wheel2, 60, 0)
  384.     bot.wheelJoint = wheelJoint
  385.  
  386.  
  387.     return bot
  388.  
  389. end
  390.  
  391. return WarBot
Advertisement
Add Comment
Please, Sign In to add comment