PokerChips Project: Classes and Inheritance

So I think I’m using touch.id successfully now, but have a problem in my touched functions in Player and Sprite. I’m trying to do a touch transform inside Player:touched, but it only works for Player 1. I suspect here is where I might need a matrix to get the job done.

https://github.com/interactivenyc/SpriteHandling

Matrix math still hurts my head. I think if I see it a couple more times, I might get the hang of it. Anyone want to give me some advice here?

Always appreciated!

Why are you bothering with all that stuff, when the touch position tells you where to draw the sprite?

ie you should be able to translate to the current touch position, rotate, and draw.

And although matrix math hurt my head for a long time, it’s not as bad as it looks. It is a very neat way of managing translations and rotations. When you translate or rotate, Codea adjusts its internal matrix, and then when you draw, it multiplies all your vertex positions by this matrix to get the correct place to draw.

@interactivenyc Here’s some advice. Starting with my code above, I created a working program. Each player moves their coins to the pot independently of each other. Their value and count are subtracted from each players totals. Then doing a swipe from the center to an edge, the coins in the pot are added into that players total. None of the code uses matrix or anything complicated. So if you’re using matrix and having trouble, rethink what you’re doing. I’ll share the code with you if and when you want it, but for now I’ll just say you don’t need matrix code or anything complicated.

I agree you don’t need matrix code for this

However, if you ever get to more complex graphics, you will need it. That’s why I provided explanatory links.

@dave1707 - I’d love to see your code. I’m still trying to find a way that makes sense to me to handle my hitTests and restrict them by Player. Thanks!

@interactivenyc Here’s the code. Slide the coins onto the green area. Touch the center and slide your finger off the green area towards the winning player.

displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)

function setup()
    rectMode(CENTER)
    w1,h1=230,250
    w2,h2=650,360
    w3,h3=530,775
    w4,h4=120,660
    pt=pot()
    tab={}
    table.insert(tab,coin(w1,h1,"penny",0,1))
    table.insert(tab,coin(w1+100,h1,"nickel",0,5))
    table.insert(tab,coin(w1+200,h1,"dime",0,10))
    table.insert(tab,coin(w1+300,h1,"quarter",0,25))
            
    table.insert(tab,coin(w2,h2,"penny",90,1))
    table.insert(tab,coin(w2,h2+100,"nickel",90,5))
    table.insert(tab,coin(w2,h2+200,"dime",90,10))
    table.insert(tab,coin(w2,h2+300,"quarter",90,25))
            
    table.insert(tab,coin(w3,h3,"penny",180,1))
    table.insert(tab,coin(w3-100,h3,"nickel",180,5))
    table.insert(tab,coin(w3-200,h3,"dime",180,10))
    table.insert(tab,coin(w3-300,h3,"quarter",180,25))
            
    table.insert(tab,coin(w4,h4,"penny",270,1))
    table.insert(tab,coin(w4,h4-100,"nickel",270,5)) 
    table.insert(tab,coin(w4,h4-200,"dime",270,10))
    table.insert(tab,coin(w4,h4-300,"quarter",270,25))    
end

function draw()
    background(40, 40, 50)
    fill(0,255,0)
    rect(WIDTH/2,HEIGHT/2,450,450)
    for a,b in pairs(tab) do
        b:draw()
    end
    pt:draw()
end

function touched(t)
    for a,b in pairs(tab) do
        b:touched(t)
    end  
    pt:touched(t)
end

pot=class()

pot.coins={}

function pot:draw()
    for a,b in pairs(self.coins) do
        pushMatrix()
        fill(255)
        translate(b.x,b.y)
        rotate(b.a)
        stroke(0, 124, 255, 255)
        strokeWidth(2)
        ellipse(0,0,80)
        fill(255,0,0)
        text(b.n,0,0)
        popMatrix()
    end
end

function pot:touched(t)
    if t.state==BEGAN then
        if t.x>WIDTH/2-100 and t.x<WIDTH/2+100 and
            t.y>HEIGHT/2-100 and t.y<HEIGHT/2+100 then
            addPot=true
        end
    end
    if t.state==ENDED and addPot then
        addPot=false
        if t.y<HEIGHT/2-225 then
            pot:add(1)
        elseif t.x>WIDTH/2+225 then
            pot:add(5)
        elseif t.y>HEIGHT/2+225 then
            pot:add(9)
        elseif t.x<WIDTH/2-225 then
            pot:add(13)
        end
    end   
end

function pot:add(pos)
    for a,b in pairs(self.coins) do
        if b.v==1 then
            tab[pos].c=tab[pos].c+1
        end
        if b.v==5 then
            tab[pos+1].c=tab[pos+1].c+1
        end
        if b.v==10 then
            tab[pos+2].c=tab[pos+2].c+1
        end
        if b.v==25 then
            tab[pos+3].c=tab[pos+3].c+1
        end
    end  
    self.coins={} 
end

coin=class()

function coin:init(x,y,n,a,v)
    self.x=x
    self.y=y
    self.ox=x
    self.oy=y
    self.c=10
    self.n=n    -- name
    self.a=a    -- rotate angle
    self.v=v    -- coin values
    self.rem=false
    self.sub=1
end

function coin:draw()    
    pushMatrix()
    fill(255)
    translate(self.x,self.y)
    rotate(self.a)
    stroke(0, 124, 255, 255)
    strokeWidth(2)
    ellipse(0,0,80)
    fill(255,0,0)
    text(self.n,0,0)
    popMatrix()
    
    pushMatrix()
    translate(self.ox,self.oy)
    rotate(self.a)
    fill(255)
    ellipse(0,0,80)
    fill(255,0,0)
    text(self.n,0,0)
    text(self.c,0,-60)
    text(self.v*self.c,0,-80)
    popMatrix()
end

function coin:touched(t)
    if t.x>self.ox-40 and t.x<self.ox+40 and 
            t.y>self.oy-40 and t.y<self.oy+40 and self.c>0 then
        if t.state==BEGAN then
            self.id=t.id
        end
    end
    if t.state==MOVING and self.id==t.id then
        self.x=t.x
        self.y=t.y
    end
    if t.state==ENDED and self.id==t.id then
        self.id=0
        if self.c>0 then
            self.c=self.c-1
        end
        table.insert(pt.coins,{x=self.x,y=self.y,n=self.n,a=self.a,v=self.v})
        self.x=self.ox
        self.y=self.oy
    end
end

Thanks Dave!!!

Super nice example there, Dave. I’ve been able to work with it, and get what’s going on. I’ve added the coin images and sizes, and have styled the look with a background and colored the pot. Thanks again for taking the time to help me along in this process!

https://github.com/interactivenyc/DavePoker3

@dave1707 Can you explain what’s going on inside your pot:touched and pot:add routines? It seems the way it’s working right now, the pot:add function is only being called when dragging a coin that’s already inside the pot to the middle of the screen, but not when a coin is dragged from the player’s coins into the pot. Was this intentional, or is it not working quite as expected? It’s also not clear to me why you’re adding 1,5,9 and 13 as values.

TIA!

@interactivenyc The pot functions are used only when there are coins in the pot (green area). When you drag from the center out to one of the players, any coins in the pot area are added to that players total. The 1,5,9,13 are used so I add the coins to the correct table position for the selected player. Since there are 4 coins per player, player 1 gets table positions 1,2,3,4, player 2 gets 5,6,7,8, player 3 gets 9,10,11,12 and player 4 gets 13,14,15,16. Any other questions, just ask.

Thanks for the clarification!