Пример #1
0
 public override EnemyState Update(GameTime gameTime)
 {
     if (Vector2.DistanceSquared(enemy.Position, target.Position) < shootingDistanceSquared)
     {
         shotCooldownMs -= gameTime.ElapsedGameTime.Milliseconds;
         Vector2 toTarget = target.Position - enemy.Position;
         toTarget.Normalize();
         input.Rotation = MathHelper.ToRadians(90) - (float)Math.Atan2(toTarget.X, toTarget.Y);
         if (shotCooldownMs <= 0f)
         {
             RaycastHit hit    = PhysicsTools.RaycastFirst(enemy.Position, target.Position);
             Random     random = Randomizer.Random;
             if (hit != null && hit.Fixture.Body.UserData == target)
             {
                 // hand shakes
                 Vector2 offset = new Vector2(
                     random.Next(-32, 32),
                     random.Next(-32, 32));
                 enemy.Weapon.Fire(target.Position + offset);
             }
             shotCooldownMs = random.Next(500, 2000);
         }
         return(this);
     }
     return(new WeaponIdleState(enemy));
 }
Пример #2
0
    void InitParabolic(Vector3 position, Quaternion rotation, float shotTime)
    {
        _bounces = _data.parabolicTrajectory.bounceCount;
        SetBouncyness(_bounces > 0);

        Vector3 forward = rotation * PhysicsTools.RandomVectorInCone(_data.parabolicTrajectory.cone);

        //TODO: fix so that forward is already normalized
        forward = forward.normalized;

        Vector3 velocity = forward * _data.velocity;

        SetPhysicsState(position, velocity, shotTime);

        //Accelerate projectiles by the time lost to frame imprecision
        lastRatio            = (Time.fixedTime - lastTime) / Time.fixedDeltaTime;
        _rigidbody.velocity *= lastRatio;

        //Multiply gravity by the square of that factor, to account for the short time
        _rigidbody.AddForce(Physics.gravity
                            * _data.parabolicTrajectory.gravity
                            * lastRatio
                            * lastRatio,
                            ForceMode.Acceleration);

        //TODO calculate the actual position by iteration,
        // given the frame count, and therefore the number of times gravity was applied
    }
Пример #3
0
    private void Update()
    {
        bool isKeyDown = GamePad.GetButtonDown(GamePad.Button.Y, GamePad.Index.Any) || Input.GetKeyDown(KeyCode.Space);

        if (isKeyDown)
        {
            BallBase ball = BallPool.TryTakeBallToPlay();
            if (ball != null)
            {
                ball.enabled = false;
                Vector2 pos = PhysicsTools.GetRandomPositionBetweenVectors(BallSpawnPositionMin.position, BallSpawnPositionMax.position);
                ball.SetGravityState(false);
                ball.ResetTrailEmitter();
                ball.transform.position = pos;
                ball.Rigidbody.velocity = Vector2.zero;
                ball.Rigidbody.AddForce(Vector2.down * PhysicsConstants.BallSpeedAtStart, ForceMode2D.Impulse);
                BallsInPlay.Add(ball);
                ball.ResetLastFrameVelocityAndPosition();

                ball.enabled = true;
            }
            CallOnGameStateChanged();
            GameStarted = true;
            EnemyController.Instance.TryInitEnemySpawning();
        }

        if (GameStarted)
        {
            GameTime += Time.deltaTime;
        }
    }
Пример #4
0
    //TODO fix all of these uses of delta time, which is usually false on the first frame anyway

    void Explode(float deltaTime)
    {
        Vector3 gravity  = Physics.gravity; //TODO get actual gravity from parent projectile. better yet, implement getpos from proj
        Vector3 position = PhysicsTools.GetPosition(prevPosition, prevVelocity, gravity, deltaTime);

        ExplosionManager.instance.GetItem(position, data.explosion.range);
    }
 protected void CheckAngleOnExit(UnityEngine.Collision other)
 {
     if (PhysicsTools.IsLayerInMask(LayersToCheck, other.gameObject.layer))
     {
         if (currentCollisions.Contains(other.gameObject))
         {
             currentCollisions.Remove(other.gameObject);
         }
     }
 }
Пример #6
0
    private void AddBallToPool()
    {
        Vector3  randomPos = PhysicsTools.GetRandomPositionBetweenVectors(BallSpawnPlaceMin.position, BallSpawnPlaceMax.position);
        BallBase ball      = Instantiate(BallPrefab, randomPos, Quaternion.identity);

        ball.SetGravityState(true);
        ballsInPool.Add(ball);
        ball.gameObject.name = "Ball " + ballCounter;
        ballCounter++;
        GameController.Instance.CallOnGameStateChanged();
    }
 protected void CheckAngleOnEnter(UnityEngine.Collision other)
 {
     if (PhysicsTools.IsLayerInMask(LayersToCheck, other.gameObject.layer))
     {
         float angle = Vector3.Angle(Vector3.up, other.contacts[0].normal);
         if (angle <= MaxAngle && !currentCollisions.Contains(other.gameObject))
         {
             currentCollisions.Add(other.gameObject);
         }
     }
 }
