Skip to content

Instantly share code, notes, and snippets.

@devilstower
Created April 23, 2012 02:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilstower/2468466 to your computer and use it in GitHub Desktop.
Save devilstower/2468466 to your computer and use it in GitHub Desktop.
Battle Chips 0.1 (part 2)
Token = class()
function Token:init(code, x, y, clr)
    -- you can accept and set parameters here
    self.x = x
    self.y = y
    self.code = code
end
function Token:draw()  
    pushStyle()
    stroke(0, 0, 0, 255)
    strokeWidth(2)
    fill(self.code.color)
    rect(self.x, self.y, self.x + 130, self.y + 32)
    fill(127, 127, 127, 255)
    rect(self.x, self.y, self.x + 8, self.y - 8)
    rect(self.x + 30, self.y, self.x + 38, self.y - 8)
    rect(self.x + 60, self.y, self.x + 68, self.y - 8)
    rect(self.x + 90, self.y, self.x + 98, self.y - 8)
    rect(self.x + 122, self.y, self.x + 130, self.y - 8)
    rect(self.x, self.y + 32, self.x + 8, self.y + 38)
    rect(self.x + 30, self.y + 32, self.x + 38, self.y + 38)
    rect(self.x + 60, self.y + 32, self.x + 68, self.y + 38)
    rect(self.x + 90, self.y + 32, self.x + 98, self.y + 38)
    rect(self.x + 122, self.y + 32, self.x + 130, self.y + 38)
    noStroke()
    fill(230, 230, 230, 255)
    font("Courier")
    fontSize(16)
    text(self.code.long, self.x + 4, self.y + 6)
    if self.code.hasValue then
        fill(231, 227, 227, 255)
        ellipse(self.x + 100, self.y + 16, 20)
        ellipse(self.x + 115, self.y + 16, 20)
        rect(self.x + 100, self.y + 6, self.x + 115, self.y + 26)
        fill(47, 47, 47, 255)
        text("1", self.x + 110, self.y + 6)
    end
end
function Token:touched(touch)
    if touch.x >= self.x and touch.x <= self.x + 130 and
    touch.y >= self.y and touch.y <= self.y + 30 then
        return true
    end
    return false
end
TokenTray = class()
function TokenTray:init(x, y)
    -- you can accept and set parameters here
    self.x = x
    self.y = y
    self.frame = Frame(x, y, x + 200, y + 850)
    self.selected = 0
    self.tokens = {}
    self.tokens[1] = Token(codes[1], 30, 0) 
    self.tokens[2] = Token(codes[2], 30, 0)
    self.tokens[3] = Token(codes[3], 30, 0)
    self.tokens[4] = Token(codes[4], 30, 0)
    self.tokens[5] = Token(codes[5], 30, 0)
    self.tokens[6] = Token(codes[6], 30, 0)
    self.tokens[7] = Token(codes[7], 30, 0)
    self.tokens[8] = Token(codes[8], 30, 0)
    self.tokens[9] = Token(codes[9], 30, 0)
    for i = 1,9 do
        self.tokens[i].y = self.frame:height() - i * 80 - 10
    end
end
function TokenTray:draw()
    pushStyle()
    pushMatrix()
    fill(177, 144, 62, 255)
    stroke(135, 122, 40, 255)
    strokeWidth(5)
    self.frame:draw()
    fill(127, 127, 127, 219)
    noStroke()
    translate(self.x, self.y)
    rect(4, self.frame:height() - 40, 
    self.frame:width() - 4, self.frame:height() - 4)
    fill(252, 252, 252, 255)
    font("Courier")
    fontSize(16)
    text("CHIPS", 10, self.frame:height() - 30)
    for i = 1, table.maxn(self.tokens) do
       self.tokens[i]:draw()
    end
    popMatrix()
    popStyle()
end
function TokenTray:touched(touch)
    t = Ttouch(touch)
    self.selected = 0
    t:translate(self.x, self.y)
    for i = 1,9 do
        if self.tokens[i]:touched(t) then
            self.selected = i
            return true
            --sound(SOUND_SHOOT, 773)
        end
    end
    return false
end
Ttouch = class()
-- Translatable Touch 
-- ver. 1.0
-- maps fields of a touch but is easily modified.
-- ====================.
function Ttouch:init(touch)
    self.x = touch.x
    self.y = touch.y
    self.state = touch.state
    self.prevX = touch.prevX
    self.prevY = touch.prevY
    self.deltaX = touch.deltaX
    self.deltaY = touch.deltaY
    self.id = touch.id
    self.tapCount = touch.tapCount
    self.timer = 0
end
function Ttouch:translate(x, y)
    self.x = self.x - x
    self.y = self.y - y
end
VColorSlider = class()
function VColorSlider:init(x, y)
    self.frame = Frame(x - 30, y - 328, x + 60, y + 56)
    self.previousY = 0
    self.pots = {}
    for i = 0, 15 do
        self.pots[i + 1] = Frame(self.frame.left, 
        self.frame.top - i * 24 - 24,
        self.frame.right, self.frame.top - i * 24)
    end
    self.selected = 0
end
function VColorSlider:draw()
    pushStyle()
    fill(143, 158, 166, 255)
    stroke(25, 25, 25, 255)
    strokeWidth(1)
    self.frame:draw()
    for i = 1, 16 do
        fill(colors[i])
        self.pots[i]:draw()
    end
    popStyle()
end
function VColorSlider:touched(touch)
    if self.frame:touched(touch) then
        for i = 1, 16 do
            self.selected = 0
            if self.pots[i]:touched(touch) then
                strokeWidth(3)
                stroke(106, 130, 155, 255)
                self.selected = i
                fill(colors[i])
                self.pots[i]:draw()
                return true
            end
        end
    end
    return false
end
Vslider = class()
function Vslider:init(x, y, v)
    self.frame = Frame(x - 30, y - 200, x + 30, y + 60)
    self.value = v
    self.previousY = 0
end
function Vslider:draw()
    pushStyle()
    fill(143, 158, 166, 255)
    stroke(25, 25, 25, 255)
    strokeWidth(1)
    self.frame:draw()
    fill(231, 227, 227, 255)
    noStroke()
    ellipse(self.frame.left + 20, self.frame.top - 15, 25)
    ellipse(self.frame.right - 20, self.frame.top - 15, 25)
    rect(self.frame.left + 20, self.frame.top - 3, 
    self.frame.right - 20, self.frame.top - 27)
    fill(23, 23, 23, 255)
    textMode(CENTER)
    fontSize(18)
    text(self.value, self.frame.left + 30, self.frame.top - 15)
    strokeWidth(2)
    line(self.frame:midX(), self.frame.bottom + 10,
    self.frame:midX(), self.frame.top - 50)
    stroke(207, 207, 207, 255)
    line(self.frame:midX() + 4, self.frame.bottom + 10,
    self.frame:midX() + 4, self.frame.top - 50)
    line(self.frame:midX() - 4, self.frame.bottom + 10,
    self.frame:midX() - 4, self.frame.top - 50)
    popStyle()
end
function Vslider:touched(touch)
    if self.frame:touched(touch) then
        self.value = math.floor((self.frame.top - 50 - touch.y) / 6)
        if self.value < 1 then self.value = 1 end
        if self.value > 30 then self.value = 30 end
        return true
    end
    return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment