private void HandleMovement()
    {
        float moveX = 0;
        float moveY = 0;

        if (Input.GetKey(KeyCode.UpArrow))
        {
            moveY = 1;
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            moveY = -1;
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            moveX = -1;
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            moveX = 1;
        }

        Vector3 moveDir = new Vector3(moveX, moveY).normalized;
        bool    isIdle  = moveX == 0 && moveY == 0;

        if (!isIdle)
        {
            Dirt_Handler.SpawnInterval(GetPosition() + new Vector3(0, -4), moveDir * -1);
        }
        animatedWalker.SetMoveVector(moveDir);
        transform.position = transform.position + moveDir * speed * Time.deltaTime;
    }
    public static void Spawn(int amt, Vector3 loc, Vector3 dir)
    {
        InitIfNeeded();
        dir.Normalize();
        Vector3 baseDir = dir;

        loc.z = 0;
        for (int i = 0; i < amt; i++)
        {
            dir = baseDir;
            Dirt_Handler handler = new Dirt_Handler();
            handler.pos        = loc;
            handler.index      = Generic_Mesh_Script.GetIndex("Mesh_Dirt");
            handler.meshScript = Generic_Mesh_Script.GetMeshScript("Mesh_Dirt");

            dir.x += Random.Range(-.4f, .4f);
            dir.y += Random.Range(-.4f, .4f);

            Vector3 velocity = dir.normalized;

            handler.speed    = 5f * Random.Range(3f, 6f);
            handler.velocity = velocity;
            handler.eulerY   = Random.Range(0, 360);
            Generic_Mesh_Script.AddGeneric("Mesh_Dirt", handler.pos, handler.eulerY, handler.material, 0f, baseSize, false);

            instanceList.Add(handler);
        }
    }
        private void HandleMovement()
        {
            float moveX = 0;
            float moveY = 0;

            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
            {
                moveY = 1;
            }
            if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            {
                moveY = -1;
            }
            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
            {
                moveX = -1;
            }
            if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            {
                moveX = 1;
            }

            Vector3 moveDir = new Vector3(moveX, moveY).normalized;

            lastMoveDir = moveDir;
            bool isIdle = moveX == 0 && moveY == 0;

            if (isIdle)
            {
                lastMoveDir = Vector3.down;
                unitAnimation.PlayAnim(GameAssets.UnitAnimTypeEnum.dSwordTwoHandedBack_Idle, lastMoveDir, 1f, null, null, null);
            }
            else
            {
                unitAnimation.PlayAnim(GameAssets.UnitAnimTypeEnum.dSwordTwoHandedBack_Walk, lastMoveDir, .75f, null, null, null);
                Dirt_Handler.SpawnInterval(GetPosition() + new Vector3(0, -4), lastMoveDir * -1);
            }
            transform.position = transform.position + moveDir * speed * Time.deltaTime;
        }
    // Use this for initialization
    private void Start()
    {
        V_Object           vObject         = CreateBasicUnit(transform, new Vector3(500, 680), 30f, GameAssets.i.m_MarineSpriteSheet);
        V_UnitAnimation    unitAnimation   = vObject.GetLogic <V_UnitAnimation>();
        V_UnitSkeleton     unitSkeleton    = vObject.GetLogic <V_UnitSkeleton>();
        V_IObjectTransform objectTransform = vObject.GetLogic <V_IObjectTransform>();

        bool canShoot = true;

        V_UnitSkeleton_Composite_Weapon unitSkeletonCompositeWeapon = new V_UnitSkeleton_Composite_Weapon(vObject, unitSkeleton, GameAssets.UnitAnimEnum.dMarine_AimWeaponRight, GameAssets.UnitAnimEnum.dMarine_AimWeaponRightInvertV, GameAssets.UnitAnimEnum.dMarine_ShootWeaponRight, GameAssets.UnitAnimEnum.dMarine_ShootWeaponRightInvertV);

        vObject.AddRelatedObject(unitSkeletonCompositeWeapon);
        unitSkeletonCompositeWeapon.SetActive();

        V_UnitSkeleton_Composite_Walker unitSkeletonCompositeWalker_BodyHead = new V_UnitSkeleton_Composite_Walker(vObject, unitSkeleton, GameAssets.UnitAnimTypeEnum.dMarine_Walk, GameAssets.UnitAnimTypeEnum.dMarine_Idle, new[] { "Body", "Head" });

        vObject.AddRelatedObject(unitSkeletonCompositeWalker_BodyHead);

        V_UnitSkeleton_Composite_Walker unitSkeletonCompositeWalker_Feet = new V_UnitSkeleton_Composite_Walker(vObject, unitSkeleton, GameAssets.UnitAnimTypeEnum.dMarine_Walk, GameAssets.UnitAnimTypeEnum.dMarine_Idle, new[] { "FootL", "FootR" });

        vObject.AddRelatedObject(unitSkeletonCompositeWalker_Feet);

        // Update Code
        bool isAimDownSights = false;

        FunctionUpdater.Create(() => {
            Vector3 targetPosition   = UtilsClass.GetMouseWorldPosition();
            Vector3 aimDir           = (targetPosition - vObject.GetPosition()).normalized;
            fieldOfView.transform.up = (aimDir);

            // Check for hits
            Vector3 gunEndPointPosition = vObject.GetLogic <V_UnitSkeleton>().GetBodyPartPosition("MuzzleFlash");
            RaycastHit2D raycastHit     = Physics2D.Raycast(gunEndPointPosition, (targetPosition - gunEndPointPosition).normalized, Vector3.Distance(gunEndPointPosition, targetPosition));
            if (raycastHit.collider != null)
            {
                // Hit something
                targetPosition = raycastHit.point;
            }

            unitSkeletonCompositeWeapon.SetAimTarget(targetPosition);

            if (canShoot && Input.GetMouseButton(0))
            {
                // Shoot
                canShoot = false;
                // Replace Body and Head with Attack
                unitSkeleton.ReplaceBodyPartSkeletonAnim(GameAssets.UnitAnimTypeEnum.dMarine_Attack.GetUnitAnim(aimDir), "Body", "Head");
                // Shoot Composite Skeleton
                unitSkeletonCompositeWeapon.Shoot(targetPosition, () => {
                    canShoot = true;
                });

                // Add Effects
                Vector3 shootFlashPosition = vObject.GetLogic <V_UnitSkeleton>().GetBodyPartPosition("MuzzleFlash");
                if (OnShoot != null)
                {
                    OnShoot(this, new OnShootEventArgs {
                        gunEndPointPosition = shootFlashPosition, shootPosition = targetPosition
                    });
                }

                //Shoot_Flash.AddFlash(shootFlashPosition);
                //WeaponTracer.Create(shootFlashPosition, targetPosition);
            }


            // Manual Movement
            bool isMoving   = false;
            Vector3 moveDir = new Vector3(0, 0);
            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
            {
                moveDir.y = +1; isMoving = true;
            }
            if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            {
                moveDir.y = -1; isMoving = true;
            }
            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
            {
                moveDir.x = -1; isMoving = true;
            }
            if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            {
                moveDir.x = +1; isMoving = true;
            }
            moveDir.Normalize();

            float moveSpeed = 50f;
            Vector3 targetMoveToPosition = objectTransform.GetPosition() + moveDir * moveSpeed * Time.deltaTime;
            // Test if can move there
            raycastHit = Physics2D.Raycast(GetPosition() + moveDir * .1f, moveDir, Vector3.Distance(GetPosition(), targetMoveToPosition));
            if (raycastHit.collider != null)
            {
                // Hit something
            }
            else
            {
                // Can move, no wall
                objectTransform.SetPosition(targetMoveToPosition);
            }

            if (isMoving)
            {
                Dirt_Handler.SpawnInterval(GetPosition(), moveDir * -1f);
            }


            // Update Feet
            unitSkeletonCompositeWalker_Feet.UpdateBodyParts(isMoving, moveDir);

            if (canShoot)
            {
                // Update Head and Body parts only when Not Shooting
                unitSkeletonCompositeWalker_BodyHead.UpdateBodyParts(isMoving, aimDir);
            }
        });
    }
    // Use this for initialization
    private void Start()
    {
        V_Object        vObject       = CreateBasicUnit(transform, new Vector3(500, 680), 30f, GameAssets.i.m_MarineSpriteSheet);
        V_UnitAnimation unitAnimation = vObject.GetLogic <V_UnitAnimation>();
        V_UnitSkeleton  unitSkeleton  = vObject.GetLogic <V_UnitSkeleton>();
        //V_IObjectWalker walker = vObject.GetLogic<V_IObjectWalker>();
        V_IObjectTransform objectTransform = vObject.GetLogic <V_IObjectTransform>();

        bool canShoot = true;

        V_UnitSkeleton_Composite_Weapon unitSkeletonCompositeWeapon = new V_UnitSkeleton_Composite_Weapon(vObject, unitSkeleton, GameAssets.UnitAnimEnum.dMarine_AimWeaponRight, GameAssets.UnitAnimEnum.dMarine_AimWeaponRightInvertV, GameAssets.UnitAnimEnum.dMarine_ShootWeaponRight, GameAssets.UnitAnimEnum.dMarine_ShootWeaponRightInvertV);

        vObject.AddRelatedObject(unitSkeletonCompositeWeapon);
        unitSkeletonCompositeWeapon.SetActive();

        V_UnitSkeleton_Composite_Walker unitSkeletonCompositeWalker_BodyHead = new V_UnitSkeleton_Composite_Walker(vObject, unitSkeleton, GameAssets.UnitAnimTypeEnum.dMarine_Walk, GameAssets.UnitAnimTypeEnum.dMarine_Idle, new[] { "Body", "Head" });

        vObject.AddRelatedObject(unitSkeletonCompositeWalker_BodyHead);

        V_UnitSkeleton_Composite_Walker unitSkeletonCompositeWalker_Feet = new V_UnitSkeleton_Composite_Walker(vObject, unitSkeleton, GameAssets.UnitAnimTypeEnum.dMarine_Walk, GameAssets.UnitAnimTypeEnum.dMarine_Idle, new[] { "FootL", "FootR" });

        vObject.AddRelatedObject(unitSkeletonCompositeWalker_Feet);

        FunctionUpdater.Create(() => {
            Vector3 targetPosition = UtilsClass.GetMouseWorldPosition();
            Vector3 aimDir         = (targetPosition - vObject.GetPosition()).normalized;

            unitSkeletonCompositeWeapon.SetAimTarget(targetPosition);

            if (canShoot && Input.GetMouseButton(0))
            {
                // Shoot
                canShoot = false;
                // Replace Body and Head with Attack
                unitSkeleton.ReplaceBodyPartSkeletonAnim(GameAssets.UnitAnimTypeEnum.dMarine_Attack.GetUnitAnim(aimDir), "Body", "Head");
                // Shoot Composite Skeleton
                unitSkeletonCompositeWeapon.Shoot(targetPosition, () => {
                    canShoot = true;
                });

                // Add Effects
                Vector3 shootFlashPosition = vObject.GetLogic <V_UnitSkeleton>().GetBodyPartPosition("MuzzleFlash");
                if (OnShoot != null)
                {
                    OnShoot(this, new OnShootEventArgs {
                        gunEndPointPosition = shootFlashPosition, shootPosition = targetPosition
                    });
                }

                //Shoot_Flash.AddFlash(shootFlashPosition);
                //WeaponTracer.Create(shootFlashPosition, targetPosition);
            }


            // Manual Movement
            bool isMoving   = false;
            Vector3 moveDir = new Vector3(0, 0);
            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
            {
                moveDir.y = +1; isMoving = true;
            }
            if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            {
                moveDir.y = -1; isMoving = true;
            }
            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
            {
                moveDir.x = -1; isMoving = true;
            }
            if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            {
                moveDir.x = +1; isMoving = true;
            }
            moveDir.Normalize();
            objectTransform.SetPosition(objectTransform.GetPosition() + moveDir * 50f * Time.deltaTime);

            /*
             * if (isMoving) {
             *  unitSkeleton.ReplaceBodyPartSkeletonAnim(GameAssets.UnitAnimTypeEnum.dBareHands_Walk.GetUnitAnim(moveDir), "FootL", "FootR");
             *  if (canShoot) {
             *      unitSkeleton.ReplaceBodyPartSkeletonAnim(GameAssets.UnitAnimTypeEnum.dBareHands_Walk.GetUnitAnim(aimDir), "Body", "Head");
             *  }
             * } else {
             *  unitSkeleton.ReplaceBodyPartSkeletonAnim(GameAssets.UnitAnimTypeEnum.dBareHands_Idle.GetUnitAnim(moveDir), "FootL", "FootR");
             *  if (canShoot) {
             *      unitSkeleton.ReplaceBodyPartSkeletonAnim(GameAssets.UnitAnimTypeEnum.dBareHands_Idle.GetUnitAnim(aimDir), "Body", "Head");
             *  }
             * }*/

            if (isMoving)
            {
                Dirt_Handler.SpawnInterval(GetPosition(), moveDir * -1f);
            }


            // Update Feet
            //unitSkeletonCompositeWalker_Feet.UpdateBodyParts(walker.GetLastMoveDir());
            unitSkeletonCompositeWalker_Feet.UpdateBodyParts(isMoving, moveDir);

            if (canShoot)
            {
                // Update Head and Body parts only when Not Shooting
                unitSkeletonCompositeWalker_BodyHead.UpdateBodyParts(isMoving, aimDir);
            }
        });
    }