stoneharry

Isometric WoW Addon

Jul 15th, 2018
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.49 KB | None | 0 0
  1.  
  2. local s2 = sqrt(2)
  3. local cos, sin, rad = math.cos, math.sin, math.rad
  4.  
  5. local GAME_FRAME_NAME = "HARRYS_GAME_FRAME"
  6.  
  7. local GRID_WIDTH = 1200
  8. local GRID_HEIGHT = 600
  9. local GRID_HEIGHT_OFFSET = 10
  10. local GRID_REFRESH_RATE = 0.1
  11. local GRID_ROW_COUNT = 6
  12. local GRID_COLUMN_COUNT = 6
  13. local GRID_TILE_WIDTH = 147
  14. local GRID_TILE_HEIGHT = 77
  15. local GRID_TILE_Y_OFFSET = -(GRID_TILE_HEIGHT * (GRID_ROW_COUNT / 2))
  16. local TILE_IMAGE_WIDTH = 128
  17. local TILE_IMAGE_HEIGHT = 128
  18.  
  19. function scaleBetween(unscaledNum, minAllowed, maxAllowed, _min, _max)
  20.     return (maxAllowed - minAllowed) * (unscaledNum - _min) / (_max - _min) + minAllowed
  21. end
  22.  
  23.  
  24. -- (maxAllowed - minAllowed) * (unscaledNum - min) / (max - min) + minAllowed
  25. -- scaleBetween(unscaled, 0, 100, minRange, maxRange);
  26. local GRID_CONVERTED_WIDTH_OFFSET = 1
  27. local GRID_CONVERTED_HEIGHT_OFFSET = 0.5
  28.  
  29.  
  30. ----------------------->
  31. -- Notes
  32. ----------------------->
  33.  
  34. -- Put in Game_OnUpdate, testing rotating sprite
  35. --[[
  36.         if (self.dragging) then
  37.             local frame = self
  38.             local angleInc = 0.07
  39.             frame.hAngle = (frame.hAngle or 0) - angleInc;
  40.             frame.s = sin(frame.hAngle)
  41.             frame.c = cos(frame.hAngle)
  42.             frame.texture:SetTexCoord(0.5-frame.s, 0.5+frame.c,
  43.                         0.5+frame.c, 0.5+frame.s,
  44.                         0.5-frame.c, 0.5-frame.s,
  45.                         0.5+frame.s, 0.5-frame.c)
  46.         end
  47. ]]
  48.     -- RotateTexture(testSprite.texture, 45)
  49.     --[[
  50.     testSprite:SetMovable(true)
  51.     testSprite:RegisterForDrag("LeftButton")
  52.     testSprite:SetScript("OnDragStart", function(self, button) self.dragging = true; self:StartMoving(); end)
  53.     testSprite:SetScript("OnDragStop", function(self) self.dragging = false; self:StopMovingOrSizing(); end)
  54.     testSprite.timer = 0
  55.     testSprite:SetScript("OnUpdate", Game_OnUpdate)
  56.     ]]
  57.  
  58. ---------------------->
  59. -- Local functions
  60. ---------------------->
  61.  
  62. ------------------>>
  63. -- Drawing
  64. ------------------>>
  65.  
  66. local function DrawGameFrame(frame)
  67.     local i, j
  68.     for i = GRID_ROW_COUNT, 1, -1 do
  69.         for j = GRID_COLUMN_COUNT, 1, -1 do
  70.             local x, y = TwoDToIso(j * (GRID_TILE_HEIGHT), i * (GRID_TILE_WIDTH / 2))
  71.             local name = "Tile(" .. tostring(i) .. ", " .. tostring(j) .. ")"
  72.            
  73.             -- Temporary, need to implement drawing matrix
  74.             local tilepath = "Interface/AddOns/aHarrysGame/Sprites/dirt" -- displays green, good for seeing border
  75.             local tilepath = "Interface/AddOns/aHarrysGame/marblepath"
  76.            
  77.             local tile = BuildSprite(name, GRID_TILE_WIDTH, GRID_TILE_HEIGHT, tilepath, frame)
  78.            
  79.             print("Drawing(" .. x .. ", " .. y .. ")...")
  80.            
  81.             tile.texture:SetTexCoord(0, 0, 0, GRID_CONVERTED_HEIGHT_OFFSET + 0.015, GRID_CONVERTED_WIDTH_OFFSET, 0, GRID_CONVERTED_WIDTH_OFFSET, GRID_CONVERTED_HEIGHT_OFFSET + 0.015)
  82.             tile:SetPoint("CENTER", parent, "CENTER", x, y + GRID_TILE_Y_OFFSET)
  83.         end
  84.     end
  85.     print("converted_w" .. GRID_CONVERTED_WIDTH_OFFSET)
  86.     print("converted_h" .. GRID_CONVERTED_HEIGHT_OFFSET)
  87. end
  88.  
  89. local function Game_OnUpdate(self, elapsed)
  90.     if self.pause then
  91.         return
  92.     end
  93.     self.timer = self.timer + elapsed
  94.     if self.timer > GRID_REFRESH_RATE then
  95.        
  96.         DrawGameFrame(self)
  97.        
  98.         self.timer = 0
  99.     end
  100. end
  101.  
  102. local function BuildGameFrame()
  103.     local frame = CreateFrame("frame", GAME_FRAME_NAME)
  104.     frame:SetFrameLevel(1)
  105.     frame:SetWidth(GRID_WIDTH)
  106.     frame:SetHeight(GRID_HEIGHT)
  107.     frame:SetPoint("CENTER", UIParent, "CENTER", 0, GRID_HEIGHT_OFFSET)
  108.    
  109.     frame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",
  110.                         edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
  111.                         tile = true, tileSize = 64, edgeSize = 16,
  112.                         insets = { left = 4, right = 4, top = 4, bottom = 4 }})
  113.     frame:SetBackdropColor(0, 0, 0, 1)
  114.    
  115.     -- Exit button
  116.     local exitButton = BuildTextButton(50, 40, "Exit", frame)
  117.     exitButton:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -3, -2)
  118.     exitButton:SetFrameLevel(3)
  119.     exitButton:SetScript("OnClick", ToggleGameFrame)
  120.    
  121.     -- Scripts
  122.     frame.timer = 0
  123.     frame:HookScript("OnUpdate", Game_OnUpdate)
  124.    
  125.     -- Test
  126.     --                                                  "Interface\AddOns\aHarrysGame\Pictures\test.blp"
  127.     -- local testSprite = BuildSprite("SpriteA", 128, 128, "Interface/AddOns/aHarrysGame/marblepath", frame) --"Interface/AddOns/aHarrysGame/Sprites/buttons0000", frame)
  128.     -- print("Texture is: [" .. tostring(testSprite.texture:GetTexture()) .. "]")
  129.    
  130.  
  131. end
  132.  
  133. ---------------------->
  134. -- Global functions
  135. ---------------------->
  136.  
  137. function GetGameFrame()
  138.     -- Return it from the global table
  139.     return _G[GAME_FRAME_NAME]
  140. end
  141.  
  142. function ToggleGameFrame()
  143.     -- Toggle frame if it exists
  144.     local gameFrame = GetGameFrame()
  145.     if gameFrame then
  146.         if gameFrame:IsVisible() then
  147.             gameFrame:Hide()
  148.         else
  149.             gameFrame:Show()
  150.         end
  151.         gameFrame.pause = not gameFrame:IsVisible()
  152.         gameFrame.timer = 0
  153.     else
  154.         -- Otherwise construct the frame
  155.         BuildGameFrame()
  156.     end
  157. end
  158.  
  159. function BuildTextButton(width, height, text, parent)
  160.     local button = CreateFrame("Button", nil, parent)
  161.    
  162.     local texture = button:CreateTexture(nil,"BACKGROUND")
  163.     texture:SetTexture("Interface/Portal/Widgets/wcbutton-big")
  164.     texture:SetAllPoints(button)
  165.     button.texture = t
  166.    
  167.     button:SetWidth(width)
  168.     button:SetHeight(height)
  169.     button:SetText(text)
  170.    
  171.     button:SetNormalFontObject(_G["GameFontNormalHuge"])
  172.     --button:SetHighlightFontObject("GameFontHighlightHuge")
  173.     --button:SetDisabledFontObject(_G["GameFontDisableHuge"])
  174.     --button:SetHighlightTextureObject("UIPanelButtonHighlightTexture")
  175.    
  176.     return button
  177. end
  178.  
  179. function BuildSprite(name, width, height, texturePath, parent)
  180.     local sprite = CreateFrame("Button", name, parent)
  181.     sprite:SetWidth(width)
  182.     sprite:SetHeight(height)
  183.    
  184.     local texture = sprite:CreateTexture(nil,"BACKGROUND")
  185.     texture:SetTexture(texturePath)
  186.     texture:SetAllPoints(sprite)
  187.     sprite.texture = texture
  188.    
  189.     sprite:SetPoint("CENTER", parent, "CENTER", 0, 0)
  190.     sprite:SetFrameLevel(2)
  191.     sprite:SetBackdropColor(0, 0, 0, 1)
  192.    
  193.     return sprite
  194. end
  195.  
  196. ---------------------->
  197. -- Utility
  198. ---------------------->
  199.  
  200. function CalculateCorner(angle)
  201.     local r = rad(angle)
  202.     return 0.5 + cos(r) / s2, 0.5 + sin(r) / s2
  203. end
  204.  
  205. function RotateTexture(texture, angle)
  206.     local LRx, LRy = CalculateCorner(angle + 45)
  207.     local LLx, LLy = CalculateCorner(angle + 135)
  208.     local ULx, ULy = CalculateCorner(angle + 225)
  209.     local URx, URy = CalculateCorner(angle - 45)
  210.    
  211.     texture:SetTexCoord(ULx, ULy, LLx, LLy, URx, URy, LRx, LRy)
  212. end
  213.  
  214. function IsoTo2D(x, y)
  215.     local newX = (2 * y + x) / 2
  216.     local newY = (2 * y - x) / 2
  217.     return newX, newY
  218. end
  219.  
  220. function TwoDToIso(x, y)
  221.     local newX = x - y
  222.     local newY = (x + y) / 2
  223.     return newX, newY
  224. end
  225.  
  226. ---------------------->
Advertisement
Add Comment
Please, Sign In to add comment