flame forge2d 实现随机小怪以及飞镖射中爆炸

上一篇实现飞镖发射juejin.cn/post/745291... 这一篇实现怪物随机出现,飞镖追踪怪物杀死并爆炸

效果图

这次比较简单

1 生产怪物

在活动区域随机生成一个怪物并让怪物朝着人物移动

随机添加怪物

js 复制代码
  var random = Random();
  //随机添加怪物
  List<MoveMonsterComponent> moveMonsterList = [];
  var maxMoveMonsterCount = 10;
  void randomAddMonster() async{
    while(true){
      await Future.delayed(const Duration(seconds: 1));
      if(moveMonsterList.length > maxMoveMonsterCount){
        continue;
      }
      //game.camera.visibleWorldRect 是会改变的 跟着移动
      var topLeft = game.camera.visibleWorldRect.topLeft.toVector2();
      var bottomRight = game.camera.visibleWorldRect.bottomRight.toVector2();

      var width = game.size.x;
      var height = game.size.y;

      var dx = (bottomRight - topLeft).x;
      var dy = (bottomRight - topLeft).y;

      var position =  (Vector2(dx * random.nextDouble() , dy * random.nextDouble())) + Vector2( - width / 2, -height/2);
      var monster = MoveMonsterComponent();
      monster.position = position;
      moveMonsterList.add(monster);
      monster.startMove(playerContainer.body);
      add(monster);
    }
  }

怪物朝玩家移动

js 复制代码
  void startMove(Body target) async{
    while(true){
      await doMove(target);
    }
  }
  var speed = 100;
  var eachTime = 0.1;
  //每eachTime 计算一次移动 知道移动到玩家身边
  Future doMove(Body target) async{
    var completer = Completer<bool>();
    var distance = speed * eachTime;
    var xOffset = target.position.x - position.x;
    var yOffset = target.position.y - position.y;

    var totalDistance = sqrt(xOffset * xOffset + yOffset * yOffset);
    var cons = xOffset / totalDistance;
    var sin = yOffset / totalDistance;
    if(totalDistance <= distance){
      distance = totalDistance;
    }
    var moveX = distance * cons;
    var moveY = distance * sin;
    add(MoveByEffect(Vector2(moveX,moveY), LinearEffectController(eachTime),onComplete: (){
      completer.complete(true);
    }));
    return completer.future;
  }

2 定时发射飞镖,并随机找一个怪物移动

定时发射飞镖

js 复制代码
  void startShoot() async{
    //每间隔1s 添加一个回旋镖 并移动
    while(true){
      await Future.delayed(const Duration(milliseconds: 300));
      var targetMonster = game.world.moveMonsterList.firstWhereOrNull((item)=>!item.isCollimation);
      if(targetMonster == null){
        continue;
      }
      targetMonster.isCollimation  = true;
      var hxb = HXBSkillComponent();
      hxb.position = body.position;
      world.add(hxb);
      hxb.moveToMonster(targetMonster);
    }
  }

飞镖朝怪物移动

js 复制代码
  void moveToMonster(PositionComponent target) async{
    add(RotateEffect.by(
      pi * 2 * 3,
      EffectController(
        duration: 1,
        // reverseDuration: 1,
        curve: Curves.linear,
        infinite: true,
      ),
    ),);
    while(true){
      await moveToMonsterStep(target);
    }
  }

  Future moveToMonsterStep(PositionComponent target) async{
    var completer = Completer<bool>();
    var distance = speed * eachTime;
    var xOffset = target.position.x - position.x;
    var yOffset = target.position.y - position.y;

    var totalDistance = sqrt(xOffset * xOffset + yOffset * yOffset);
    var cons = xOffset / totalDistance;
    var sin = yOffset / totalDistance;
    var moveX = distance * cons;
    var moveY = distance * sin;
    add(MoveByEffect(Vector2(moveX,moveY), LinearEffectController(eachTime),onComplete: (){
      completer.complete(true);
    }));
    return completer.future;
  }
相关推荐
比特鹰14 小时前
手把手带你用Flutter手搓人生K线
前端·javascript·flutter
火柴就是我15 小时前
Flutter限制输入框只能输入中文,iOS拼音打不出来?
flutter
TT_Close15 小时前
【Flutter×鸿蒙】debug 包也要签名,这点和 Android 差远了
android·flutter·harmonyos
TT_Close1 天前
【Flutter×鸿蒙】FVM 不认鸿蒙 SDK?4步手动塞进去
flutter·swift·harmonyos
TT_Close1 天前
【Flutter×鸿蒙】一个"插队"技巧,解决90%的 command not found
flutter·harmonyos
恋猫de小郭1 天前
Flutter 发布官方 Skills ,Flutter 在 AI 领域再添一助力
android·前端·flutter
恋猫de小郭2 天前
2026 Flutter VS React Native ,同时在 AI 时代 VS Native 开发,你没见过的版本
android·前端·flutter
明君879972 天前
Flutter 如何给图片添加多行文字水印
前端·flutter
四眼肥鱼3 天前
flutter 利用flutter_libserialport 实现SQ800 串口通信
前端·flutter
火柴就是我3 天前
让我们实现一个更好看的内部阴影按钮
android·flutter