Пример #1
0
    void OnReceive(byte[] bytes)
    {
        var recv_msg = GameMessage.GetRootAsGameMessage(new ByteBuffer(bytes));
        switch (recv_msg.MsgType)
        {
            case GameMessages.GetAgents:
                {
                    agents.Clear();
                    GetAgents getAgents = recv_msg.Msg<GetAgents>().Value;
                    for (int i = 0; i < getAgents.AgentsLength; ++i)
                    {
                        var agent_id = getAgents.Agents(i).Value.AgentId;
                        var pos = new Vector3(getAgents.Agents(i).Value.Pos.Value.X, getAgents.Agents(i).Value.Pos.Value.Y, getAgents.Agents(i).Value.Pos.Value.Z);
                        agents[agent_id] = new Agent() { pos = pos, state = getAgents.Agents(i).Value.State };

                        GameObject game_object = null;
                        if(game_objects.TryGetValue(agent_id, out game_object) ==false)
                        {
                            switch(getAgents.Agents(i).Value.GameObjectType)
                            {
                                case GameObjectType.Monster:
                                    game_object = (GameObject)Instantiate(Resources.Load("Monster"), pos, Quaternion.identity);
                                    game_object.GetComponent<Monster>().agnet_id = agent_id;
                                    break;
                                case GameObjectType.Character:
                                    game_object = (GameObject)Instantiate(Resources.Load("Character2"), pos, Quaternion.identity);
                                    game_object.GetComponent<Character>().agnet_id = agent_id;
                                    player_agnet_id = agent_id;
                                    break;
                            }

                            game_objects[agent_id] = game_object;
                        }

                        if (getAgents.Agents(i).Value.GameObjectType == GameObjectType.Monster)
                        {
                            switch(getAgents.Agents(i).Value.State)
                            {
                                case AIState.Detect:
                                    game_objects[agent_id].GetComponent<MeshRenderer>().material.color = Color.red;
                                    break;
                                case AIState.Patrol:
                                    game_objects[agent_id].GetComponent<MeshRenderer>().material.color = Color.white;
                                    break;
                                case AIState.Attack:
                                    game_objects[agent_id].GetComponent<MeshRenderer>().material.color = Color.blue;
                                    break;
                            }
                        }
                    }

                    List<int> removals = new List<int>();
                    foreach(var game_object in game_objects)
                    {
                        if (agents.ContainsKey(game_object.Key) == false)
                            removals.Add(game_object.Key);
                    }

                    foreach(var id in removals)
                    {
                        Destroy(game_objects[id]);
                        game_objects.Remove(id);
                    }

                    for (int i = 0; i < getAgents.DebugsLength; ++i)
                    {
                        Vector3 pos;
                        pos.x = getAgents.Debugs(i).Value.EndPos.Value.X;
                        pos.y = getAgents.Debugs(i).Value.EndPos.Value.Y;
                        pos.z = getAgents.Debugs(i).Value.EndPos.Value.Z;
                        var obj = (GameObject)Instantiate(Resources.Load("DebugTarget"), pos, Quaternion.identity);
                    }
                }
                break;
        }
    }