1000行代码写小游戏(二)

玩家属性Table数据格式:

local userData = {
    floor = 1,      -- 楼层
    level = 0,      -- 等级
    curExp = 0,     -- 当前经验
    maxExp = 0,     -- 升级经验
    money = 0,      -- 金钱
    ironKey = 0,    -- 铁钥匙
    silverKey = 0,  -- 银钥匙
    goldKey = 0,    -- 金钥匙
    skillDot = 0,   -- 升级点
    curActive = 0,  -- 当前体力
    maxActive = 0,  -- 体力
    curHp = 0,      -- 当前生命
    maxHp = 0,      -- 生命
    recover = 0,    -- 恢复
    atk = 0,        -- 攻击
    def = 0,        -- 防御
    collection = {  -- 收藏
        {letter = "S", num = 0},
        {letter = "H", num = 0},
        {letter = "O", num = 0},
        {letter = "W", num = 0},
        {letter = "E", num = 0},
        {letter = "R", num = 0},
    },
}
可以将矿和敌人的状态存储在本地,下次加载的时候读取上次玩的数据,玩家数据也可以在本地存储,便于存档

其他游戏数据:

local BarMaxNum = {
    HP = 1000,
    ACTIVE = 500,
    RECOVER = 200,
    ATK = 200,
    DEF = 200,
    KEY = 99,
    COLLECT = 1,
}
local posX, posX2, posY, gap = 150, 320, sz.height - 13, 15
local barSize = CCSizeMake(120,12)

初始化每个等级的玩家数据和更新玩家经验如下:

-- 初始化固定等级的最大值
function MineSecretDialog:initUserData(level)
    userData.level = level
    userData.maxExp = getUserMaxExp(level)
    if level == 1 then
        local initBattleData = getInitBattleData()
        userData.curActive = initBattleData.active
        userData.maxActive = initBattleData.active
        userData.curHp = initBattleData.hp
        userData.maxHp = initBattleData.hp
        userData.recover = initBattleData.recover
        userData.atk = initBattleData.atk
        userData.def = initBattleData.def
    end
end
-- 更新等级,经验条和升级点
function MineSecretDialog:addUserExp(addExp, addSkillDot, isInit)
    addExp = addExp or 0
    addSkillDot = addSkillDot or 0
    isInit = isInit or false

    local updateLevel, updateSkillDot, updateExp = false, false, false
    if addExp > 0 then
        if userData.curExp + addExp >= userData.maxExp then
            self:updateUserLevel(addExp)
            updateLevel, updateSkillDot = true, true
        else
            userData.curExp = userData.curExp + addExp
        end
        updateExp = true
    end
    if addSkillDot ~= 0 then
        userData.skillDot = math.max(0, userData.skillDot + addSkillDot)
        updateSkillDot = true
    end

    if updateLevel or isInit then AddLabel( userData.floor.." F    ".."LV. "..userData.level, ccp(48, sz.height - 16), self.m_bgKuang, Tag.LevelLabel, H_Font_Btn_Cancle_Stroke ) end
    if updateSkillDot or isInit then AddLabel( userData.skillDot, ccp(posX2 + 162, posY), self.m_bgKuang, Tag.SkillDotLabel, ColorList[1] ) end
    if updateExp or isInit then
        self.m_expBar:setScaleX(userData.curExp/userData.maxExp)
        AddLabel( userData.curExp.."/"..userData.maxExp, ccp(posX + 5, posY), self.m_bgKuang, Tag.ExpNumLabel, ColorList[1], ccp(0, 0.5) )
    end
end
-- 计算增加的经验能够连升几级
function MineSecretDialog:updateUserLevel(addExp)
    userData.curExp = userData.curExp + addExp - userData.maxExp
    self:initUserData(userData.level + 1)
    userData.skillDot = userData.skillDot + getLevelUpAddSkillDot(userData.level)
    if userData.curExp >= userData.maxExp then
        self:updateUserLevel(0)
    end
end

你可能感兴趣的:(Lua)