Пример #8
0
    void FireAt(float deltaTime, Quaternion direction, float speed)
    {
        //calculate real position in this frame
        //TODO use projectile to also get _lastTime ???
        //If I want an actual replayability, this is gonna be a mess
        Vector3 gravity  = Physics.gravity; //TODO get actual gravity from parent projectile
        Vector3 position = PhysicsTools.GetPosition(prevPosition, prevVelocity, gravity, deltaTime);

        for (int i = 0; i < data.shot.rate; i++)
        {
            ProjectileManager.instance.InitProjectile(this, position, transform.rotation, currentTime, data.projectile, data.effect);
        }
    }
Пример #9
0
        GameObject InstantiateAndAdjustPrefab(GameObject prefab, Vector3 hitPos, Vector3 hitNormal, Vector3 position, Vector3 rotation, float sizeMultiplier)
        {
            Transform instance = (PrefabUtility.InstantiatePrefab(prefab) as GameObject).transform;
            Vector3   up       = alignNormal ? hitNormal : Vector3.up;

            instance.position = hitPos;
            instance.up       = up;
            instance.Rotate(rotation, Space.Self);
            instance.localScale *= sizeMultiplier;
            instance.position    = PhysicsTools.UnIntersectColliderGroup(instance.GetComponentsInChildren <Collider>(), instance.position, up);
            instance.position   += position;
            instance.gameObject.AddComponent <PrefabPainted>();
            return(instance.gameObject);
        }
Пример #10
0
        private void OnCollisionExit(UnityEngine.Collision other)
        {
            if (!allowCollisions)
            {
                return;
            }

            if (PhysicsTools.IsLayerInMask(LayersToCheck, other.collider.gameObject.layer))
            {
                LastDetectedObject = other;
                IsTriggered        = false;
                InvokeTriggerExited();
            }
        }
Пример #11
0
        public override void Update(GameTime gameTime)
        {
            MouseState mouse = Mouse.GetState();

            Position      = new Vector2(mouse.Position.X, mouse.Position.Y);
            WorldPosition = Camera.Main.ToWorldCoordinates(Position);
            HoverCell     = Map.Current.CellAtWorld(WorldPosition);
            if (Settings.DEBUG_RAYCAST)
            {
                RaycastHit hit = PhysicsTools.RaycastFirst(Player.Current.Position, WorldPosition);
                if (hit != null)
                {
                    Gizmo.Line(Player.Current.Position, hit.Point, Color.Azure);
                }
            }
        }
Пример #12
0
    void FixedUpdate()
    {
        _time += Time.fixedDeltaTime;
        float completion = _time / (baseTime * _size);

        if (completion > 1f)
        {
            //gameObject.SetActive(false);
            //GetComponent<Renderer>().enabled = false;
            return;
        }

        transform.position += PhysicsTools.GetMovementUpdateVelocity(ref _velocity, _size * baseGravity * Vector3.down, Time.fixedDeltaTime);

        transform.localScale = baseScale * _size * scale.Evaluate(completion) * Vector3.one;
        GetComponent <Renderer>().material.SetFloat("_Heat", warmth.Evaluate(completion));
        GetComponent <Renderer>().material.SetFloat("_Alpha", alpha.Evaluate(completion));
    }
Пример #13
0
        private void OnCollisionEnter(UnityEngine.Collision other)
        {
            if (LogTrigger)
            {
                UnityEngine.Debug.Log(
                    "Collision with: " + LayerMask.LayerToName(other.collider.gameObject.layer) + " (Is in mask: " +
                    PhysicsTools.IsLayerInMask(LayersToCheck, other.collider.gameObject.layer) + ")", other.gameObject);
            }

            if (!allowCollisions)
            {
                return;
            }

            if (PhysicsTools.IsLayerInMask(LayersToCheck, other.collider.gameObject.layer))
            {
                LastDetectedObject = other;
                IsTriggered        = true;
                InvokeTriggerEntered();
            }
        }
Пример #14
0
        void HandlePreview(bool isValid, bool shiftHeld)
        {
            if (drawMode != DrawMode.Single)
            {
                previewLocked = false;
                if (preview != null)
                {
                    MonoBehaviour.DestroyImmediate(preview.gameObject);
                }
                return;
            }

            if (preview == null)
            {
                OnPrefabChange(GetPrefab());
            }

            if ((!isValid || shiftHeld) && !previewLocked)
            {
                preview.gameObject.SetActive(false);
                return;
            }

            preview.gameObject.SetActive(true);

            preview.position   = hitPos;
            preview.localScale = originalPreviewScale * previewSizeMult;
            Vector3 up = alignNormal ? hitNormal : Vector3.up;

            preview.up = up;
            preview.Rotate(previewRot, Space.Self);
            preview.position  = PhysicsTools.UnIntersectColliderGroup(previewCols, preview.position, up);
            preview.position += previewPos;

            GUI.enabled = false;
            Handles.PositionHandle(preview.position, preview.rotation);
            GUI.enabled = true;
        }