Esempio n. 1
0
        private void ShootBullet()
        {
            _triggerHeldDown = false;
            _timeSlowFired   = false;
            Time.timeScale   = 1;

            _playerMovement.EnableMovement();

            if (_timeBeforeLastShot < _shotWaitTime)
            {
                OnPlayerShot?.Invoke(_playerController.PlayerHasBow, false);
                return;
            }
            else
            {
                OnPlayerShot?.Invoke(_playerController.PlayerHasBow, true);
            }

            _timeBeforeLastShot = 0;

            Vector3 shootingPosition = _playerController.PlayerHasBow
                ? _bowShootingPoint.position
                : _slingShotShootingPoint.position;

            GameObject bulletPrefab   = _playerController.PlayerHasBow ? _playerController.PlayerHasAntidote ? _playerAntidoteArrowPrefab : _arrowPrefab : _stonePrefab;
            GameObject bulletInstance = Instantiate(bulletPrefab, shootingPosition, Quaternion.identity);

            float   launchSpeed     = bulletInstance.GetComponent <BaseProjectile>().LaunchSpeed;
            float   xVelocity       = Mathf.Cos(_shootBowDirectionDisplay.rotation.eulerAngles.z * Mathf.Deg2Rad);
            float   yVelocity       = Mathf.Sin(_shootBowDirectionDisplay.rotation.eulerAngles.z * Mathf.Deg2Rad);
            Vector2 launchDirection = new Vector2(xVelocity, yVelocity);

            bulletInstance.transform.rotation = _shootBowDirectionDisplay.rotation;

            bulletInstance.GetComponent <Rigidbody2D>().velocity = launchSpeed * launchDirection.normalized;
            bulletInstance.transform.SetParent(_bulletHolder);
        }
Esempio n. 2
0
 public static void OnPlayerShot(int ammo) => PlayerShot?.Invoke(ammo);
 void PlayerStartedShooting()
 {
     PlayerShot?.Invoke();
 }
Esempio n. 4
0
        public static async Task HostGame(Game state, CancellationToken token)
        {
            using (var listener = new HttpListener())
            {
                try
                {
                    listener.Prefixes.Add($"http://localhost/BattleShip/");
                    listener.Start();
                    while (!token.IsCancellationRequested)
                    {
                        //Await request
                        var pendingRequest = listener.GetContextAsync();
                        while (!pendingRequest.IsCompleted)
                        {
                            await Task.Delay(100, token);
                        }
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }
                        //Get request context
                        var req = await pendingRequest;

                        switch (req.Request.HttpMethod)
                        {
                        case "GET":
                            await req.Respond(200, state, token);

                            break;

                        case "POST":

                            var contentString = req.ReadContent();

                            if (WaitForPlayers)
                            {
                                var player = JsonConvert.DeserializeObject <Player>(contentString);
                                if (player == null)
                                {
                                    await req.Respond(400, "Invalid player", token);
                                }
                                else
                                {
                                    PlayerConnected?.Invoke(player);
                                    await req.Respond(202, state, token);

                                    continue;
                                }
                            }
                            else
                            {
                                var shot = JsonConvert.DeserializeObject <Shot>(contentString);
                                PlayerShot?.Invoke(shot);
                                await req.Respond(202, state, token);
                            }
                            break;

                        default:
                            await req.Respond(405, "method not allowed", token);

                            break;
                        }
                    }
                }
                catch (Exception e) { System.Diagnostics.Debugger.Launch(); }
                finally { listener.Stop(); }
            }
        }