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;
  }
相关推荐
●VON6 小时前
Flutter 项目成功运行后,如何正确迁移到 OpenHarmony?常见疑问与跳转失效问题解析
flutter·华为·openharmony·开源鸿蒙
●VON7 小时前
Flutter 编译开发 OpenHarmony 全流程实战教程(基于 GitCode 社区项目)
flutter·openharmony·gitcode
消失的旧时光-194319 小时前
Flutter 组件:Row / Column
flutter
程序员老刘21 小时前
Flutter版本选择指南:3.35稳定,3.38发布 | 2025年11月
flutter·客户端
kirk_wang1 天前
Flutter 3.38和Dart 3.10中最大的更新
flutter
前端小伙计1 天前
Flutter 配置国内镜像,加速项目加载!
flutter
zonda的地盘1 天前
开发 Flutter Plugin 之 初始配置
flutter
消失的旧时光-19432 天前
Flutter TextField 从入门到精通:掌握输入框的完整指南
flutter
wordbaby2 天前
Flutter Form Builder 完全指南:告别 Controller 地狱
前端·flutter
tbit2 天前
fluwx 拉起小程序WXLog:Error:fail to load Keychain status:-25300, keyData null:1
flutter·ios·微信小程序