Esempio n. 1
0
        /// <inheritdoc />
        public void Execute(IPlayerEntity player, object[] parameters)
        {
            if (parameters.Length <= 0 || parameters.Length > 2)
            {
                throw new ArgumentException($"Create monster command must have 1 or 2 parameters.", nameof(parameters));
            }

            if (!int.TryParse((string)parameters[0], out int monsterId))
            {
                throw new ArgumentException($"Cannot convert the first parameter in int.", nameof(parameters));
            }

            int quantityToSpawn = 1;

            if (parameters.Length == 2)
            {
                if (!int.TryParse((string)parameters[1], out quantityToSpawn))
                {
                    throw new ArgumentException($"Cannot convert the second parameter in int.", nameof(parameters));
                }
            }
            _logger.LogTrace($"{player.Object.Name} want to spawn {quantityToSpawn} mmonster");

            const int    sizeOfSpawnArea = 13;
            const int    respawnTime     = 65535;
            IMapInstance currentMap      = player.Object.CurrentMap;
            IMapLayer    currentMapLayer = currentMap.GetMapLayer(player.Object.LayerId);
            Vector3      currentPosition = player.Object.Position.Clone();
            var          respawnRegion   = new MapRespawnRegion((int)currentPosition.X - sizeOfSpawnArea / 2, (int)currentPosition.Z - sizeOfSpawnArea / 2, sizeOfSpawnArea, sizeOfSpawnArea, respawnTime, WorldObjectType.Mover, monsterId, quantityToSpawn);

            for (int i = 0; i < quantityToSpawn; i++)
            {
                IMonsterEntity monsterToCreate = _monsterFactory.CreateMonster(currentMap, currentMapLayer, monsterId, respawnRegion, true);
                monsterToCreate.Object.Position = player.Object.Position.Clone();
                currentMapLayer.AddEntity(monsterToCreate);
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        public IPlayerEntity CreatePlayer(DbCharacter character)
        {
            int playerModelId = character.Gender == 0 ? 11 : 12; // TODO: remove these magic numbers

            if (!_gameResources.Movers.TryGetValue(playerModelId, out MoverData moverData))
            {
                throw new ArgumentException($"Cannot find mover with id '{playerModelId}' in game resources.", nameof(playerModelId));
            }

            var player = _playerFactory(_serviceProvider, null) as PlayerEntity;

            IMapInstance map = _mapManager.GetMap(character.MapId);

            if (map == null)
            {
                throw new InvalidOperationException($"Cannot find map with id '{character.MapId}'.");
            }

            IMapLayer mapLayer = map.GetMapLayer(character.MapLayerId) ?? map.DefaultMapLayer;

            player.Object = new ObjectComponent
            {
                ModelId     = playerModelId,
                Type        = WorldObjectType.Mover,
                MapId       = character.MapId,
                CurrentMap  = map,
                LayerId     = mapLayer.Id,
                Position    = new Vector3(character.PosX, character.PosY, character.PosZ),
                Angle       = character.Angle,
                Size        = ObjectComponent.DefaultObjectSize,
                Name        = character.Name,
                Spawned     = false,
                Level       = character.Level,
                MovingFlags = ObjectState.OBJSTA_STAND
            };
            player.VisualAppearance = new VisualAppearenceComponent
            {
                Gender    = character.Gender,
                SkinSetId = character.SkinSetId,
                HairId    = character.HairId,
                HairColor = character.HairColor,
                FaceId    = character.FaceId,
            };
            player.PlayerData = new PlayerDataComponent
            {
                Id         = character.Id,
                Gender     = character.Gender.ToString().ToEnum <GenderType>(),
                Slot       = character.Slot,
                Gold       = character.Gold,
                Authority  = (AuthorityType)character.User.Authority,
                Experience = character.Experience,
                JobData    = _gameResources.Jobs[(DefineJob.Job)character.JobId]
            };
            player.Moves = new MovableComponent
            {
                Speed        = _gameResources.Movers[player.Object.ModelId]?.Speed ?? 0.1f,
                LastMoveTime = Time.GetElapsedTime(),
                NextMoveTime = Time.GetElapsedTime() + 10
            };

            player.Data = moverData;

            player.Attributes[DefineAttributes.HP]  = character.Hp;
            player.Attributes[DefineAttributes.MP]  = character.Mp;
            player.Attributes[DefineAttributes.FP]  = character.Fp;
            player.Attributes[DefineAttributes.STR] = character.Strength;
            player.Attributes[DefineAttributes.STA] = character.Stamina;
            player.Attributes[DefineAttributes.DEX] = character.Dexterity;
            player.Attributes[DefineAttributes.INT] = character.Intelligence;

            player.Statistics          = new StatisticsComponent(character);
            player.Timers.NextHealTime = Time.TimeInSeconds() + RecoverySystem.NextIdleHealStand;

            player.Behavior = _behaviorManager.GetDefaultBehavior(BehaviorType.Player, player);
            player.Hand     = _itemFactory.CreateItem(11, 0, ElementType.None, 0);

            var gameServices = _serviceProvider.GetRequiredService <IEnumerable <IGameSystemLifeCycle> >().OrderBy(x => x.Order);

            foreach (IGameSystemLifeCycle service in gameServices)
            {
                service.Initialize(player);
            }

            mapLayer.AddEntity(player);

            return(player);
        }