Beispiel #1
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 #2
0
        private void UpdateHP(KeyValuePair <UInt32, Entity> kv)
        {
            Character           character = (Character)kv.Value;
            EntityUpdateMessage hpUpdate  = new EntityUpdateMessage(
                EntityUpdateMessage.Type.PLAYER,
                EntityUpdateMessage.Action.HP_UPDATE,
                kv.Value.ID,
                character.currentHealth);

            this.SendMessage(hpUpdate);
            Debug.Log("hello");
        }
Beispiel #3
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 #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);
            }
        }
 public void Visit(EntityUpdateMessage m)
 {
     Debug.Log(m);
 }