Advertisement
Guest User

HelperClass

a guest
Jan 8th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.77 KB | None | 0 0
  1. -- HelperClass
  2.  
  3. -- Created by: Mr Coxall
  4. -- Created on: Nov 2014
  5. -- Created for: ICS2O
  6. -- This is a collection of classes and a scene management system,
  7. -- to help in the aid of coding.
  8.  
  9.  
  10. -- Button Class
  11.  
  12. -- This class simplifies the process of checking if a sprite button is pressed or not
  13. -- Then you can ask the button if it is being touched or was ended.
  14. -- Works best with vector sprite buttons, since it enlarges them when they are touched
  15. --
  16. -- Parameters to pass in:
  17. --  1. Untouched button image name
  18. --  2. A vector2 that is the location of the button
  19.  
  20. Button = class()
  21.  
  22. function Button:init(buttonImage, buttonPosition, buttonScaleSize)
  23.     -- accepts the button image, location and scale to draw it
  24.    
  25.     self.buttonImage = buttonImage
  26.     self.buttonLocation = buttonPosition
  27.     self.buttonScaleSize = buttonScaleSize or 1.0
  28.    
  29.     self.buttonImageSize = vec2(spriteSize(self.buttonImage))
  30.     -- resize the button to new size (or scale by 1.0 if no scale given!)
  31.     if not(self.buttonScaleSize == 1.0) then
  32.         self.buttonImage = resizeImage(self.buttonImage,(self.buttonScaleSize*self.buttonImageSize.x),(self.buttonScaleSize*self.buttonImageSize.y))
  33.         -- now rest button image size
  34.         self.buttonImageSize = vec2(spriteSize(self.buttonImage))  
  35.     end
  36.     self.buttonTouchScale = 1.15
  37.     --self.buttonImageSize = vec2(spriteSize(self.buttonImage))    
  38.     self.currentButtonImage = self.buttonImage
  39.     self.buttonTouchedImage = resizeImage(self.buttonImage, (self.buttonImageSize.x*self.buttonTouchScale), (self.buttonImageSize.y*self.buttonTouchScale))  
  40.     self.selected = false
  41. end
  42.  
  43. function Button:draw()
  44.     -- Codea does not automatically call this method
  45.  
  46.     pushStyle()  
  47.     pushMatrix()
  48.     noFill()
  49.     noSmooth()
  50.     noStroke()
  51.      
  52.     sprite(self.currentButtonImage, self.buttonLocation.x, self.buttonLocation.y)
  53.    
  54.     popMatrix()
  55.     popStyle()
  56. end
  57.  
  58. function Button:touched(touch)  
  59.     -- local varaibles
  60.     local currentTouchPosition = vec2(touch.x, touch.y)
  61.    
  62.     -- reset touching variable to false
  63.     self.selected = false
  64.    
  65.     if (touch.state == BEGAN) then
  66.          if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  67.             (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  68.             (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  69.             (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  70.                
  71.             self.currentButtonImage = self.buttonTouchedImage
  72.             --print("Now touching! - began")
  73.         else          
  74.             self.currentButtonImage = self.buttonImage  
  75.             --print("Not touching - began")
  76.         end            
  77.     end
  78.    
  79.     if (touch.state == MOVING) then
  80.         if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  81.             (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  82.             (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  83.             (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  84.        
  85.             self.currentButtonImage = self.buttonTouchedImage
  86.             --print("Now touching! - moving")
  87.         else
  88.             self.currentButtonImage = self.buttonImage  
  89.             --print("Not touching - moving")
  90.         end
  91.     end
  92.    
  93.     if (touch.state == ENDED) then
  94.         if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  95.             (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  96.             (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  97.             (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  98.        
  99.             self.selected = true
  100.             --print("Activated button")
  101.         end
  102.          
  103.         self.currentButtonImage = self.buttonImage
  104.     end
  105. end
  106.  
  107. function resizeImage(img, width, height)
  108.     -- function from
  109.     -- http://codea.io/talk/discussion/3490/importing-pics-from-dropbox/p1
  110.    
  111.     local newImg = image(width,height)
  112.     setContext(newImg)
  113.     sprite( img, width/2, height/2, width, height )    
  114.     setContext()
  115.     return newImg
  116. end
  117.  
  118.  
  119. -- Dragging Object Class
  120. -- This class simplifies the process of dragging and dropping objects
  121. -- You can have several objects interacting, but it is not mulit-touch
  122. --
  123. -- Parameters to pass in:
  124. --  1. Object image name
  125. --  2. A vector2 that is the location of the button
  126. --  3. Optional object id
  127.  
  128.  
  129. SpriteObject = class()
  130.  
  131. function SpriteObject:init(objectImage, objectStartPosition, objectID)
  132.  
  133.     self.objectImage = objectImage
  134.     self.objectStartLocation = objectStartPosition
  135.     self.ID = objectID or math.random()
  136.    
  137.     self.objectCurrentLocation = self.objectStartLocation
  138.     self.objectImageSize = vec2(spriteSize(self.objectImage))
  139.     --self.selected = false
  140.     self.dragOffset = vec2(0,0)
  141.     self.draggable = true
  142.     -- yes, the following does need to be global to the entire program
  143.     -- this is the only way (easy way!) to move things around and no get
  144.     -- several object "attached" to each other
  145.     -- this way just the top object in the stack moves
  146.     DRAGGING_OBJECT_MOVING = nil or DRAGGING_OBJECT_MOVING    
  147. end
  148.  
  149. function SpriteObject:draw()
  150.     -- Codea does not automatically call this method
  151.    
  152.     pushStyle()  
  153.     pushMatrix()
  154.     noFill()
  155.     noSmooth()
  156.     noStroke()
  157.      
  158.     sprite(self.objectImage, self.objectCurrentLocation.x, self.objectCurrentLocation.y)
  159.      
  160.     popMatrix()
  161.     popStyle()
  162.  
  163. end
  164.  
  165. function SpriteObject:touched(touch)
  166.     -- Codea does not automatically call this method
  167.    
  168.     -- local varaibles
  169.     local currentTouchPosition = vec2(touch.x, touch.y)
  170.    
  171.     if (touch.state == BEGAN and self.draggable == true) then
  172.         if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
  173.             (self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
  174.             (self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
  175.             (self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
  176.             -- if the touch has began, we need to find delta from touch to center of object
  177.             -- since will need it to reposition the object for draw
  178.             -- subtracting 2 vec2s here
  179.             self.dragOffset = self.objectCurrentLocation - currentTouchPosition
  180.             DRAGGING_OBJECT_MOVING = self.ID
  181.         end        
  182.     end
  183.    
  184.     if (touch.state == MOVING and self.draggable == true) then
  185.         if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
  186.             (self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
  187.             (self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
  188.             (self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
  189.                 -- only let it move if self.draggable == true
  190.             if (self.draggable == true) then
  191.                 -- add the offset back in for its new position
  192.                 if (self.ID == DRAGGING_OBJECT_MOVING) then
  193.                     self.objectCurrentLocation = currentTouchPosition + self.dragOffset
  194.                 end
  195.             end
  196.         end      
  197.     end
  198.    
  199.     if (touch.state == ENDED and self.draggable == true) then
  200.         DRAGGING_OBJECT_MOVING = nil  
  201.     end
  202. end
  203.  
  204. function SpriteObject:isTouching(otherSpriteObject)
  205.     -- this method checks if one dragging object is touching another dragging object
  206.    
  207.     local isItTouching = false
  208.    
  209.     if( (self.objectCurrentLocation.x + self.objectImageSize.x/2) > (otherSpriteObject.objectCurrentLocation.x - otherSpriteObject.objectImageSize.x/2) and
  210.         (self.objectCurrentLocation.x - self.objectImageSize.x/2) < (otherSpriteObject.objectCurrentLocation.x + otherSpriteObject.objectImageSize.x/2) and
  211.         (self.objectCurrentLocation.y - self.objectImageSize.y/2) < (otherSpriteObject.objectCurrentLocation.y + otherSpriteObject.objectImageSize.y/2) and
  212.         (self.objectCurrentLocation.y + self.objectImageSize.y/2) > (otherSpriteObject.objectCurrentLocation.y - otherSpriteObject.objectImageSize.y/2) ) then
  213.         -- if true, then not touching
  214.         isItTouching = true
  215.     end        
  216.    
  217.     return isItTouching
  218. end
  219.  
  220.  
  221. -- SceneManager
  222. --
  223. -- This file lets you easily manage different scenes
  224. --     Original code from Brainfox, off the Codea forums
  225.  
  226. Scene = {}
  227. local scenes = {}
  228. local sceneNames = {}
  229. local currentScene = nil
  230.  
  231. setmetatable(Scene,{__call = function(_,name,cls)
  232.    if (not currentScene) then
  233.        currentScene = 1
  234.    end
  235.    table.insert(scenes,cls)
  236.    sceneNames[name] = #scenes
  237.    Scene_Select = nil
  238. end})
  239.  
  240. --Change scene
  241. Scene.Change = function(name)
  242.   currentScene = sceneNames[name]
  243.     scenes[currentScene]:init()
  244.    if (Scene_Select) then
  245.        Scene_Select = currentScene
  246.    end
  247.    
  248.    collectgarbage()
  249. end
  250.  
  251. Scene.Draw = function()
  252.    pushStyle()
  253.    pushMatrix()
  254.    scenes[currentScene]:draw()
  255.    popMatrix()
  256.    popStyle()
  257. end
  258.  
  259. Scene.Touched = function(t)
  260.    if (scenes[currentScene].touched) then
  261.        scenes[currentScene]:touched(t)
  262.    end
  263. end
  264.  
  265. Scene.Keyboard = function()
  266.    if (scenes[currentScene].keyboard) then
  267.        scenes[currentScene]:keyboard(key)
  268.    end
  269. end
  270.  
  271. Scene.OrientationChanged = function()
  272.    if (scenes[currentScene].orientationChanged) then
  273.        scenes[currentScene]:orientationChanged()
  274.    end
  275. end
  276.  
  277.  
  278. -- Simulates the use of Game Center, so you can actually write the calls in Codea and still test on the iPad
  279.  
  280. if (DEBUG_GAMECENTER == true) then
  281.        
  282.     function GameCenterClass()
  283.         -- the new instance
  284.         local self = {          
  285.         }
  286.  
  287.         function self.enabled()
  288.             -- simulates that Game Center is logged in
  289.            
  290.             return true
  291.         end
  292.        
  293.         function self.showLeaderboards(leaderboardID)
  294.             -- Show leaderboards
  295.            
  296.             if (leaderboardID == nil) then
  297.                 alert("Game Center leaderboard screen will pop up.", "Game Center Simulation")
  298.             else
  299.                 alert("Game Center leaderboard with ID " .. leaderboardID .. " will pop up.", "Game Center Simulation")
  300.             end
  301.         end
  302.        
  303.         function self.showAchievements()
  304.             -- Show achievements
  305.            
  306.             alert("Game Center achievement screen will pop up.", "Game Center Simulation")
  307.         end
  308.        
  309.         function self.submitScore(score, leaderboardID)
  310.             -- simulates submitting a score to Game Center
  311.            
  312.             if (leaderboardID == nil) then
  313.                 alert("Default Leaderboard updated with score " .. score, "Game Center Simulation")
  314.             else
  315.                 alert("Leaderboard with ID " .. leaderboardID .. " updated with score " .. score, "Game Center Simulation")
  316.             end
  317.         end
  318.  
  319.         function self.submitAchievement(achievementID, percent)
  320.             -- simulates submitting an achievement to Game Center
  321.        
  322.             alert("Achievement with ID " .. achievementID .. " updated to " .. percent .. "%", "Game Center Simulation")
  323.         end
  324.  
  325.         -- return the instance
  326.         return self
  327.     end
  328.    
  329.     -- create a global instance and call it "gamecenter"
  330.     -- the exact name you need to impliment when you make the changes in Xcode
  331.     -- but now you can do it locally :)
  332.     gamecenter = GameCenterClass()    
  333. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement