Пример #1
0
    void Start()
    {
        instance = this;

        allRocks      = new Rock[rockCount];
        conveyorRocks = new List <Rock>(rockCount);
        matrices      = new Matrix4x4[rockCount];

        minConveyorX = -conveyorMargin;
        maxConveyorX = ArmManager.armRowWidth + conveyorMargin;
        Vector3 basePos = new Vector3(minConveyorX, 0f, 1.5f);

        float spacing = (ArmManager.armRowWidth + conveyorMargin * 2f) / rockCount;

        for (int i = 0; i < rockCount; i++)
        {
            Rock rock = new Rock();
            rock.position   = basePos + Vector3.right * spacing * i;
            rock.state      = Rock.State.Conveyor;
            rock.targetSize = Random.Range(minRockSize, maxRockSize);
            rock.size       = 0f;
            matrices[i]     = Matrix4x4.TRS(rock.position, Quaternion.identity, Vector3.one * rock.size);
            allRocks[i]     = rock;
            conveyorRocks.Add(rock);
        }
    }
Пример #2
0
        public void attack()
        {
            Vector2 pos = getLocation();

            if (getState() == sSTATE_ATTACKING)
            {
                if (getCurrentSprite().getCurrentFrame() == 24 && !shotBullet)
                {
                    RockManager.getInstance().createObject(pos + new Vector2(30, 20), new Vector2(getCurrentSprite().isFlipped()? 10:-10, -200), color_);
                    shotBullet = true;

                    SoundManager.PlaySound(cSOUND_SHOOT);
                }
                else
                {
                    shotBullet = false;
                }
            }
        }
Пример #3
0
 void Awake()
 {
     Instance = this;
 }
Пример #4
0
        public override void update(GameTime gameTime)
        {
            base.update(gameTime);//getCurrentSprite().update();
            rockerWing.mX = mX - getCurrentSprite().getWidth() / 2 - 2;
            rockerWing.mY = mY - getCurrentSprite().getHeight() / 2 + 30;
            rockerWing.update(gameTime);
            rockPos.X = mX + 12;
            rockPos.Y = mY + 65;

            //provisorio
            if (!attacking)
            {
                if (mX < GamePlayScreen.sCURRENT_STAGE_X && tempMove == false)
                {
                    offsetX  = -3;
                    tempMove = true;
                }

                if (tempMove == true)
                {
                    changeState(sSTATE_FLYING_RIGHT);
                    moveRight(3);
                }

                if (mX > GamePlayScreen.sCURRENT_STAGE_X + 800 - 120 && tempMove == true)
                {
                    tempMove = false;
                }

                if (tempMove == false)
                {
                    offsetX = 3;
                    changeState(sSTATE_FLYING_LEFT);
                    moveLeft(3);
                }

                if (mX + (getCurrentSprite().getWidth() / 2) > getPlayerCenter() - 2 && (mX + getCurrentSprite().getWidth() / 2) < getPlayerCenter() + 2)
                {
                    attacking = true;
                    offsetX   = 0;
                    changeState(sSTATE_FLYING_STOPPED);
                }
                x += 0.1f;
                float sinMov = 5.0f * (float)Math.Sin(x);
                mY += sinMov;
            }

            if (getState() == sSTATE_FLYING_STOPPED)
            {
                if (attackTimer < 60)
                {
                    attackTimer++;
                }
                else
                {
                    changeState(sSTATE_FLYING_ATTACKING);
                    RockManager.getInstance().createObject(new Vector2((int)rockPos.X + offsetX, (int)rockPos.Y + offsetY));
                    isRockActive = false;
                }
            }

            if (getState() == sSTATE_FLYING_ATTACKING)
            {
                if (waitTimer < 60)
                {
                    waitTimer++;
                }
                else
                {
                    attackTimer  = 0;
                    waitTimer    = 0;
                    alphaTexture = 0;
                    attacking    = false;
                    isRockActive = true;
                }
            }
            if (alphaTexture < 1)
            {
                alphaTexture += 0.01f;
            }
        }
