it works and before I go further, let's commit

This commit is contained in:
Paul Liverman
2015-12-05 23:08:57 -08:00
parent a5c7aae494
commit 1dc98d80d3
4 changed files with 462 additions and 33 deletions

View File

@@ -1,20 +1,19 @@
local set = setmetatable
local class = require "lib.middleclass"
local lg = love.graphics
local cornerOffset = 5
local Card = {}
local Card = class("Card")
function Card.initialize(suit, rank)
local self = {}
Card.static.width = 64*2
Card.static.height = 89*2
function Card:initialize(suit, rank)
self.suit = suit or "!"
self.rank = rank or "#"
self.x = 0
self.y = 0
self.r = 0
self.face = "down" --or "up"
set(self, {__index = Card})
return self
end
function Card:draw(face, x, y, r)
@@ -23,11 +22,51 @@ function Card:draw(face, x, y, r)
if not y then y = self.y end
if not r then r = self.r end
lg.push()
lg.translate(x, y)
lg.rotate(r)
lg.translate(x, y)
--TODO if Joker, no suit!
lg.print(self.rank .. " of " .. self.suit)
lg.setColor(0, 0, 0, 255)
lg.rectangle("fill", -Card.static.width/2, -Card.static.height/2, Card.static.width, Card.static.height)
lg.setColor(255, 255, 255, 255)
lg.rectangle("line", -Card.static.width/2, -Card.static.height/2, Card.static.width, Card.static.height)
if face == "up" then
if self.rank == "Joker" then
lg.print("Joker", cornerOffset - Card.static.width/2, cornerOffset - Card.static.height/2)
-- I wanted this to be a symbol for a suit, but the Joker doesn't have one!
lg.print("#") --used to be "J"
else
lg.print(self.rank .. " of " .. self.suit, cornerOffset - Card.static.width/2, cornerOffset - height/2)
-- I wanted these to be symbols for suits, but for some reason I made them ranks
--[[
if self.rank == "Ace" then
lg.print("A")
elseif self.rank == "Jack" then
lg.print("J")
elseif self.rank == "Queen" then
lg.print("Q")
elseif self.rank == "King" then
lg.print("K")
else
lg.print(self.rank)
end
--]]
if self.suit == "Clubs" then
lg.print("")
elseif self.suit == "Diamonds" then
lg.print("")
elseif self.suit == "Hearts" then
lg.print("")
elseif self.suit == "Spades" then
lg.print("")
else
lg.print("!")
end
end
end
lg.pop()
end
function Card:moveTo(x, y, r)
@@ -44,5 +83,4 @@ function Card:flip()
end
end
set(Card, {__call = Card.initialize})
return Card

View File

