unity3d + lua + 斗地主 系列 (1) 创建扑克对象

框架

根据 https://github.com/Tencent/xLua 的教程搭建的框架

Lua面向对象相关

lua 面向对象,我从云风大神那边拿了过来:
https://blog.codingnow.com/cloud/LuaOO

创建扑克类

---扑克类

Poker = class()

--- 扑克类

Poker = class()


function Poker:ctor(id, name, num)
	self.id = id or 0               --- 扑克id,这里主要是指 对应的图片资源的id
	self.name = name or ''          --- 扑克名称
	self.num = num or 0             --- 扑克的编码,一个整数,标识同一花色的扑克牌
end

function Poker:ctor(id, name, num, isPlay)
	self.id = id or 0               --- 扑克id
	self.name = name or ''          --- 扑克名称
	self.num = num or 0             --- 扑克的编码
	self.isPlay = isPlay or true    --- 是否打出去了
end

--- 获取扑克id , 扑克的唯一id
function Poker:getId()
	return self.id
end

--- 设置扑克id
function Poker:setId(id)
	self.id = id
end


---获取牌的名称
function Poker:getName()
	return self.name
end

--- 设置牌的名称
function Poker:setName(name)
	self.name = name
end

--- 获取扑克的编码
function Poker:getNum()
	return self.num
end

--- 设置扑克的编码
function Poker:setNum(num)
	self.num = num
end


--- 判断扑克是否打出去了, true表示打出去了, false 表示没有打出去
function Poker:getIsPlay()
	return self.isPlay
end

function Poker:setPlay(is_play)
	self.isPlay = is_play
end

使用也是很简单:
记得 require(‘class.class’) class 父类, 根据你自己存放的路径来写。
然后:

	require('game_thing.Poker')
	local poker = Poker.new(0, 'A', 1, true)
	print(poker:getName())

将会打印:
A

创建扑克精灵

我们首先来实现一个简单的功能

--- 扑克精灵类,即一张扑克的UI表现

PokerSprite = class()

local rect_transform
local transform

function PokerSprite:ctor(id, name, num)
	self.id = id or 0               --- 扑克id
	self.name = name or ''          --- 扑克名称
	self.num = num or 0             --- 扑克的编码

	self.image = nil

end

function PokerSprite:ctor(id, name, num, isPlay, isUp, isSelected)
	self.id = id or 0               --- 扑克id
	self.name = name or ''          --- 扑克名称
	self.num = num or 0             --- 扑克的编码
	self.isPlay = isPlay or true    --- 是否打出去了
	self.isUp = isUp or false       --- 扑克是否翻上来
	self.isSelected = isSelected or false --- 是否被选中

	self.image = nil

end




function PokerSprite:getId()
	return self.id
end

function PokerSprite:setId(id)
	self.id = id
end


function PokerSprite:getName()
	return self.name
end

function PokerSprite:setName(name)
	self.name = name
end

function PokerSprite:getNum()
	return self.num
end

function PokerSprite:setNum(num)
	self.num = num
end

--- 判断扑克是否打出去了, true表示打出去了, false 表示没有打出去
function PokerSprite:getIsPlay()
	return self.isPlay
end

function PokerSprite:setPlay(is_play)
	self.isPlay = is_play
end

--- 判断牌是否往上提,表示 想出这张牌
function PokerSprite:getIsUp()
	return self.isUp
end

--- true 表示往上抽牌,想出这张牌。 false 把牌抽回来,不想出这张牌
function PokerSprite:setUp(is_up)

	--如果扑克已经抽了出来,又调用了抽出来, 或者 扑克没有被抽出来,又调用了放回去 。 则什么都不做
	if ((self.isUp == true and is_up == true) or (self.isUp == false and is_up == false) )
	then
		return nil
	end

	self.isUp = is_up

    if (self.isUp == true)
	then
		transform.position = CS.UnityEngine.Vector3(transform.position.x, transform.position.y + 75, transform.position.z)
	else
		transform.position = CS.UnityEngine.Vector3(transform.position.x, transform.position.y - 75, transform.position.z)
	end
end


--- 判断牌是否被选择了
function PokerSprite:getIsSelected()
	return self.isSelected
end

--- 设置牌是否选中 true 表示选择了这张牌, false表示不选这张牌
function PokerSprite:setSelected(is_selected)
	self.isSelected = is_selected
end


function PokerSprite:start(image_url)
    ---local button = CS.UnityEngine.GameObject.Find('Canvas/Button'):GetComponent(typeof(CS.UnityEngine.UI.Button))
    ---button.onClick:AddListener(function ( )
    ---print('click me')
    
    ---end)

    self.image = CS.UnityEngine.GameObject.Find(image_url):GetComponent(typeof(CS.UnityEngine.UI.Image))
    
	---改变Image组件的宽高,使他跟牌的原始图片的宽高一致
	transform = self.image.transform
	rect_transform = transform:GetComponent(typeof(CS.UnityEngine.RectTransform))
	local newSizeDelta = CS.UnityEngine.Vector2(105, 150)
	rect_transform.sizeDelta = newSizeDelta


	--默认给一张翻的牌
	ResourceManager:LoadObject('Game/Doudizhu/res/poker/down', function( obj )
        self.image.sprite = obj:ToSprite()
    end)

	---self:changeImage()
end

function PokerSprite:changeImage()
	-- body

	string.format("Game/Doudizhu/res/poker/%d", self.id)

    ResourceManager:LoadObject(string.format("Game/Doudizhu/res/poker/%d", self.id), function( obj )
        self.image.sprite = obj:ToSprite()
    end)
end


function PokerSprite:turnUp()
	-- body
    self:changeImage()
end

function PokerSprite:turnDown()
	-- body
	ResourceManager:LoadObject('Game/Doudizhu/res/poker/down', function( obj )
        self.image.sprite = obj:ToSprite()
    end)
end

function PokerSprite:turn(isUp)
	-- body
    if (isUp == true)
	then
		self:turnUp()
	else
		self:turnDown()
	end
end

让我们实现一个简单的功能:

ui布局

在这里插入图片描述

  1. 点击扑克牌,扑克牌向上移动,模拟抽牌动作

BattlePanel 就是一个Panel

function BattlePanel.Init()
    ---
    LuaUtils.UI.BindClick(transform, 'Image', BattlePanel.OnClicked_StartBattle)
    -- DEBUG
    ---LuaUtils.UI.BindClick(transform, 'Image', BattlePanel.OnClicked_StartBattle)

    poker = Poker.new(1, 'K', 1, true)

    poker_sprite = PokerSprite.new(poker:getId(),
    poker:getName(), poker:getNum(), false, false, false)

    poker_sprite:start('Canvas/Panels/BattlePanel/Image')
    poker_sprite:turn(true)
    isUp = true
end

function BattlePanel.OnClicked_StartBattle()
    print('开始发牌')
    poker_sprite:setUp(isUp)
    isUp = not isUp
end

效果图

开始运行,没有点击扑克
没有点击扑克

点击扑克后的效果,往上移动了一截
点击扑克后的效果,往上移动了一截

©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值