Пример #5
0
    void Update()
    {
        float time = Time.time + timeOffset;

        // resting position
        Vector3 idleHandTarget = transform.position + new Vector3(Mathf.Sin(time) * .35f, 1f + Mathf.Cos(time * 1.618f) * .5f, 1.5f);

        if (heldRock == null && windupTimer <= 0f)
        {
            if (intendedRock == null && reachTimer == 0f)
            {
                // we're idle - see if we can grab a rock
                Rock nearestRock = RockManager.NearestConveyorRock(transform.position - Vector3.right * .5f);
                if (nearestRock != null)
                {
                    if ((nearestRock.position - transform.position).sqrMagnitude < maxReachLength * maxReachLength)
                    {
                        // found a rock to grab!
                        // mark it as reserved so other hands don't reach for it
                        intendedRock          = nearestRock;
                        intendedRock.reserved = true;
                        lastIntendedRockSize  = intendedRock.size;
                    }
                }
            }
            else if (intendedRock == null)
            {
                // stop reaching if we've lost our target
                reachTimer -= Time.deltaTime / reachDuration;
            }

            if (intendedRock != null)
            {
                // we're reaching for a rock (but we haven't grabbed it yet)
                Vector3 delta = intendedRock.position - transform.position;
                if (delta.sqrMagnitude < maxReachLength * maxReachLength)
                {
                    // figure out where we want to put our wrist
                    // in order to grab the rock
                    Vector3 flatDelta = delta;
                    flatDelta.y = 0f;
                    flatDelta.Normalize();
                    grabHandTarget      = intendedRock.position + Vector3.up * intendedRock.size * .5f - flatDelta * intendedRock.size * .5f;
                    lastIntendedRockPos = intendedRock.position;

                    reachTimer += Time.deltaTime / reachDuration;
                    if (reachTimer >= 1f)
                    {
                        // we've arrived at the rock - pick it up
                        heldRock = intendedRock;
                        RockManager.RemoveFromConveyor(heldRock);
                        heldRock.state = Rock.State.Held;
                        // remember the rock's position in "hand space"
                        // (so we can position the rock while holding it)
                        heldRockOffset = handMatrix.inverse.MultiplyPoint3x4(heldRock.position);
                        intendedRock   = null;

                        // random minimum delay before starting the windup
                        windupTimer = Random.Range(-1f, 0f);
                        throwTimer  = 0f;
                    }
                }
                else
                {
                    // we didn't grab the rock in time - forget it
                    intendedRock.reserved = false;
                    intendedRock          = null;
                }
            }
        }
        if (heldRock != null)
        {
            // stop reaching after we've successfully grabbed a rock
            reachTimer -= Time.deltaTime / reachDuration;

            if (targetCan == null)
            {
                // find a target
                targetCan = TinCanManager.GetNearestCan(transform.position, true, targetXRange);
            }
            if (targetCan != null)
            {
                // found a target - prepare to throw
                targetCan.reserved = true;
                windupTimer       += Time.deltaTime / windupDuration;
            }
        }

        reachTimer = Mathf.Clamp01(reachTimer);

        // smoothed reach timer
        float grabT = reachTimer;

        grabT = 3f * grabT * grabT - 2f * grabT * grabT * grabT;

        // reaching overrides our idle hand position
        handTarget = Vector3.Lerp(idleHandTarget, grabHandTarget, grabT);

        if (targetCan != null)
        {
            // we've got a target, which means we're currently throwing
            if (windupTimer < 1f)
            {
                // still winding up...
                float windupT = Mathf.Clamp01(windupTimer) - Mathf.Clamp01(throwTimer * 2f);
                windupT    = 3f * windupT * windupT - 2f * windupT * windupT * windupT;
                handTarget = Vector3.Lerp(handTarget, windupHandTarget, windupT);
                Vector3 flatTargetDelta = targetCan.position - transform.position;
                flatTargetDelta.y = 0f;
                flatTargetDelta.Normalize();

                // windup position is "behind us," relative to the target position
                windupHandTarget = transform.position - flatTargetDelta * 2f + Vector3.up * (3f - windupT * 2.5f);
            }
            else
            {
                // done winding up - actual throw, plus resetting to idle
                throwTimer += Time.deltaTime / throwDuration;

                // update our aim until we release the rock
                if (heldRock != null)
                {
                    aimVector = AimAtCan(targetCan, lastIntendedRockPos);
                }

                // we start this animation in our windup position,
                // and end it by returning to our default idle pose
                Vector3 restingPos = Vector3.Lerp(windupHandTarget, handTarget, throwTimer);

                // find the hand's target position to perform the throw
                // (somewhere forward and upward from the windup position)
                Vector3 throwHandTarget = windupHandTarget + aimVector.normalized * 2.5f;

                handTarget = Vector3.LerpUnclamped(restingPos, throwHandTarget, throwCurve.Evaluate(throwTimer));

                if (throwTimer > .15f && heldRock != null)
                {
                    // release the rock
                    heldRock.reserved = false;
                    heldRock.state    = Rock.State.Thrown;
                    heldRock.velocity = aimVector;
                    heldRock          = null;
                }

                if (throwTimer >= 1f)
                {
                    // we've completed the animation - return to idle
                    windupTimer = 0f;
                    throwTimer  = 0f;
                    TinCanManager.UnreserveCanAfterDelay(targetCan, 3f);
                    targetCan = null;
                }
            }
        }

        // solve the arm IK chain first
        FABRIK.Solve(armChain, armBoneLength, transform.position, handTarget, handUp * armBendStrength);

        // figure out our current "hand vectors" from our arm orientation
        handForward = (armChain.Last(0) - armChain.Last(1)).normalized;
        handUp      = Vector3.Cross(handForward, transform.right).normalized;
        handRight   = Vector3.Cross(handUp, handForward);

        // create handspace-to-worldspace matrix
        handMatrix = Matrix4x4.TRS(armChain.Last(), Quaternion.LookRotation(handForward, handUp), Vector3.one);

        // how much are our fingers gripping?
        // (during a reach, this is based on the reach timer)
        float fingerGrabT = grabT;

        if (heldRock != null)
        {
            // move our held rock to match our new hand position
            heldRock.position   = handMatrix.MultiplyPoint3x4(heldRockOffset);
            lastIntendedRockPos = heldRock.position;

            // if we're holding a rock, we're always gripping
            fingerGrabT = 1f;
        }

        // create rendering matrices for arm bones
        UpdateMatrices(armChain, 0, armBoneThickness, handUp);
        int matrixIndex = armChain.Length - 1;

        // next:  fingers

        Vector3 handPos = armChain.Last();
        // fingers spread out during a throw
        float openPalm = throwCurve.Evaluate(throwTimer);

        for (int i = 0; i < fingerChains.Length; i++)
        {
            // find knuckle position for this finger
            Vector3 fingerPos = handPos + handRight * (fingerXOffset + i * fingerSpacing);

            // find resting position for this fingertip
            Vector3 fingerTarget = fingerPos + handForward * (.5f - .1f * fingerGrabT);

            // spooky finger wiggling while we're idle
            fingerTarget += handUp * Mathf.Sin((time + i * .2f) * 3f) * .2f * (1f - fingerGrabT);

            // if we're gripping, move this fingertip onto the surface of our rock
            Vector3 rockFingerDelta = fingerTarget - lastIntendedRockPos;
            Vector3 rockFingerPos   = lastIntendedRockPos + rockFingerDelta.normalized * (lastIntendedRockSize * .5f + fingerThicknesses[i]);
            fingerTarget = Vector3.Lerp(fingerTarget, rockFingerPos, fingerGrabT);

            // apply finger-spreading during throw animation
            fingerTarget += (handUp * .3f + handForward * .1f + handRight * (i - 1.5f) * .1f) * openPalm;

            // solve this finger's IK chain
            FABRIK.Solve(fingerChains[i], fingerBoneLengths[i], fingerPos, fingerTarget, handUp * fingerBendStrength);

            // update this finger's rendering matrices
            UpdateMatrices(fingerChains[i], matrixIndex, fingerThicknesses[i], handUp);
            matrixIndex += fingerChains[i].Length - 1;
        }

        // the thumb is pretty much the same as the fingers
        // (but pointing in a strange direction)
        Vector3 thumbPos    = handPos + handRight * thumbXOffset;
        Vector3 thumbTarget = thumbPos - handRight * .15f + handForward * (.2f + .1f * fingerGrabT) - handUp * .1f;

        thumbTarget += handRight * Mathf.Sin(time * 3f + .5f) * .1f * (1f - fingerGrabT);
        // thumb bends away from the palm, instead of "upward" like the fingers
        Vector3 thumbBendHint = (-handRight - handForward * .5f);

        Vector3 rockThumbDelta = thumbTarget - lastIntendedRockPos;
        Vector3 rockThumbPos   = lastIntendedRockPos + rockThumbDelta.normalized * (lastIntendedRockSize * .5f);

        thumbTarget = Vector3.Lerp(thumbTarget, rockThumbPos, fingerGrabT);

        FABRIK.Solve(thumbChain, thumbBoneLength, thumbPos, thumbTarget, thumbBendHint * thumbBendStrength);

        UpdateMatrices(thumbChain, matrixIndex, thumbThickness, thumbBendHint);

        // draw all of our bones
        Graphics.DrawMeshInstanced(boneMesh, 0, material, matrices);
    }