• Quick Cocos2dx 初步战斗


    呵呵,图片先来一发:

    最近懒,很懒,连我自己都觉得有点可耻了。

    但是实在没有什么东西啊,温水煮青蛙的什么的。

    吐槽完成,以上。

    目前完成了简单的战斗部分,AI还需要抽出来。

    然后突然之间想到手游的帧频是60fps/s,然后就放弃了之前比较平滑的CCMoveTo,换成了每帧去计算坐标。

    还增加了掉血计算与表现,与死亡逻辑处理。

    虽然处理得不是很干净,但是这个是后续需要优化的部分。

    本次的主要用到的知识点:

    framework.scheduler

    framework.transition

    写这篇blog的目的是在于给自己一点动力,能够一步一个脚印去记录自己。

    主要更新的代码部分:

    GameUnit.lua:增加了死亡逻辑处理

    GameUnit.lua
      1 local GameUnit = class("GameUnit", function()
      2     return display.newNode()
      3 end)
      4 local scheduler = import("framework.scheduler")
      5 local Progress = import("app.views.Progress")
      6 
      7 function GameUnit:ctor(hero)
      8     -- body
      9     local clas = hero.class
     10     cc.EventProxy.new(hero, self)
     11         :addEventListener(clas.CHANGE_STATE_EVENT, handler(self, self.onStateChange))
     12         :addEventListener(clas.KILL_EVENT, handler(self, self.onKilled))
     13         :addEventListener(clas.HP_CHANGED_EVENT, handler(self, self.onHpChanged))
     14         :addEventListener(clas.EXP_CHANGED_EVENT, handler(self, self.onExpChanged))
     15 
     16     self.player = hero
     17     self:initDisplay()
     18 end
     19 
     20 function GameUnit:onStateChange( event )
     21     self:updateShapeDisplay(self.player:getState())
     22 end
     23 
     24 --更新角色显示{"anim_walk","anim_eat","anim_placeladder","anim_idle","anim_ladderwalk","anim_laddereat","anim_death"}
     25 function GameUnit:updateShapeDisplay(state)
     26     local animname = "anim_idle"
     27     if state == "walking" then
     28         animname = "anim_walk"
     29     elseif state == "firing" then
     30         animname = "anim_eat"
     31     elseif state == "dead" then
     32         animname = "anim_death"
     33     end
     34     printInfo("GameUnit state changed to %s playing : %s", state, animname)
     35     if self.shape ~= nil then
     36         local animation = self.shape:getAnimation()
     37         animation:setSpeedScale(0.6)
     38         animation:play(animname)
     39         self:setPosition(self.player:getX(), self.player:getY())
     40         self:setScaleX(self.player:getDirection())
     41         
     42     end
     43 
     44     if self.player:isDead() then
     45         scheduler.performWithDelayGlobal(handler(self, self.autoRemove), 1.5)
     46     end
     47 end
     48 
     49 function GameUnit:onKilled( event)
     50     -- body
     51 end
     52 
     53 function GameUnit:onHpChanged( event )
     54     self.hpbar:setProgress(self.player:getHp()/self.player:getMaxHp() * 100)
     55     
     56 end
     57 
     58 function GameUnit:autoRemove()
     59     local action = CCFadeTo:create(1.0, 0.0)
     60     transition.execute(self.shape, action, {
     61         onComplete = function( )
     62             self:removeSelf()
     63         end
     64         })
     65 end
     66 
     67 function GameUnit:onExpChanged( event )
     68     -- body
     69 end
     70 
     71 --初始化显示
     72 function GameUnit:initDisplay()
     73     -- 角色外观
     74     local shape = CCArmature:create(self.player:getRes())
     75     self.shape = shape
     76        self:updateShapeDisplay(self.player:getState())
     77        shape:setAnchorPoint(CCPoint(0.5,0))
     78        self:addChild(shape)
     79 
     80     --角色血条
     81     self.hpbar = Progress.new("progres_bg.png","progress.png")
     82     self.hpbar:pos(0.5, shape:getContentSize().height)
     83     self:addChild(self.hpbar)
     84 end
     85 --传入|更新数据
     86 function GameUnit:setData()
     87     -- body
     88 end
     89 --取得数据
     90 function GameUnit:getData()
     91     return self.hero
     92 end
     93 --添加事件:重复利用这个对象的时候会用到
     94 function GameUnit:addEvents()
     95     -- body
     96 end
     97 --移除事件:重复利用这个对象的时候会用到
     98 function GameUnit:removeEvents()
     99     -- body
    100 end
    101 
    102 return GameUnit

    PlayerController.lua:增加了伤害跳字,优化了角色移动的计算方式

      1 local PlayerController = class("PlayerController")
      2 
      3 function PlayerController:ctor()
      4 end
      5 
      6 function PlayerController:initEventListener(hero)
      7 end
      8 
      9 function PlayerController:tick(spMaps)
     10     ---更新数据
     11     --[[
     12     for name,sp in ipairs(spMaps) do
     13         if sp:getAI() == nil then
     14             sp:setAI(ai)
     15         end
     16     end]]
     17     local  enemy
     18     local me = app:getObject("me")    
     19     local mytarget = me:getTarget()
     20     if mytarget == nil then
     21         me:searchTarget()
     22     end
     23 
     24     mytarget = me:getTarget()
     25     if mytarget == nil then
     26         printLog(10, "Win==============")
     27     else
     28         enemy = app:getObject(mytarget)
     29         if enemy:isDead() then
     30             printLog(10, "Win==============")
     31             return
     32         end
     33         local myShape
     34         local distance = self:dist(enemy:getX(),enemy:getY(), me:getX(), me:getY())
     35         if distance < me:getRadius() then
     36             if me:getState() == "walking" then--走动状态中的话进行待命
     37                 myShape = spMaps[me:getId()]
     38                 myShape:stopActionByTag(100)
     39                 me:standby()
     40             elseif me:getState() == "idle" then--待命状态的话根据冷却时间判断是否攻击
     41                 if me:canFire() then
     42                     me:fire(enemy)
     43                     local percent = math.random(0,10)
     44                     local stat_sp
     45                     if percent >= 6 then
     46                         stat_sp = self:getHarmSp(10,enemy:getX(),enemy:getY()+50)
     47                         enemy:decreaseHp(10)
     48                     else
     49                         stat_sp = self:getHarmSp(0,enemy:getX(),enemy:getY()+50)
     50                     end
     51 
     52                     local parent = display.getRunningScene()
     53                     assert(parent ~= nil, "parent is nil") 
     54                     assert(stat_sp ~= nil, "stat_sp is nil")
     55                     if stat_sp ~= nil and parent ~= nil then
     56                         parent:addChild(stat_sp)
     57                         transition.execute(stat_sp, CCMoveTo:create(1.0, CCPoint(enemy:getX()-50, enemy:getY()+150)), {
     58                             onComplete = function()
     59                                 stat_sp:removeSelf()
     60                             end
     61                             })
     62                     end
     63                 else
     64                     print("Cooling down....")
     65                 end
     66             end
     67         else
     68             if me:getState() ~= "walking" then
     69                 me:walk()
     70                 myShape = spMaps[me:getId()]
     71                 --[[local duration = distance/me:getSpeed()
     72                 local action = CCMoveTo:create(duration,CCPoint(enemy:getX(),enemy:getY()))
     73                 action:setTag(100)
     74                 myShape:runAction(action)]]
     75             else
     76                 me:updatePos()
     77             end
     78         end
     79     end
     80     --更新显示:用显示去更新数据,这个是不对的
     81     for key,sp in pairs(spMaps) do
     82         local data = app:getObject(key)
     83         if data ~= nil then
     84             sp:setPosition(data:getX(), data:getY())
     85             --[[data:setX(sp:getPositionX())
     86             data:setY(sp:getPositionY())]]
     87         end
     88     end
     89 end
     90 
     91 function PlayerController:getHarmSp( harm, posx, posy )
     92     local  sp 
     93     local harmstr = string.format("%d", harm)
     94     if harm > 0 then
     95         sp = CCNode:create()
     96         sp:pos(posx, posy)
     97         for i=1,#harmstr do
     98             local tempsp = display.newSprite("fight/"..string.sub(harmstr, i,i)..".png",(i-1)*30,0)
     99             local size = tempsp:getContentSize()
    100             printLog(1, "tempsp size w:%f h:%f", size.width, size.height)
    101             if tempsp ~= nil then
    102                 sp:addChild(tempsp)
    103             end
    104         end
    105     else
    106         sp = display.newSprite("shanbix_wenzi.png", posx, posy)
    107     end
    108     return sp
    109 end
    110 
    111 function PlayerController:dist( ax,ay,bx,by )
    112     local dx,dy = ax - bx, ay - by
    113     return math.sqrt(dx*dx + dy*dy)
    114 end
    115 
    116 function PlayerController:useSkill(skill)
    117     self.view:useSkill(skill)
    118 end
    119 
    120 function PlayerController:control( view )
    121     -- body
    122     self.view = view
    123 end
    124 
    125 return PlayerController
    PlayerController
  • 相关阅读:
    spring profile 多环境配置管理
    搭建docker私服仓库
    ubuntu安装mysql添加密码
    yum源安装docker
    keep + haproxy 运行原理
    gitlab汉化
    什么是DevOps?
    Kafka高并发原理概述
    k8s + flannel 内网不能互通问题排查
    Flannel跨主机互联概述及容器网络拓扑图
  • 原文地址:https://www.cnblogs.com/adoontheway/p/3978854.html
Copyright © 2020-2023  润新知