1000行代码写小游戏(五)

主角的一些属性更新和展示都已经做完了,下面主要做玩法方面的,添加小怪、矿和系统,并增加点击逻辑:

-- 增加矿,敌人和系统
function MineSecretDialog:addMineAndEnemy()
    local bonusList = getBonusListInfo()
    for i,v in ipairs(bonusList) do
        if v.types == BonusType.MINE then
            self:initMineStatus(v)
        elseif v.types == BonusType.ENEMY then
            self:initEnemyStatus(v)
        elseif v.types == BonusType.SYSTEM then
            self:initSystemStatus(v)
        elseif v.types == BonusType.BOSS then

        end
    end
end

-- 检查对应种类的钥匙是否足够,不足进行提示,足够扣除钥匙、更新数量并解锁
function MineSecretDialog:checkKeyEnough(keyId, needNum)
    local keyList = {"ironKey", "silverKey", "goldKey"}
    local keyStr = {"蓝钥匙", "橙钥匙", "紫钥匙"}
    local costNum = {0, 0, 0}
    keyId = keyId or 1
    keyId = math.max(1, math.min(3, keyId))
    needNum = needNum or 1
    if (userData[keyList[keyId]] - needNum) >= 0 then
        costNum[keyId] = 0 - needNum
        self:updateUserKey(costNum[1], costNum[2], costNum[3])
        return true
    else
        game.fleetingLabelManager:pushLabel( keyStr[keyId].."不足", self, "error" )
        return false
    end
end
-- 随机生成传入概率数的随机结果
function MineSecretDialog:getRandomResult(percentage)
    math.randomseed(tostring(os.time()):reverse():sub(1, 6))  
    local random = math.random(1, 100)
    if random <= percentage then
        return true
    else
        return false
    end
end
-- 随机生成两个数之间的数
function MineSecretDialog:getRandomNum(from, to)
    math.randomseed(tostring(os.time()):reverse():sub(1, 6))  
    local random = math.random(from*10, to*10)/10
    return random
end

初始化矿的基本信息和状态,增加点击逻辑,采矿完成逻辑等:

-- 初始化矿的信息和状态
function MineSecretDialog:initMineStatus(v)
    local times = 0
    local haveRunAction = false 
    local itemSize = CCSizeMake(v.width, v.height)
    local mineData = getMineActivendRecover(v.width, v.height)

    local sprite = CCScale9Sprite:create("p_purecolor_gray.png", CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6))
    sprite:setContentSize(itemSize)
    local mineItem = CCMenuItemSprite:create( sprite, sprite )
    self.m_menu:addChild(mineItem, 1)
    mineItem:setPosition(v.pos)
    mineItem:setAnchorPoint(ccp(0, 1))
    
    local itemBar = AddScale9Sprite("p_purecolor_blue.png", ccp(0, v.height/2), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), itemSize, mineItem)
    itemBar:setAnchorPoint(ccp(0, 0.5))
    if v.locked then AddLabel( "锁", ccp(v.width/2, v.height/2), mineItem, Tag.BonusLockLabel, ColorLock[v.key or 1] ) end
    if v.digged == false then 
        itemBar:setScaleX(0)
        AddLabel( "0%", ccp(3, v.height/2), mineItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) 
    end

    mineItem:registerScriptTapHandler(function ()
        game.fleetingLabelManager:pushLabel( "posX "..v.pos.x.." posY "..v.pos.y, self )
        cclog("the mine data is",mineData.life,v.digged,v.curlife)
        if v.locked then
            if self:checkKeyEnough(v.key) then
                v.locked = false
                game.fleetingLabelManager:pushLabel( "解锁成功", self )
                mineItem:removeChildByTag( Tag.BonusLockLabel, true )
            end
        else
            if v.digged and self.m_timer - times < 0.6 then
                return
            elseif v.digged then
                times = self.m_timer
            end
            -- 判断剩余的体力是否足够此次点击消耗
            local cost = mineData.clickCost
            if v.digged == false then cost = math.ceil(math.min(mineData.life - v.curlife, mineData.clickCost)) end
            if userData.curActive < cost then
                game.fleetingLabelManager:pushLabel( "体力不足", self, "error" )
                return
            end
            self:updateUserActive(0 - cost)

            if v.digged then
                -- 收获金币,并有1%概率获得收藏品
                local collectPercent = self:getRandomNum(0.8, 1.2)
                self:updateUserMoney(math.floor((mineData.clickMoney)*collectPercent))
                if v.collect and self:getRandomResult(1) then self:updateUserCollection(v.collect) end
            else
                v.curlife = v.curlife + cost
                itemBar:setScaleX(v.curlife/mineData.life)
                AddLabel( math.floor(100*v.curlife/mineData.life).."%", ccp(3, v.height/2), mineItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) 
               
                if v.curlife == mineData.life then
                    v.digged = true
                    mineItem:stopAllActions()
                    mineItem:removeChildByTag( Tag.BonusNumLabel, true )
                    -- 开发完成,开始收获金币、经验,并有20%概率获得收藏品
                    self:addUserExp(mineData.digExp)
                    self:updateUserMoney(mineData.digMoney)
                    if v.collect and self:getRandomResult(20) then self:updateUserCollection(v.collect) end
                else
                    if haveRunAction == false then
                        haveRunAction = true
                        local array = CCArray:create()
                        array:addObject(CCDelayTime:create(0.2))                    
                        array:addObject(CCCallFunc:create(function ()
                            if v.curlife > 0 then 
                                v.curlife = math.max(0, v.curlife - mineData.recover) 
                                itemBar:setScaleX(v.curlife/mineData.life)
                                AddLabel( math.floor(100*v.curlife/mineData.life).."%", ccp(3, v.height/2), mineItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) 
                            end
                            if v.curlife == 0 then
                                haveRunAction = false
                                mineItem:stopAllActions()
                            end
                        end))   
                        mineItem:runAction(CCRepeatForever:create(CCSequence:create(array)))                        
                    end
                end
            end
        end
    end)
end


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值