@@ -1,34 +1,42 @@
local set = setmetatable
local class = require "lib.middleclass"
local insert = table.insert
local remove = table.remove
local random = math.random
local floor = math.floor
local ceil = math.ceil
local lg = love.graphics
local Deck = {}
local Deck = class("Deck")
local inspect = require "lib.inspect" --NOTE DEBUG
Deck.static.width = 64*2
Deck.static.height = 89*2
function Deck.initialize(cards)
local self = {}
print(inspect(cards)) --NOTE DEBUG
function Deck:initialize(cards)
self.cards = cards or {}
self.x = 0
self.y = 0
self.r = 0
self.face = "down" --or "up"
set(self, {__index = Deck})
return self
end
function Deck:draw()
local thickness = floor(#self.cards / 3)
print(inspect(self.cards)) --NOTE DEBUG
self.cards[#self.cards]:draw(self.face, self.x, self.y, self.r)
function Deck:draw(face, x, y, r)
if not face then face = self.face end
if not x then x = self.x end
if not y then y = self.y end
if not r then r = self.r end
--TODO draw the extra lines
lg.push()
lg.translate(x, y)
lg.rotate(r)
lg.setColor(255, 255, 255, 255)
for i=ceil(#self.cards / 3), 1, -1 do --every 3 cards is 1 pixel of thickness to the deck, rounded up
lg.rectangle("line", i - Deck.static.width/2, i - Deck.static.height/2, Deck.static.width, Deck.static.height)
end
lg.pop()
self.cards[#self.cards]:draw(face, x, y, r)
end
function Deck:moveTo(x, y, r)
@@ -37,6 +45,14 @@ function Deck:moveTo(x, y, r)
self.r = r or self.r
end
function Deck:flip()
if self.face == "down" then
self.face = "up"
else
self.face = "down"
end
end
function Deck:shuffleCards()
local new = {}
@@ -47,6 +63,10 @@ function Deck:shuffleCards()
self.cards = new
end
function Deck:shuffleIn(card) --TODO make capable of handling multiple cards
--
end
function Deck:drawCards(count)
if count and (count > 1) then
local new = {}
@@ -83,5 +103,4 @@ function Deck:placeCardsUnder(cards)
end
end
set(Deck, {__call = Deck.initialize})
return Deck

203
src/lib/middleclass.lua Normal file
View File

@@ -0,0 +1,203 @@
local middleclass = {
_VERSION = 'middleclass v3.2.0',
_DESCRIPTION = 'Object Orientation for Lua',
_URL = 'https://github.com/kikito/middleclass',
_LICENSE = [[
MIT LICENSE
Copyright (c) 2011 Enrique García Cota
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
}
local _metamethods = {}
for m in ([[ add band bor bxor bnot call concat div eq
gc ipairs idiv le len lt metatable mod mode
mul pairs pow shl shr sub tostring unm ]]):gmatch("%S+") do
_metamethods['__' .. m] = true
end
local function _setClassDictionariesMetatables(aClass)
local dict = aClass.__instanceDict
dict.__index = dict
local super = aClass.super
if super then
local superStatic = super.static
setmetatable(dict, { __index = super.__instanceDict })
setmetatable(aClass.static, { __index = function(_,k) return rawget(dict,k) or superStatic[k] end })
else
setmetatable(aClass.static, { __index = function(_,k) return dict[k] end })
end
end
local function _propagateMetamethod(aClass, name, f)
for subclass in pairs(aClass.subclasses) do
if not subclass.__metamethods[name] then
subclass.__instanceDict[name] = f
_propagateMetamethod(subclass, name, f)
end
end
end
local function _updateClassDict(aClass, key, value)
if _metamethods[key] then
if value == nil then
aClass.__metamethods[key] = nil
if aClass.super then
value = aClass.super.__instanceDict[key]
end
else
aClass.__metamethods[key] = true
end
_propagateMetamethod(aClass, key, value)
end
aClass.__instanceDict[key] = value
end
local function _setClassMetatable(aClass)
setmetatable(aClass, {
__tostring = function() return "class " .. aClass.name end,
__index = aClass.static,
__newindex = _updateClassDict,
__call = function(self, ...) return self:new(...) end
})
end
local function _createClass(name, super)
local aClass = { name = name, super = super, static = {}, __mixins = {}, __instanceDict = {}, __metamethods = {} }
aClass.subclasses = setmetatable({}, {__mode = "k"})
_setClassDictionariesMetatables(aClass)
_setClassMetatable(aClass)
return aClass
end
local function _setSubclassMetamethods(aClass, subclass)
for m in pairs(_metamethods) do
subclass.__instanceDict[m] = aClass.__instanceDict[m]
end
end
local function _setDefaultInitializeMethod(aClass, super)
aClass.initialize = function(instance, ...)
return super.initialize(instance, ...)
end
end
local function _includeMixin(aClass, mixin)
assert(type(mixin)=='table', "mixin must be a table")
for name,method in pairs(mixin) do
if name ~= "included" and name ~= "static" then aClass[name] = method end
end
if mixin.static then
for name,method in pairs(mixin.static) do
aClass.static[name] = method
end
end
if type(mixin.included)=="function" then mixin:included(aClass) end
aClass.__mixins[mixin] = true
end
local Object = _createClass("Object", nil)
function Object.static:allocate()
assert(type(self) == 'table', "Make sure that you are using 'Class:allocate' instead of 'Class.allocate'")
return setmetatable({ class = self }, self.__instanceDict)
end
function Object.static:new(...)
local instance = self:allocate()
instance:initialize(...)
return instance
end
function Object.static:subclass(name)
assert(type(self) == 'table', "Make sure that you are using 'Class:subclass' instead of 'Class.subclass'")
assert(type(name) == "string", "You must provide a name(string) for your class")
local subclass = _createClass(name, self)
_setSubclassMetamethods(self, subclass)
_setDefaultInitializeMethod(subclass, self)
self.subclasses[subclass] = true
self:subclassed(subclass)
return subclass
end
function Object.static:subclassed(other) end
function Object.static:isSubclassOf(other)
return type(other) == 'table' and
type(self) == 'table' and
type(self.super) == 'table' and
( self.super == other or
type(self.super.isSubclassOf) == 'function' and
self.super:isSubclassOf(other)
)
end
function Object.static:include( ... )
assert(type(self) == 'table', "Make sure you that you are using 'Class:include' instead of 'Class.include'")
for _,mixin in ipairs({...}) do _includeMixin(self, mixin) end
return self
end
function Object.static:includes(mixin)
return type(mixin) == 'table' and
type(self) == 'table' and
type(self.__mixins) == 'table' and
( self.__mixins[mixin] or
type(self.super) == 'table' and
type(self.super.includes) == 'function' and
self.super:includes(mixin)
)
end
function Object:initialize() end
function Object:__tostring() return "instance of " .. tostring(self.class) end
function Object:isInstanceOf(aClass)
return type(self) == 'table' and
type(self.class) == 'table' and
type(aClass) == 'table' and
( aClass == self.class or
type(aClass.isSubclassOf) == 'function' and
self.class:isSubclassOf(aClass)
)
end
function middleclass.class(name, super, ...)
super = super or Object
return super:subclass(name, ...)
end
middleclass.Object = Object
setmetatable(middleclass, { __call = function(_, ...) return middleclass.class(...) end })
return middleclass

View File

@@ -2,10 +2,11 @@ math.randomseed(os.time())
local Deck = require "Deck"
local Card = require "Card"
local insert = table.insert
local lg = love.graphics
local lm = love.mouse
local items = {}
local inspect = require "lib.inspect" --NOTE DEBUG
local function makeDeck(jokers)
local cards = {}
@@ -23,22 +24,190 @@ local function makeDeck(jokers)
insert(cards, Card("", "Joker"))
end
print(inspect(cards)) --NOTE DEBUG
print(inspect(cards[1].suit))
return Deck(cards)
end
function love.load()
insert(items, makeDeck(true))
items[1]:shuffleCards()
items[1]:moveTo(love.graphics.getWidth()/2, love.graphics.getHeight()/2)
items[1]:moveTo(lg.getWidth()/2, lg.getHeight()/2)
items[2] = Card("", "Joker")
items[2]:moveTo(100, 100)
items[2]:flip()
end
local holding = false -- we might be holding something!
-- helper function to see if we are hovering over something
local function isOnItem(x, y, item)
if (x > item.x - Card.static.width/2)
and (x < item.x + Card.static.width/2)
and (y > item.y - Card.static.height/2)
and (y < item.y + Card.static.height/2) then
return true
else
return false
end
end
function love.draw()
for i=1,#items do
items[i]:draw()
end
--lg.line(lg.getWidth()/2, 0, lg.getWidth()/2, lg.getHeight())
--lg.line(0, 0, lg.getWidth(), lg.getHeight())
--lg.line(lg.getWidth(), 0, 0, lg.getHeight())
if holding then
--draw holding where the mouse is
local x, y = lm.getPosition()
holding:draw(nil, x, y) --nil face, draw however it is
end
--lg.print("Left click to move cards/decks, right click to flip a card (or all cards in a deck). Scroll to shuffle decks.", 2, lg.getHeight() - 14)
--All cards in a deck are facing the same way automatically.
if not holding then
--not holading anything
lg.print("Left click to grab a card or deck. Scroll over a deck to shuffle it. Right click to flip a card or the cards in a deck.", 2, lg.getHeight() - 14)
else
local hovering = false
for i=#items,1,-1 do
local x, y = lm.getPosition()
if isOnItem(x, y, items[i]) then
if items[i]:isInstanceOf(Card) then
hovering = "Card"
else
hovering = "Deck"
end
break
end
end
if holding:isInstanceOf(Card) then
if hovering == "Card" then
--card on card
lg.print("Left click to form a deck with this card and the one under it. Right click to flip card.", 2, lg.getHeight() - 14)
elseif hovering == "Deck" then
--card on deck
lg.print("Left click to shuffle it into the deck. Scroll up to place card on top of deck, scroll down to place card on bottom of deck. Right click to flip card.", 2, lg.getHeight() - 14)
else
--card over nothing
lg.print("Left click to place card. Right click to flip card.", 2, lg.getHeight() - 14)
end
else
if hovering == "Card" then
--deck over card
lg.print("Left click to place deck (will not shuffle card underneath into the deck). Right click to flip cards in the deck.", 2, lg.getHeight() - 14)
elseif hovering == "Deck" then
--deck over deck
lg.print("Left click to place deck (will not interact with deck underneath). Scroll up to add this deck on top, scroll down to add this deck on bottom. Right click to flip cards in this deck.", 2, lg.getHeight() - 14)
else
--deck over nothing
lg.print("Left click to place deck. Scroll to shuffle the deck. Right click to flip cards in the deck.", 2, lg.getHeight() - 14)
end
end
end
end
function love.mousepressed(x, y, button)
--[[ ORIGINAL TEXT, NOT ACCURATE
Left click will grab a card or deck.
While holding a card: Left click will place it (as long as the mouse is not over a deck).
While holding a card over a deck: Scroll up to place it on top of the deck, scroll down to place it on the bottom of the deck. Right click to shuffle it into the deck.
While holding a deck: Left click will place it (as long as the mouse is not over a deck).
While holding a deck over a deck: Scroll up to place it on top of the deck, scroll down to place it on the bottom of the deck. Right click to shuffle the decks together.
While NOT holding anything: Right click a card to flip it, or right click a deck to flip the cards in it (does not flip order of cards). Scroll over a deck to shuffle it.
All cards in a deck are facing the same way automatically.
--]]
if button == "l" then
if not holding then
for i=#items,1,-1 do
if isOnItem(x, y, items[i]) then
holding = table.remove(items, i)
break
end
end
elseif holding:isInstanceOf(Deck) then
holding:moveTo(x, y)
insert(items, holding)
holding = false
elseif holding:isInstanceOf(Card) then --redundant checks woo
local item = false
for i=#items,1,-1 do
if isOnItem(x, y, items[i]) then
item = i
end
end
if not item then
holding:moveTo(x, y)
insert(items, holding)
holding = false
elseif items[item]:isInstanceOf(Card) then
local card = table.remove(items, item)
local deck = Deck({card, holding})
insert(items, deck)
holding = false
elseif items[item]:isInstanceOf(Deck) then
--TODO shuffle it into the deck
end
end
--[[
--card on deck
"Scroll up to place card on top of deck, scroll down to place card on bottom of deck."
--deck over deck
"Scroll up to add this deck on top, scroll down to add this deck on bottom."
--deck over nothing
"Scroll to shuffle the deck."
]]
elseif button == "wu" then --WU AND WD ARE ALMOST IDENTICAL, COLLAPSE THEM INTO ONE WHERE POSSIBLE
if not holding then
for i=#items,1,-1 do --ABSTRACT THIS FOR, I DO IT TOO MUCH ?
if isOnItem(x, y, items[i]) and items[i]:isInstanceOf(Deck) then
items[i]:shuffleCards()
break
end
end
elseif holding:isInstanceOf(Card) then
--TODO easy
elseif holding:isInstanceOf(Deck) then
--TODO maybe harder
end
elseif button == "wd" then
if not holding then
for i=#items,1,-1 do
if isOnItem(x, y, items[i]) and items[i]:isInstanceOf(Deck) then
items[i]:shuffleCards()
break
end
end
elseif holding:isInstanceOf(Card) then
--TODO easy
elseif holding:isInstanceOf(Deck) then
--TODO maybe harder
end
elseif button == "r" then
if holding then
holding:flip()
else
for i=#items,1,-1 do
if isOnItem(x, y, items[i]) then
items[i]:flip()
break
end
end
end
end
end
function love.keypressed(key)
if key == "escape" then
love.event.quit()
end
end
-- ♣ ♦ ♥ ♠ A 2 3 4 5 6 7 8 9 10 J Q K Joker