Example #1
0
    public Transform CreateNewGeometryAt(float startX)
    {
        GameObject holder = new GameObject("Terrain_" + startX);

        holder.transform.parent = this.transform;

        for (int i = 0; i < SEGMENTS_PER_GROUP; i++)
        {
            // Calculating the start and end points is pretty easy using Unity's built in perlin noise:
            float   x          = startX + i * lengthPerSegment;
            Vector3 startPoint = new Vector2(x, GetY(x));
            Vector3 endPoint   = new Vector2(x + lengthPerSegment, GetY(x + lengthPerSegment));

            // We can draw a line pretty simply to see our terrain:
            Debug.DrawLine(startPoint, endPoint, i % 2 == 1 ? Color.blue : Color.red, 10f);

            // But we want to rotate and resize default Unity cubes to match these endpoints, so we need some math...
            // Segment length is used as the scale.
            Vector3 delta         = endPoint - startPoint;
            float   segmentLength = delta.magnitude;

            // find the rotation:
            Quaternion rot = Quaternion.FromToRotation(Vector3.right, delta);

            // determine center position of segment:
            Vector3 center = startPoint + delta / 2f;

            GameObject segment = Instantiate(terrainChunkPrefab, center, rot) as GameObject;
            segment.transform.localScale = new Vector3(segmentLength, 1f, 1f);
            Chipmunk.UpdatedTransform(segment);

            // That's all we need to do! If you want, you can edit the Chipmunk Shape like this:
            ChipmunkSegmentShape shape = segment.GetComponent <ChipmunkSegmentShape>();
            shape.friction = 1.5f;
            // and anything else you might want to do with the shape...

            // Parent the new object.
            segment.transform.parent = holder.transform;
        }


        return(holder.transform);
    }
    protected void Update()
    {
        // Works for the mouse or a single touch. If doing multitouch, replace with with a loop over Input.touches
        // and adapt accordingly
        if (Input.GetMouseButtonDown(0))
        {
            //Debug.Log ("mouse: "+ Input.mousePosition + " position: " + GetPosition(Input.mousePosition));

            ChipmunkNearestPointQueryInfo info;
            Chipmunk.NearestPointQueryNearest(mouseBody.position, fingerThickness, out info);
            //Debug.Log ("Grabbed shape: " + info.shape);

            if (info.shape != null)
            {
                if (info.shape.body == null)
                {
                    // We clicked on a static shape. You can't drag those!
                    return;
                }

                grabbedShape = info.shape;

                mouseConstraint       = mouseBody.gameObject.AddComponent(typeof(ChipmunkPivotJoint)) as ChipmunkPivotJoint;
                mouseConstraint.bodyB = grabbedShape.body;
                mouseConstraint.pivot = Vector2.zero;

                // high but not infinite. You can push heavy things, but not force yourself through things.
                mouseConstraint.maxForce = 1e3f;

                // 60f = the approximate intended frame rate
                mouseConstraint.errorBias = Mathf.Pow(1.0f - 0.15f, 60f);
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            // remove mouse constraint.
            Destroy(mouseConstraint);
        }
    }
Example #3
0
 public static CCPhysicsDebugNode DebugNode(Chipmunk.Space space)
 {
     return DebugNode (space.Handle.Handle);
 }
Example #4
0
    // Update is called once per frame
    void Update()
    {
        bool isIdle = true;

        // This sets the correct jump status when the player without jumping enters on free fall state.
        // Also corrects a sprite animation flickering when walking because the animation starts again
        // constantly after jump.resetStatus()
        if (exitedFromScenery && !jump.isJumping())
        {
            ChipmunkSegmentQueryInfo qinfo;
            // check if there is no shape below us
            Vector2 end = body.position + queryOffset;
            Chipmunk.SegmentQueryFirst(body.position, end, collisionLayersSkip, collisionGroupSkip, out qinfo);
            // if no handler it means no hit
            if (System.IntPtr.Zero == qinfo._shapeHandle)
            {
                jump.reset();                 // set state as if were jumping
            }
        }

        // jump
        if (Gamepad.Instance.isA())
        {
            walk.stop();
            jump.jump(lightJumpVelocity);
            // apply gain jump power. Only once per jump (handled in Jump component)
            if (Gamepad.Instance.isHardPressed(EnumButton.A))
            {
                jump.applyGain(gainJumpFactor);
            }
            isIdle = false;
        }

        // power up action
        if (powerUp != null && powerUp.ableToUse())
        {
            powerUp.action(gameObject);
        }

        // walk
        if (Gamepad.Instance.isLeft())
        {
            // is speed up button being pressed?
            if (Gamepad.Instance.isB())
            {
                walk.setGain(walk.speedUpFactor);
            }
            else
            {
                walk.setGain(1f);
            }
            walk.walk(-walkVelocity);
            fireDir = leftFireDir;
        }
        else if (Gamepad.Instance.isRight())
        {
            // is speed up button being pressed?
            if (Gamepad.Instance.isB())
            {
                walk.setGain(walk.speedUpFactor);
            }
            else
            {
                walk.setGain(1f);
            }
            walk.walk(walkVelocity);
            fireDir = rightFireDir;
        }
        if (walk.isWalking())
        {
            isIdle = false;
        }

        // crouch
        if (Gamepad.Instance.isDown())
        {
            crouch.crouch();
            isIdle = false;
        }
        else
        {
            crouch.noCrouch();
        }

        // look upwards/downwards
        if (!jump.isJumping())
        {
            if (Gamepad.Instance.isUp())
            {
                lookDirections.lookUpwards();
                isIdle = false;
            }
            else
            {
                lookDirections.restore();
            }
        }
        else
        {
            lookDirections.lockYWhenJumping();
        }

        // finally only if not doing any action then set idle state
        if (isIdle)
        {
            idle.setIdle(false);
        }
    }
Example #5
0
    // Update is called once per frame
    void Update()
    {
        // this set the correct jump status when the player without jumping enters on free fall state
        // and also correct a sprite animation flickering when walking because the animation starts again
        // constantly after jump.resetStatus()
        if (exitedFromScenery && !jump.IsJumping())
        {
            // check if there is no shape below us
            ChipmunkSegmentQueryInfo qinfo;
            Vector2 end = body.position + queryOffset;
            Chipmunk.SegmentQueryFirst(body.position, end, collisionLayers, collisionGroupSkip, out qinfo);
            // if no handler it means no hit
            if (System.IntPtr.Zero == qinfo._shapeHandle)
            {
                // set state as if were jumping
                jump.resetStatus();
            }
        }

        bool isIdle = true;

        // jump
        if (Gamepad.isA() || Input.GetButton("Jump"))
        {
            walk.stopWalking();             // it resets walk behavior
            jump.jump(lightJumpVelocity);
            // apply gain jump power. Only once per jump (handled in Jump component)
            if (Gamepad.isHardPressed(EnumButton.A))
            {
                jump.applyGain(gainJumpFactor);
            }
            isIdle = false;
        }

        // power up action
        if (powerUp != null && powerUp.ableToUse())
        {
            powerUp.action(gameObject);
        }

        // move
        walk.enableWalking();
        if (Gamepad.isLeft() || Input.GetAxis("Horizontal") < -0.1f)
        {
            walk.walk(-walkVelocity);
            fireDir = leftFireDir;
            isIdle  = false;
            // enable move speed after a wall collision if intended moving direction changes
            if (signCollision > 0f)
            {
                restoreWalkVel();
            }
        }
        else if (Gamepad.isRight() || Input.GetAxis("Horizontal") > 0.1f)
        {
            walk.walk(walkVelocity);
            fireDir = rightFireDir;
            isIdle  = false;
            // enable move speed after a wall collision if intended moving direction changes
            if (signCollision < 0f)
            {
                restoreWalkVel();
            }
        }
        else
        {
            // if no movement input then set correct internal status
            walk.stopWalking();
        }

        // crouch
        if (Gamepad.isDown() || Input.GetAxis("Vertical") < -0.1f)
        {
            crouch.crouch();
            isIdle = false;
        }
        else
        {
            crouch.noCrouch();
        }

        // look upwards
        if (!jump.IsJumping())
        {
            if (Gamepad.isUp() || Input.GetAxis("Vertical") > 0.1f)
            {
                lookUpwards.lookUpwards();
                isIdle = false;
            }
            else
            {
                lookUpwards.restore();
            }
        }
        else
        {
            lookUpwards.lockYWhenJumping();
        }

        // finally only if no doing any action then set idle state
        if (isIdle)
        {
            idle.setIdle(false);
        }
    }