你不仅是指挥官,还是战术家!
学会同时指挥整支队伍移动和攻击!
你要带领一队弓箭手穿越森林。
你不需要对每个士兵喊话。
用 for 循环,一次命令所有人!
// 1. 获取所有朋友 auto friends = hero.findFriends(); // 2. 对每一个朋友下令 for(int i=0; i < friends.size(); i++) { auto friend = friends[i]; // 这里写指挥逻辑... }
为了不让队伍走散,我们要让他们慢慢走。
不要直接走到终点,而是一步一步挪动。
x + 0.35// 创建一个新的移动目标 auto moveTo = { "x": friend.pos.x + 0.35, "y": friend.pos.y }; // 命令朋友移动 hero.command(friend, "move", moveTo);
把刚才学到的拼起来!
while(true) { auto friends = hero.findFriends(); // 🔄 遍历每一个朋友 for(int i=0; i < friends.size(); i++) { auto friend = friends[i]; // 👀 让朋友自己找敌人 auto enemy = friend.findNearestEnemy(); if(enemy) { // ⚔️ 看到敌人就打 hero.command(friend, "attack", enemy); } else { // 🚶 没有敌人就慢慢前进 auto moveTo = {friend.pos.x + 0.35, friend.pos.y}; hero.command(friend, "move", moveTo); } } }
for(int i=0; ifriends[i]hero.command(){x+0.35, y}你已经是一个合格的小队长了!
继续前进!