Example #1
0
    public void msg_recieved(SPPlayerObject obj)
    {
        _ct = 200;
        _pos.x = obj._pos._x;
        _pos.y = obj._pos._y;
        _pos.z = obj._pos._z;

        _vel.x = obj._vel._x;
        _vel.y = obj._vel._y;
        _vel.z = obj._vel._z;

        _rot.x = obj._rot._x;
        _rot.y = obj._rot._y;
        _rot.z = obj._rot._z;

        if (obj._alive == 1) {
            gameObject.SetActive(true);
        } else {
            gameObject.SetActive(false);
        }

        if (obj._name == null) obj._name = "Anonymous";
        if (_name_plate != null) _name_plate.text = obj._name;
    }
Example #2
0
 public static SPPlayerObject from_json(JSONObject jso)
 {
     SPPlayerObject rtv = new SPPlayerObject(0,"",new SPVector(0,0,0),new SPVector(0,0,0),new SPVector(0,0,0));
     rtv._id = (int)jso.GetNumber(SN.ID);
     rtv._name = jso.GetString(SN.NAME);
     rtv._pos = SPVector.from_json(jso.GetObject(SN.POS));
     rtv._vel = SPVector.from_json(jso.GetObject(SN.VEL));
     rtv._rot = SPVector.from_json(jso.GetObject(SN.ROT));
     rtv._alive = (int)jso.GetNumber(SN.ALIVE);
     return rtv;
 }
Example #3
0
    private void update()
    {
        while (true) {
            bool queue_empty = true;
            string next_client_msg_str = null;
            lock (_connection_id_to_msg) {
                if (_connection_id_to_msg.Keys.Count > 0) {
                    int target_id = -1;
                    foreach(int i in _connection_id_to_msg.Keys) {
                        target_id = i;
                        break;
                    }
                    next_client_msg_str = _connection_id_to_msg[target_id];
                    _connection_id_to_msg.Remove(target_id);
                    queue_empty = false;
                }
            }
            if (queue_empty) break;

            SPClientMessage next_client_msg = null;
            try {
                next_client_msg = SPClientMessage.from_json(JSONObject.Parse(next_client_msg_str));
            } catch (Exception e) {
                IOut.Log ("---[BAD JSON]---");
                IOut.Log (next_client_msg_str);
                IOut.Log (e.StackTrace);
                continue;
            }

            if (next_client_msg._player._id < 0) {
                if (!_generated_ids_outstanding.Contains(next_client_msg._player._id)) {
                    _events.Add(new SPEvent(SPEvent.TYPE_ALLOC_ID,next_client_msg._player._id,_allocid));
                    _allocid++;
                }
                _generated_ids_outstanding.Add(next_client_msg._player._id);

            } else {
                for (int i = _events.Count-1; i >= 0; i--) {
                    SPEvent evt = _events[i];
                    if (evt._type == SPEvent.TYPE_ALLOC_ID && next_client_msg._player._id == evt._value) {
                        _events.RemoveAt(i);
                    }
                }

                if (!_id_to_players.ContainsKey(next_client_msg._player._id)) {
                    SPPlayerObject neu_obj = new SPPlayerObject(
                        next_client_msg._player._id,
                        next_client_msg._player._name,
                        next_client_msg._player._pos,
                        next_client_msg._player._vel,
                        next_client_msg._player._rot
                    );
                    _id_to_players[next_client_msg._player._id] = neu_obj;
                }

                SPPlayerObject tar_obj = _id_to_players[next_client_msg._player._id];
                tar_obj._pos = next_client_msg._player._pos;
                tar_obj._rot = next_client_msg._player._rot;
                tar_obj._vel = next_client_msg._player._vel;
                tar_obj._alive = next_client_msg._player._alive;
                tar_obj._name = next_client_msg._player._name;
                tar_obj.__timeout = 10;

                foreach(SPBulletObject b in next_client_msg._bullets) {
                    if (!_key_to_bullets.ContainsKey(b.unique_key())) {
                        _key_to_bullets[b.unique_key()] = new SPBulletObject();
                    }

                    SPBulletObject tar = _key_to_bullets[b.unique_key()];
                    tar._id = b._id;
                    tar._playerid = b._playerid;
                    tar._pos = b._pos.copy();
                    tar._rot = b._rot.copy();
                    tar._vel = b._vel.copy();
                    tar.__timeout = 10;
                }
            }

            update_msg_send();
        }

        List<string> uniquekeys_to_remove = new List<string>();
        foreach(string key in _key_to_bullets.Keys) {
            SPBulletObject b = _key_to_bullets[key];
            b.__timeout--;
            if (b.__timeout <= 0) uniquekeys_to_remove.Add(key);
        }
        foreach(string key in uniquekeys_to_remove) _key_to_bullets.Remove(key);

        List<int> ids_to_remove = new List<int>();
        foreach(int id in _id_to_players.Keys) {
            SPPlayerObject obj = _id_to_players[id];
            obj.__timeout--;
            if (obj.__timeout <= 0) {
                ids_to_remove.Add(id);
            }
        }
        foreach(int i in ids_to_remove) _id_to_players.Remove(i);

        for (int i = _events.Count-1; i >= 0; i--) {
            SPEvent evt = _events[i];
            evt.__duration--;
            if (evt.__duration <= 0) {
                _events.RemoveAt(i);
            }
        }
    }