Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local s2 = sqrt(2)
- local cos, sin, rad = math.cos, math.sin, math.rad
- local GAME_FRAME_NAME = "HARRYS_GAME_FRAME"
- local GRID_WIDTH = 1200
- local GRID_HEIGHT = 600
- local GRID_HEIGHT_OFFSET = 10
- local GRID_REFRESH_RATE = 0.1
- local GRID_ROW_COUNT = 6
- local GRID_COLUMN_COUNT = 6
- local GRID_TILE_WIDTH = 147
- local GRID_TILE_HEIGHT = 77
- local GRID_TILE_Y_OFFSET = -(GRID_TILE_HEIGHT * (GRID_ROW_COUNT / 2))
- local TILE_IMAGE_WIDTH = 128
- local TILE_IMAGE_HEIGHT = 128
- function scaleBetween(unscaledNum, minAllowed, maxAllowed, _min, _max)
- return (maxAllowed - minAllowed) * (unscaledNum - _min) / (_max - _min) + minAllowed
- end
- -- (maxAllowed - minAllowed) * (unscaledNum - min) / (max - min) + minAllowed
- -- scaleBetween(unscaled, 0, 100, minRange, maxRange);
- local GRID_CONVERTED_WIDTH_OFFSET = 1
- local GRID_CONVERTED_HEIGHT_OFFSET = 0.5
- ----------------------->
- -- Notes
- ----------------------->
- -- Put in Game_OnUpdate, testing rotating sprite
- --[[
- if (self.dragging) then
- local frame = self
- local angleInc = 0.07
- frame.hAngle = (frame.hAngle or 0) - angleInc;
- frame.s = sin(frame.hAngle)
- frame.c = cos(frame.hAngle)
- frame.texture:SetTexCoord(0.5-frame.s, 0.5+frame.c,
- 0.5+frame.c, 0.5+frame.s,
- 0.5-frame.c, 0.5-frame.s,
- 0.5+frame.s, 0.5-frame.c)
- end
- ]]
- -- RotateTexture(testSprite.texture, 45)
- --[[
- testSprite:SetMovable(true)
- testSprite:RegisterForDrag("LeftButton")
- testSprite:SetScript("OnDragStart", function(self, button) self.dragging = true; self:StartMoving(); end)
- testSprite:SetScript("OnDragStop", function(self) self.dragging = false; self:StopMovingOrSizing(); end)
- testSprite.timer = 0
- testSprite:SetScript("OnUpdate", Game_OnUpdate)
- ]]
- ---------------------->
- -- Local functions
- ---------------------->
- ------------------>>
- -- Drawing
- ------------------>>
- local function DrawGameFrame(frame)
- local i, j
- for i = GRID_ROW_COUNT, 1, -1 do
- for j = GRID_COLUMN_COUNT, 1, -1 do
- local x, y = TwoDToIso(j * (GRID_TILE_HEIGHT), i * (GRID_TILE_WIDTH / 2))
- local name = "Tile(" .. tostring(i) .. ", " .. tostring(j) .. ")"
- -- Temporary, need to implement drawing matrix
- local tilepath = "Interface/AddOns/aHarrysGame/Sprites/dirt" -- displays green, good for seeing border
- local tilepath = "Interface/AddOns/aHarrysGame/marblepath"
- local tile = BuildSprite(name, GRID_TILE_WIDTH, GRID_TILE_HEIGHT, tilepath, frame)
- print("Drawing(" .. x .. ", " .. y .. ")...")
- 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)
- tile:SetPoint("CENTER", parent, "CENTER", x, y + GRID_TILE_Y_OFFSET)
- end
- end
- print("converted_w" .. GRID_CONVERTED_WIDTH_OFFSET)
- print("converted_h" .. GRID_CONVERTED_HEIGHT_OFFSET)
- end
- local function Game_OnUpdate(self, elapsed)
- if self.pause then
- return
- end
- self.timer = self.timer + elapsed
- if self.timer > GRID_REFRESH_RATE then
- DrawGameFrame(self)
- self.timer = 0
- end
- end
- local function BuildGameFrame()
- local frame = CreateFrame("frame", GAME_FRAME_NAME)
- frame:SetFrameLevel(1)
- frame:SetWidth(GRID_WIDTH)
- frame:SetHeight(GRID_HEIGHT)
- frame:SetPoint("CENTER", UIParent, "CENTER", 0, GRID_HEIGHT_OFFSET)
- frame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",
- edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
- tile = true, tileSize = 64, edgeSize = 16,
- insets = { left = 4, right = 4, top = 4, bottom = 4 }})
- frame:SetBackdropColor(0, 0, 0, 1)
- -- Exit button
- local exitButton = BuildTextButton(50, 40, "Exit", frame)
- exitButton:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -3, -2)
- exitButton:SetFrameLevel(3)
- exitButton:SetScript("OnClick", ToggleGameFrame)
- -- Scripts
- frame.timer = 0
- frame:HookScript("OnUpdate", Game_OnUpdate)
- -- Test
- -- "Interface\AddOns\aHarrysGame\Pictures\test.blp"
- -- local testSprite = BuildSprite("SpriteA", 128, 128, "Interface/AddOns/aHarrysGame/marblepath", frame) --"Interface/AddOns/aHarrysGame/Sprites/buttons0000", frame)
- -- print("Texture is: [" .. tostring(testSprite.texture:GetTexture()) .. "]")
- end
- ---------------------->
- -- Global functions
- ---------------------->
- function GetGameFrame()
- -- Return it from the global table
- return _G[GAME_FRAME_NAME]
- end
- function ToggleGameFrame()
- -- Toggle frame if it exists
- local gameFrame = GetGameFrame()
- if gameFrame then
- if gameFrame:IsVisible() then
- gameFrame:Hide()
- else
- gameFrame:Show()
- end
- gameFrame.pause = not gameFrame:IsVisible()
- gameFrame.timer = 0
- else
- -- Otherwise construct the frame
- BuildGameFrame()
- end
- end
- function BuildTextButton(width, height, text, parent)
- local button = CreateFrame("Button", nil, parent)
- local texture = button:CreateTexture(nil,"BACKGROUND")
- texture:SetTexture("Interface/Portal/Widgets/wcbutton-big")
- texture:SetAllPoints(button)
- button.texture = t
- button:SetWidth(width)
- button:SetHeight(height)
- button:SetText(text)
- button:SetNormalFontObject(_G["GameFontNormalHuge"])
- --button:SetHighlightFontObject("GameFontHighlightHuge")
- --button:SetDisabledFontObject(_G["GameFontDisableHuge"])
- --button:SetHighlightTextureObject("UIPanelButtonHighlightTexture")
- return button
- end
- function BuildSprite(name, width, height, texturePath, parent)
- local sprite = CreateFrame("Button", name, parent)
- sprite:SetWidth(width)
- sprite:SetHeight(height)
- local texture = sprite:CreateTexture(nil,"BACKGROUND")
- texture:SetTexture(texturePath)
- texture:SetAllPoints(sprite)
- sprite.texture = texture
- sprite:SetPoint("CENTER", parent, "CENTER", 0, 0)
- sprite:SetFrameLevel(2)
- sprite:SetBackdropColor(0, 0, 0, 1)
- return sprite
- end
- ---------------------->
- -- Utility
- ---------------------->
- function CalculateCorner(angle)
- local r = rad(angle)
- return 0.5 + cos(r) / s2, 0.5 + sin(r) / s2
- end
- function RotateTexture(texture, angle)
- local LRx, LRy = CalculateCorner(angle + 45)
- local LLx, LLy = CalculateCorner(angle + 135)
- local ULx, ULy = CalculateCorner(angle + 225)
- local URx, URy = CalculateCorner(angle - 45)
- texture:SetTexCoord(ULx, ULy, LLx, LLy, URx, URy, LRx, LRy)
- end
- function IsoTo2D(x, y)
- local newX = (2 * y + x) / 2
- local newY = (2 * y - x) / 2
- return newX, newY
- end
- function TwoDToIso(x, y)
- local newX = x - y
- local newY = (x + y) / 2
- return newX, newY
- end
- ---------------------->
Advertisement
Add Comment
Please, Sign In to add comment