示例#1
0
    private void FixedUpdate()
    {
        if (transform.hasChanged)
        {
            transform.hasChanged = false;

            base.X  = body.position.x;
            base.Y  = body.position.y;
            base.DX = body.velocity.x;
            base.DY = body.velocity.y;

            Assets.Server.MovementMessage m = new Assets.Server.MovementMessage(
                this.ID,
                0,
                0,
                base.X,
                base.Y,
                base.Rotation,
                base.DX,
                base.DY
                );

            UDPServer.getInstance().BroadcastMessage(m);
        }
    }
        public void Visit(MovementMessage m)
        {
            // TODO: Make sure that player can only control their character!

            Server.instance.TaskQueue.Enqueue(new Action(() =>
            {
                GameState.instance.PlayerMove(m.GetEntityId(),
                                              m.SequenceNumber,
                                              m.GetXCoordinate(),
                                              m.GetYCoordinate(),
                                              m.GetXVelocity(),
                                              m.GetYVelocity());
            }));
        }
示例#3
0
    public void ForceUpdateClientPosition()
    {
        base.X  = body.position.x;
        base.Y  = body.position.y;
        base.DX = body.velocity.x;
        base.DY = body.velocity.y;

        Assets.Server.MovementMessage m = new Assets.Server.MovementMessage(
            this.ID,
            0,
            0,
            base.X,
            base.Y,
            base.Rotation,
            base.DX,
            base.DY
            );

        this.Client.MessageQueue.Enqueue(m);
    }
示例#4
0
        /// <summary>
        /// Initializes Player, must be run on the main thread
        /// </summary>
        public void Init()
        {
            try
            {
                // Update gamestate of current client
                foreach (KeyValuePair <UInt32, Entity> kv in GameState.instance.Entities)
                {
                    if (kv.Value.GetType() == typeof(Player)) // TODO: Create all entities, not just player
                    {
                        Player e = (Player)kv.Value;
                        EntityUpdateMessage entity = new EntityUpdateMessage(
                            EntityUpdateMessage.Type.PLAYER,
                            EntityUpdateMessage.Action.CREATE,
                            kv.Key,
                            0
                            );
                        MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);

                        this.SendMessage(entity);
                        this.SendMessage(move);
                        this.UpdateHP(kv);
                    }

                    if (kv.Value.GetType() == typeof(Enemy))
                    {
                        Enemy e = (Enemy)kv.Value;
                        EntityUpdateMessage entity = new EntityUpdateMessage(
                            EntityUpdateMessage.Type.ENEMY,
                            EntityUpdateMessage.Action.CREATE,
                            kv.Key,
                            0
                            );
                        MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);

                        this.SendMessage(entity);
                        this.SendMessage(move);
                        this.UpdateHP(kv);
                    }

                    if (kv.Value.GetType() == typeof(Bow) || kv.Value.GetType() == typeof(Crossbow))
                    {
                        EntityUpdateMessage entity = null;
                        Weapon e = (Weapon)kv.Value;
                        if (e.GetType() == typeof(Bow))
                        {
                            entity = new EntityUpdateMessage(
                                EntityUpdateMessage.Type.WEAPON_BOW,
                                EntityUpdateMessage.Action.CREATE,
                                kv.Key,
                                0
                                );
                        }
                        else if (e.GetType() == typeof(Crossbow))
                        {
                            entity = new EntityUpdateMessage(
                                EntityUpdateMessage.Type.WEAPON_CROSSBOW,
                                EntityUpdateMessage.Action.CREATE,
                                kv.Key,
                                0
                                );
                        }
                        else
                        {
                            throw new Exception("Weapon type not found");
                        }
                        Debug.Log("Creating a weapon move message with x: " + e.X + " y: " + e.Y);
                        MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);

                        this.SendMessage(entity);
                        this.SendMessage(move);
                    }
                }

                //this.Player = GameObject.Instantiate(Resources.Load("Player") as GameObject);
                this.PlayerID = GameState.instance.CreatePlayer(this, 50, 50);
                //Server.instance.Entities.TryAdd(this.Player.GetComponent<Player>().ID, this.Player.GetComponent<Player>());

                Debug.Log("Entity id is " + this.PlayerID);

                EntityUpdateMessage NewClient = new EntityUpdateMessage(
                    EntityUpdateMessage.Type.PLAYER,
                    EntityUpdateMessage.Action.CREATE,
                    this.PlayerID,
                    0
                    );
                UDPServer.getInstance().BroadcastMessage(NewClient);

                MovementMessage ClientMovement = new MovementMessage(this.PlayerID, 0, 0, 0, 0, 0, 0, 0);
                UDPServer.getInstance().BroadcastMessage(ClientMovement);

                EntityUpdateMessage control = new EntityUpdateMessage(
                    EntityUpdateMessage.Type.PLAYER,
                    EntityUpdateMessage.Action.CONTROL,
                    this.PlayerID,
                    0
                    );
                this.SendMessage(control);
            }
            catch (Exception e)
            {
                Debug.Log(e);
            }
        }