Beispiel #1
0
        public void HandleKilledEntity(float damageAmount, UInt32 entityID)
        {
            RemoveEntity(entityID);
            AttackMessage newAttack = new AttackMessage(entityID, 0, 0, 0, 0, 1, damageAmount, 0, 0, 0, 1);

            UDPServer.getInstance().BroadcastMessage(newAttack);
        }
Beispiel #2
0
        public void SpawnEnemies()
        {
            // Check to see how many enemies we need to fill up the map with
            int alive = 0;

            foreach (KeyValuePair <UInt32, Entity> kvp in GameState.instance.Entities)
            {
                if (kvp.Value is Enemy && ((Enemy)kvp.Value).IsAlive())
                {
                    alive += 1;
                }
            }

            // Arbitrary equation, start with two vampires and add one every 100 seconds
            int enemies = 2 + (int)Math.Floor(Time.realtimeSinceStartup / 100);

            for (int i = 0; i < enemies - alive; i++)
            {
                UInt32 id = CreateEnemy(45, 45);
                EntityUpdateMessage message = new EntityUpdateMessage(
                    EntityUpdateMessage.Type.ENEMY,
                    EntityUpdateMessage.Action.CREATE,
                    id,
                    20
                    );
                // ^ Hard coded enemy health
                UDPServer.getInstance().BroadcastMessage(message);
            }
        }
Beispiel #3
0
        /// <summary>
        /// A character takes damage.
        /// </summary>
        /// <param name="targetCharacterID">Id of damaged <c>Player</c>.</param>
        /// <param name="damageAmount">Amount of damage that the character takes.</param>
        public void AttackValid(UInt32 targetCharacterID, float damageAmount)
        {
            Character targetEntity = (Character)GetEntity(targetCharacterID);

            targetEntity.TakeDamage(damageAmount);
            AttackMessage newAttack = new AttackMessage(targetCharacterID, 0, 0, 0, 0, 1, damageAmount, 0, 0, 0, 0);

            //targetEntity.Client.MessageQueue.Enqueue(newAttack);
            UDPServer.getInstance().BroadcastMessage(newAttack);
        }
        /// <summary>
        /// Add message about the current mode to broadcast queue.
        /// </summary>
        public void BroadcastState()
        {
            StateUpdateMessage.Descriptor descriptor = IsDay ? StateUpdateMessage.Descriptor.DAY : StateUpdateMessage.Descriptor.NIGHT;
            StateUpdateMessage            message    = new StateUpdateMessage(
                StateUpdateMessage.Type.DAY_NIGHT,
                descriptor
                );

            //Debug.Log(message);
            UDPServer.getInstance().BroadcastMessage(message);
        }
        public void Visit(ItemPickupMessage m)
        {
            Entity item;

            if (GameState.instance.Entities.TryGetValue(m.GetPickupItemId(), out item))
            {
                m.SetPickupConfirmed(1);
                UDPServer.getInstance().BroadcastMessage(m);
                GameState.instance.DestroyEntityID(m.GetPickupItemId());
            }
        }
Beispiel #6
0
        /// <summary>
        /// <c>Player</c> attack towards a direction.
        /// </summary>
        /// <param name="playerId">Id of attacking <c>Player</c>.</param>
        /// <param name="weaponId">weaponId of attacking <c>Player</c>.</param>
        /// <param name="clickPositionX">X position of the attack <c>Player</c>.</param>
        /// <param name="clickPositionY">Y position of the attack<c>Player</c>.</param>
        public void EnemyAttack(UInt32 enemyID, UInt32 targetPlayerID)
        {
            //Debug.Log("Start of EnemyAttack");

            Enemy  enemy  = (Enemy)GetEntity(enemyID);
            Player player = (Player)GetEntity(targetPlayerID);

            AttackMessage AttackInit = new AttackMessage(enemyID, 0, 0, targetPlayerID, 0, 0, 0, player.X, player.Y, 1, 0);

            Debug.Log("Broadcasting in EnemyAttack");
            UDPServer.getInstance().BroadcastMessage(AttackInit);
        }
Beispiel #7
0
        void FixedUpdate()
        {
            while (this.TaskQueue.Count > 0)
            {
                bool s = this.TaskQueue.TryDequeue(out Action a);

                if (s)
                {
                    a.Invoke();
                }
            }

            GameState.instance.FixedUpdate();
            UDPServer.getInstance().FixedUpdate();
        }
Beispiel #8
0
        /// <summary>
        /// <c>Player</c> attack towards a direction.
        /// </summary>
        /// <param name="playerId">Id of attacking <c>Player</c>.</param>
        /// <param name="weaponId">weaponId of attacking <c>Player</c>.</param>
        /// <param name="clickPositionX">X position of the attack <c>Player</c>.</param>
        /// <param name="clickPositionY">Y position of the attack<c>Player</c>.</param>
        public void PlayerAttack(UInt32 playerId, short weaponId, float clickPositionX, float clickPositionY)
        {
            //Debug.Log("Start of PlayerAttack");
            Vector2 clickPosition;

            clickPosition.x = clickPositionX;
            clickPosition.y = clickPositionY;

            Player player = (Player)GetEntity(playerId);

            AttackMessage AttackInit = new AttackMessage(playerId, 0, 0, 0, weaponId, 0, 0, clickPositionX, clickPositionY, 1, 0);

            Debug.Log("Broadcasting in PlayerATtack");
            UDPServer.getInstance().BroadcastMessage(AttackInit);

            player.TryToAttack(clickPosition, weaponId);
        }
Beispiel #9
0
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed and unmanaged resources.
                if (disposing)
                {
                    GameState.instance.DestroyEntityID(this.PlayerID);
                    EntityUpdateMessage message = new EntityUpdateMessage(
                        EntityUpdateMessage.Type.PLAYER,
                        EntityUpdateMessage.Action.DELETE,
                        this.PlayerID,
                        0
                        );
                    UDPServer.getInstance().BroadcastMessage(message);
                }

                disposed = true;
            }
        }
Beispiel #10
0
        // Start is called before the first frame update
        void Start()
        {
            Debug.Log("Starting server on port " + port + "...");

            Debug.Log("Registering with lobbymanager... (" + lobbyManager + ")");
            UnityWebRequest wr = UnityWebRequest.Post(lobbyManager + "/api/lobby/new/" + port, "");

            wr.SendWebRequest();
            while (!wr.isDone)
            {
                ;
            }
            try
            {
                this.lobby = JsonUtility.FromJson <LobbyResponse>(wr.downloadHandler.text);
            }
            catch (ArgumentException e)
            {
                Debug.LogError(wr.downloadHandler.text);
                Debug.LogError(e);
            }
            Debug.Log("Sever has ID " + this.lobby.id);
            // Start lobbymanager heartbeat
            InvokeRepeating("Heartbeat", 1f, 10f);


            Debug.Log("Loading game...");
            //Runs the GameLoader script
            (Resources.Load("GameLoader") as GameObject).GetComponent <GameLoader>().Init();
            this.TaskQueue  = new ConcurrentQueue <Action>();
            Server.instance = this;


            Debug.Log("Listening for new connections...");
            UDPServer.getInstance().Init(this, port);
            Debug.Log("Loading complete");
        }
Beispiel #11
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);
            }
        }
Beispiel #12
0
 private void OnDestroy()
 {
     UDPServer.getInstance().Stop();
 }