简体   繁体   English

处理多人游戏中能力使用情况的好方法?

[英]Good way to handle ability usage within a multiplayer game?

I have written a client<>server based multiplayer game and have finished the basics (it's a flash client written in actionscript 3). 我已经编写了一个基于client <> server的多人游戏,并且已经完成了基础知识(这是用动作脚本3编写的Flash客户端)。 The next step will be implementing a system which will allow me to easily add abilities to the game. 下一步将实现一个系统,该系统将使我能够轻松地为游戏添加功能。

The problem here is that an ability can do so many things. 这里的问题是一种能力可以做很多事情。 Eg. 例如。 player A uses ability "repair" on player B, the following will have to happen: 玩家A对玩家B使用能力“修复”,则必须进行以下操作:

  • Player A sends message to server informing about the action 播放器A向服务器发送消息,通知该操作
  • Player A is now showing a "repair casted" animation on its own ship 玩家A现在正在自己的飞船上显示“修复投放”动画
  • The server has to inform all players near that ship A now has the "repair casted" animation 服务器必须通知该船A附近的所有玩家现在具有“修复投放”动画
  • The server has to increase the "health" of player B because it has been repaired by player A 服务器必须增加玩家B的“健康状况”,因为服务器A已对其进行了修复
  • The server has to inform all nearby players that player B now has a different health value 服务器必须通知所有附近的玩家B玩家现在具有不同的健康值
  • The server has to inform all nearby players that player B should show the "being repaired" animation 服务器必须通知所有附近的玩家,玩家B应该显示“正在修复”动画

That's just an example, a lot of things have to happen for 1 simple ability. 这只是一个例子,一个简单的功能必须完成很多事情。 I could go into every object and add lines of code just for this spell. 我可以进入每个对象,并为该咒语添加代码行。 However, that would become a mess when I need to add a lot (~50) abilities. 但是,当我需要添加很多(〜50)功能时,这将变得一团糟。 Also notice that some abilities do whole other things than others, some will have to show animations, some won't. 还要注意,某些能力会比其他能力做其他事情,有些则必须显示动画,有些则不会。 Some will have to damage, some will have to increase statistics, etc. 有些将不得不损坏,有些将不得不增加统计数据,等等。

So, how is such "ability system" usually handled both client and server side? 那么,这种“能力系统”通常如何同时在客户端和服务器端处理?

On client side never do a thing without the consent of the server. 在客户端,未经服务器同意绝不能做任何事情。 So client should only send the request of an ability and an optional target or some value. 因此,客户仅应发送能力和可选目标或某个值的请求。 Also client should have to listen server for any commands. 客户端也应该监听服务器的任何命令。 On the other hand, server should implement a function or classes (whatever you feel comfortable) to control ability actions. 另一方面,服务器应该实现一个函数或类(无论您觉得如何)来控制能力动作。 Whenever a client requests an ability to cast ability function should be called. 每当客户请求能力转换能力功能时,都应调用。 As first task, function should check prerequisites. 作为首要任务,职能部门应检查前提条件。 If they fail, client should be notified. 如果失败,则应通知客户。 Otherwise, a use repair ability ok signal should be passed to client. 否则,应将使用修复能力正常信号传递给客户端。 Then the function finds and send required commands to any nearby ships while updating their statuses. 然后,该功能会在更新其状态时查找所需的命令并将其发送给附近的任何船只。 This method will guarantee that abilities cannot be abused by clients. 这种方法将确保客户不会滥用这些能力。

To understand it check the following, this is for illustration of the method, so dont tell me its not good to use so many ifs. 要了解它,请检查以下内容,这是方法的说明,所以不要告诉我使用那么多的ifs不好。

Client 客户

function massRepair() {
    server.send("USE mass_repair;"); //no decisions in here
}

function recive(command, target) {
    if(command=="USE mass_repair OK")
        massRepairAnim.gotoAndPlay(0);

    if(command=="ANIM mass_repair") {
        massRepairAnim.setTarget(target);
        massRepairAnim.gotoAndPlay(0);
    }

    if(command==/SET ENERGY [0-9]/) {
         value=getValue(command);
         setEnergy(value);
    }

    if(command==/SET HEALTH [0-9]/) {
         value=getValue(command);
         setHealth(value);
    }

    if(command=="ANIM self_repair") {
        selfRepairAnim.gotoAndPlay(0);
    }

    if(command=="USE mass_repair FAIL NOENERGY") {
        display("Not enough energy to use the skill");
    }
}

somebutton.click=function() { massRepair(); }

Server 服务器

function userMassRepair(Ship owner) {
    if(owner.energy<30) {
        send(owner, "USE mass_repair FAIL NOENERGY");
        return false;
    }

    owner.energy-=30;
    send(owner, "SET ENERGY "+owner.energy); //every data sent is absolute
    send(owner, "USE mass_repair OK");

   foreach(ship in owner.shipsInRange(100)) {
        if(owner.ally(ship)) {
             ship.health+=10;
             ship.send("SET HEALTH " + ship.health);
             ship.send("ANIM mass_repair "+owner.position);
             ship.send("ANIM self_repair");
        }
   }
}

It looks like a lot of things, but it's really just a permutation of several standard components. 它看起来很多东西,但实际上只是几个标准组件的排列。 Most actions will consist of changes to intrinsic properties and some cosmetics to be shown on the client, and there are only a handful of different ways to do those. 大多数动作将包括对内在特性的更改和一些要在客户身上展示的化妆品,并且只有少数几种不同的方法可以做到。

You need something like the following: 您需要以下内容:

  • a standard way of sending actions to the server, which allows for convenient extraction of arbitrary parameters (as each action may have some very specific arguments) 将动作发送到服务器的标准方法,它允许方便地提取任意参数(因为每个动作可能都有一些非常具体的参数)
  • a message type from server to clients to start playing a specified animation on a specified ship 从服务器到客户端的消息类型,以开始在指定的飞船上播放指定的动画
  • a message type from server to clients increasing the health value on a ship 从服务器到客户端的消息类型,增加了船上的健康值

Then your server code for the action you mention looks like this pseudocode: 然后,您提到的操作的服务器代码看起来像下面的伪代码:

if (message.type == REPAIR)
{
    repairer = message.actor;
    repaired = message.target;

    // Insert verification code here to check that the 2 ships are close enough,
    // that the repairer is entitled to perform this action, etc

    // Verification has passed: do the work
    broadcastToAllClients(new Message(type=PLAY_ANIM, target=repairer, value=REPAIR_CASTED));
    broadcastToAllClients(new Message(type=CHANGE_PROPERTY, target=repaired, proerty=HEALTH, value=5)); // 5 is the amount 
    broadcastToAllClients(new Message(type=PLAY_ANIM, target=repaired, value=BEING_REPAIRED));
}

That is very easy to extend to any number of actions, and will just require a few extra message types to accommodate the different things that can change or can be displayed. 这很容易扩展到任意数量的操作,并且只需要一些额外的消息类型即可容纳可以更改或显示的不同内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM