void LateInitialize()
    {
        defaultCameraPos = transform.Find("Main Camera").localPosition;
        targetCameraPos = defaultCameraPos;
        currentTextureSpeedModifier = 1.0f;
        childKart = transform.Find("Kart");
        originalRotation = childKart.transform.localRotation;
        rigidbody = gameObject.GetComponent<Rigidbody>();
        boxCollider = gameObject.GetComponent<BoxCollider>();
        kartInformation = gameObject.transform.Find("Kart").GetComponent<KartInformation>();

        //stats
        defaultMaxSpeed = 65;
        maxSpeed = defaultMaxSpeed;
        maxSpeedChange = 0;
        maxReverse = 15;
        turnSpeed = 100;
        acceleration = 0.25f;
        brakeForce = 0.95f;
        engineDeceleration = 0.15f;
        spinSpeed = 250;
        stabilizeTorqueForce = 2000.0f;

        //common
        jumpLimit = 2.5f;
        state = new Stopped(this.gameObject);
        mainCamera = transform.FindChild("Main Camera").gameObject;
        pw = gameObject.GetComponent<PlayerNetwork>();
        oldPosition = transform.position;
        groundNormal = Vector3.up;

        childKart.localPosition = new Vector3(0, 0, 0);
        childKart.localEulerAngles = new Vector3(0, -90, 0);
        lateInitialized = true;
    }
Exemple #2
0
 public KartState(GameObject _kart, KartState _lastState = null)
 {
     kart      = _kart;
     lastState = _lastState;
     name      = "nameless state";
     kb        = kart.GetComponent <KartBehaviour>();
 }
Exemple #3
0
    void LateInitialize()
    {
        defaultCameraPos            = transform.Find("Main Camera").localPosition;
        targetCameraPos             = defaultCameraPos;
        currentTextureSpeedModifier = 1.0f;
        childKart        = transform.Find("Kart");
        originalRotation = childKart.transform.localRotation;
        rigidbody        = gameObject.GetComponent <Rigidbody>();
        boxCollider      = gameObject.GetComponent <BoxCollider>();
        kartInformation  = gameObject.transform.Find("Kart").GetComponent <KartInformation>();

        //stats
        defaultMaxSpeed      = 65;
        maxSpeed             = defaultMaxSpeed;
        maxSpeedChange       = 0;
        maxReverse           = 15;
        turnSpeed            = 100;
        acceleration         = 0.25f;
        brakeForce           = 0.95f;
        engineDeceleration   = 0.15f;
        spinSpeed            = 250;
        stabilizeTorqueForce = 2000.0f;

        //common
        jumpLimit    = 2.5f;
        state        = new Stopped(this.gameObject);
        mainCamera   = transform.FindChild("Main Camera").gameObject;
        pw           = gameObject.GetComponent <PlayerNetwork>();
        oldPosition  = transform.position;
        groundNormal = Vector3.up;

        childKart.localPosition    = new Vector3(0, 0, 0);
        childKart.localEulerAngles = new Vector3(0, -90, 0);
        lateInitialized            = true;
    }
Exemple #4
0
 public KartState(GameObject _kart, KartState _lastState = null)
 {
     kart = _kart;
     lastState = _lastState;
     name = "nameless state";
     kb = kart.GetComponent<KartBehaviour>();
 }
 //==( FUNCTIONS )=========================================================//
 void Start()
 {
     kartState        = KartState.Drive;
     offsetPlus       = 1f;
     targetOffsetPlus = 1;
     velThreshold     = 40f;
     SetCameraTarget(target);
 }
 void CheckState()
 {
     if (charController.DriftInput > 0)
     {
         kartState = KartState.Drift;
     }
     else
     {
         kartState = KartState.Drive;
     }
 }
Exemple #7
0
    // Update is called once per frame
    void Update()
    {
        if (!lateInitialized)
        {
            if (transform.Find("Kart") != null)
            {
                LateInitialize();
            }
            else
            {
                return;
            }
        }
        if (networkState != null)
        {
            state        = networkState;
            networkState = null;
        }

        //state
        KartState tempState = state.UpdateState();

        if (tempState != null)
        {
            state = tempState;
        }

        //speedometer
        trueSpeedTimer += Time.deltaTime;
        if (trueSpeedTimer > 0.25f)
        {
            trueSpeed      = Vector3.Distance(transform.position, oldPosition) / trueSpeedTimer;
            trueSpeedTimer = 0;
            oldPosition    = transform.position;
        }
        Vector3 cameraPos = transform.Find("Main Camera").localPosition;

        if (!GetComponent <Placement>().gameFinished)
        {
            transform.Find("Main Camera").localPosition = Vector3.Lerp(cameraPos, targetCameraPos, Time.deltaTime);
        }
        if (transform.position.y <= -200)
        {
            Reset(0, true);
        }

        turnWheels();
    }
Exemple #8
0
 public void Airborne()
 {
     f_yVelocity = f_yVelocity+f_gravity*Time.deltaTime;
     float f_ySample;
     if(b_onTrack) {
         f_ySample = c_terrainGen.SampleTerrain(v2_pos,c_terrainGen.f_trackRoughness)*c_terrainGen.i_yRes+f_radius;
     }
     else {
         f_ySample = Mathf.Ceil(c_terrainGen.SampleTerrain(v2_pos,c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
     }
     if((transform.position.y+f_yVelocity*Time.deltaTime) < f_ySample /*&& f_ySample-f_bumperY < 0.5f*/){
         transform.position = new Vector3(transform.position.x,f_ySample,transform.position.z);
         state = KartState.grounded;
     }
     else transform.Translate(0,f_yVelocity*Time.deltaTime,0);
 }
Exemple #9
0
    // Use this for initialization
    void Start()
    {
        gamePadState     = ControllerManager.Instance.GamePadStates[PlayerIndex];
        lastGamePadState = ControllerManager.Instance.LastGamePadStates[PlayerIndex];
        m_input          = new KartInput(gamePadState, lastGamePadState);

        KartState  = new KartState();
        m_animator = GetComponent <Animator>();
        KartRigidbody.transform       = transform;
        KartRigidbody.position        = transform.position;
        KartTransformer.KartRigidbody = KartRigidbody;

        KartRigidbody.KartTransformer = KartTransformer;
        KartRigidbody.Initialize();

        WeaponPrefab = new List <GameObject>();

        TraceL = GetComponentsInChildren <SlidingTrace>()[0];
        TraceR = GetComponentsInChildren <SlidingTrace>()[1];
    }
Exemple #10
0
    // Use this for initialization
    void Start()
    {
        kc         = GetComponent <KartController>();
        kart_state = GetComponent <KartState>();


        foreach (Transform child in transform)
        {
            if (child.name == "kartSmoke")
            {
                smoke.Add(child.gameObject);
                continue;
            }
            if (child.name != "steering")
            {
                continue;
            }
            wheels["steering"] = child;
            foreach (Transform w in child.transform)
            {
                wheels[w.name] = w;
            }
        }
    }
Exemple #11
0
    public void Grounded()
    {
        f_zAngleDelta = (-Mathf.Atan2((f_yDelta),(f_zDelta))-f_zAngle);
        f_zAngle = -Mathf.Atan2((f_yDelta),(f_zDelta));//print(f_zAngle + " " + c_kartAngleCalc.f_xAngle);
        f_angleBreak = 20f*Mathf.Clamp(1f-Mathf.Clamp(f_mVelocity,0,f_mMaxVelocity)/(f_mMaxVelocity*1.01f),0,1f);

        float f_zRadians = c_kartAngleCalc.f_xAngle * Mathf.PI/180f;
        float f_zVelocityRatio = Mathf.Cos(f_zRadians);
        float f_yVelocityRatio = Mathf.Sin(f_zRadians);
        f_zVelocity = f_zVelocityRatio*f_mVelocity;

        //f_yVelocity = -f_yVelocityRatio*f_mVelocity;
        f_xVelocity = f_xDelta;
        f_yVelocity = f_yDelta;

        f_modTurnStrength = f_turnStrength*Mathf.Abs(1f-Mathf.Abs(Mathf.Abs(f_mVelocity)-f_mMaxVelocity*0.75f)/(f_mMaxVelocity*0.75f));

        if(b_onTrack) {
            f_ySample = c_terrainGen.SampleTerrain(v2_pos,c_terrainGen.f_trackRoughness)*c_terrainGen.i_yRes+f_radius;
        }
        else {
            float f_ySampleBL,f_ySampleTL,f_ySampleBR,f_ySampleTR;
            f_ySampleBL = Mathf.Ceil(c_terrainGen.SampleTerrain(new Vector2(Mathf.Round(v2_pos.x)-0.5f,Mathf.Round(v2_pos.y)-0.5f),c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
            f_ySampleTL = Mathf.Ceil(c_terrainGen.SampleTerrain(new Vector2(Mathf.Round(v2_pos.x)-0.5f,Mathf.Round(v2_pos.y)+0.5f),c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
            f_ySampleBR = Mathf.Ceil(c_terrainGen.SampleTerrain(new Vector2(Mathf.Round(v2_pos.x)+0.5f,Mathf.Round(v2_pos.y)-0.5f),c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
            f_ySampleTR = Mathf.Ceil(c_terrainGen.SampleTerrain(new Vector2(Mathf.Round(v2_pos.x)+0.5f,Mathf.Round(v2_pos.y)+0.5f),c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
            f_ySample = Mathf.Lerp(Mathf.Lerp(f_ySampleBL,f_ySampleTL,v2_pos.y-(Mathf.Round(v2_pos.y)-0.5f)),Mathf.Lerp(f_ySampleBR,f_ySampleTR,v2_pos.y-(Mathf.Round(v2_pos.y)-0.5f)),v2_pos.x-(Mathf.Round(v2_pos.x)-0.5f));
        }

        transform.position = new Vector3(transform.position.x,f_ySample,transform.position.z);

        if (!b_amISpinningOutRightNow) {
            if(!b_AI)f_normalTurnStrength = Mathf.Clamp((Input.GetAxis("p"+s_player+"Steer") / 0.08906883f), -1.0f, 1.0f);
            if	((!b_AI && (f_normalTurnStrength < 0)|| Input.GetKey(KeyCode.A)) || (b_AI && i_AIDirection == 0)){
                if(f_mVelocity > 0)
                    transform.Rotate(0,(-f_modTurnStrength-f_driftVelocity)*Mathf.Abs(f_normalTurnStrength)*Time.deltaTime,0);
                else
                    transform.Rotate(0,f_modTurnStrength*Mathf.Abs(f_normalTurnStrength)*Time.deltaTime,0);
            }
            else if((!b_AI && (f_normalTurnStrength > 0) || Input.GetKey(KeyCode.D)) || (b_AI && i_AIDirection == 1)) {
                if(f_mVelocity > 0)
                    transform.Rotate(0,(f_modTurnStrength-f_driftVelocity)*Mathf.Abs(f_normalTurnStrength)*Time.deltaTime,0);
                else
                    transform.Rotate(0,-f_modTurnStrength*Mathf.Abs(f_normalTurnStrength)*Time.deltaTime,0);
            }
        }

        if((!b_AI && (Input.GetAxis("p"+s_player+"Accel") > 0) || Input.GetKey(KeyCode.W)) || (b_AI && b_AIForward)) {
            if (!b_amISpinningOutRightNow) {
                f_mVelocity = f_mVelocity+f_acceleration*Time.deltaTime;
                if(f_mVelocity < 0)
                    f_mVelocity = f_mVelocity+f_handbreakValue*Time.deltaTime;
                }
            else if(f_mVelocity > 0.1f) f_mVelocity = f_mVelocity-f_acceleration*f_spinSlowStrength*Time.deltaTime;
            else if(f_mVelocity < -0.1f) f_mVelocity = f_mVelocity+f_acceleration*f_spinSlowStrength*Time.deltaTime;
            else if(f_mVelocity >= -0.1f && f_mVelocity <= 0.1f) f_mVelocity = 0;

        }
        else if((!b_AI && (Input.GetAxis("p"+s_player+"Accel") < 0) || Input.GetKey(KeyCode.S))) {
            if (!b_amISpinningOutRightNow) {
                f_mVelocity = f_mVelocity-f_acceleration*Time.deltaTime;
                if(f_mVelocity > 0)
                    f_mVelocity = f_mVelocity-f_handbreakValue*Time.deltaTime;
                }
        }
        else if(f_mVelocity > 0.1f) f_mVelocity = f_mVelocity-f_acceleration*f_slowDownValue*Time.deltaTime;
        else if(f_mVelocity < -0.1f) f_mVelocity = f_mVelocity+f_acceleration*f_slowDownValue*Time.deltaTime;
        else if(f_mVelocity >= -0.1f && f_mVelocity <= 0.1f) f_mVelocity = 0;
        else f_mVelocity = 0f;

        if (!b_AI && Input.GetButton("p"+s_player+"HBrake")) {
            if (f_mVelocity > 0)
                f_mVelocity = f_mVelocity - f_acceleration * 1.5f * Time.deltaTime;
            else if (f_mVelocity < 0)
                f_mVelocity = f_mVelocity + f_acceleration * 1.5f * Time.deltaTime;
        }

        //if (Input.GetButton("p"+s_player+"Item"))
            //UseItem();
        if(b_onTrack) {
            if(f_mVelocity > f_mMaxVelocity) f_mVelocity = f_mVelocity-f_acceleration*1.1f*Time.deltaTime;
            if(f_mVelocity < -f_mMaxVelocity*0.5f) f_mVelocity = f_mVelocity+f_acceleration*1.1f*Time.deltaTime;
        }
        else {
            if(f_mVelocity > f_mMaxVelocity*0.75f) f_mVelocity = f_mVelocity-f_acceleration*1.1f*Time.deltaTime;
            if(f_mVelocity < -f_mMaxVelocity*0.5f*0.75f) f_mVelocity = f_mVelocity+f_acceleration*1.1f*Time.deltaTime;
        }
        //if(f_bumperY > f_curY+0.5f && !b_bumperOnTrack)
        //	f_mVelocity = -f_mVelocity*0.5f;

        if(b_onTrack && (f_zAngleDelta-f_zAngle*0.01f) > f_angleBreak)
            state = KartState.airborne;
    }
Exemple #12
0
    public void Grounded()
    {
        f_zAngleDelta = (-Mathf.Atan2((f_yDelta),(f_zDelta))-f_zAngle);
        f_zAngle = -Mathf.Atan2((f_yDelta),(f_zDelta));//print(f_zAngle + " " + c_kartAngleCalc.f_xAngle);
        f_angleBreak = 5f*Mathf.Clamp(1f-Mathf.Clamp(f_mVelocity,0,f_mMaxVelocity)/(f_mMaxVelocity*1.01f),0,1f);

        float f_zRadians = c_kartAngleCalc.f_xAngle * Mathf.PI/180f;
        float f_zVelocityRatio = Mathf.Cos(f_zRadians);
        float f_yVelocityRatio = Mathf.Sin(f_zRadians);
        f_zVelocity = f_zVelocityRatio*f_mVelocity;

        //f_yVelocity = -f_yVelocityRatio*f_mVelocity;
        f_yVelocity = f_yDelta;

        f_modTurnStrength = f_turnStrength*Mathf.Abs(1f-Mathf.Abs(Mathf.Abs(f_mVelocity)-f_mMaxVelocity*0.85f)/(f_mMaxVelocity*0.85f));

        if(b_onTrack) {
            f_ySample = c_terrainGen.SampleTerrain(v2_pos,c_terrainGen.f_trackRoughness)*c_terrainGen.i_yRes+f_radius;
        }
        else {
            float f_ySampleBL,f_ySampleTL,f_ySampleBR,f_ySampleTR;
            f_ySampleBL = Mathf.Ceil(c_terrainGen.SampleTerrain(new Vector2(Mathf.Round(v2_pos.x)-0.5f,Mathf.Round(v2_pos.y)-0.5f),c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
            f_ySampleTL = Mathf.Ceil(c_terrainGen.SampleTerrain(new Vector2(Mathf.Round(v2_pos.x)-0.5f,Mathf.Round(v2_pos.y)+0.5f),c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
            f_ySampleBR = Mathf.Ceil(c_terrainGen.SampleTerrain(new Vector2(Mathf.Round(v2_pos.x)+0.5f,Mathf.Round(v2_pos.y)-0.5f),c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
            f_ySampleTR = Mathf.Ceil(c_terrainGen.SampleTerrain(new Vector2(Mathf.Round(v2_pos.x)+0.5f,Mathf.Round(v2_pos.y)+0.5f),c_terrainGen.f_blendAmount)*c_terrainGen.i_yRes)+f_radius;
            f_ySample = Mathf.Lerp(Mathf.Lerp(f_ySampleBL,f_ySampleTL,v2_pos.y-(Mathf.Round(v2_pos.y)-0.5f)),Mathf.Lerp(f_ySampleBR,f_ySampleTR,v2_pos.y-(Mathf.Round(v2_pos.y)-0.5f)),v2_pos.x-(Mathf.Round(v2_pos.x)-0.5f));
        }

        transform.position = new Vector3(transform.position.x,f_ySample,transform.position.z);
        if	(((Input.GetAxis("p1Steer") < 0f || Input.GetKey(kc_left)) && !b_AI) || (b_AI && i_AIDirection == 0)){
            if(f_mVelocity > 0)
                transform.Rotate(0,-f_modTurnStrength*Time.deltaTime,0);
            else
                transform.Rotate(0,f_modTurnStrength*Time.deltaTime,0);
        }
        else if(((Input.GetAxis("p1Steer") > 0f || Input.GetKey(kc_right)) && !b_AI)|| (b_AI && i_AIDirection == 1)) {
            if(f_mVelocity > 0)
                transform.Rotate(0,f_modTurnStrength*Time.deltaTime,0);
            else
                transform.Rotate(0,-f_modTurnStrength*Time.deltaTime,0);
        }
        if(((Input.GetAxis("p1Accel") > 0 || Input.GetKey(kc_accel)) && !b_AI) || (b_AI && b_AIForward)) {
            f_mVelocity = f_mVelocity+f_acceleration*Time.deltaTime;
            if(f_mVelocity < 0)
                f_mVelocity = f_mVelocity+f_handbreakValue*Time.deltaTime;

        }
        else if(((Input.GetAxis("p1Accel") > 0f|| Input.GetKey(kc_reverse)) && !b_AI)) {
            f_mVelocity = f_mVelocity-f_acceleration*Time.deltaTime;
            if(f_mVelocity > 0)
                f_mVelocity = f_mVelocity-f_handbreakValue*Time.deltaTime;
        }
        else if(f_mVelocity > 0.1f) f_mVelocity = f_mVelocity-f_acceleration*f_slowDownValue*Time.deltaTime;
        else if(f_mVelocity < -0.1f) f_mVelocity = f_mVelocity+f_acceleration*f_slowDownValue*Time.deltaTime;
        else if(f_mVelocity >= -0.1f && f_mVelocity <= 0.1f) f_mVelocity = 0;

        else f_mVelocity = 0f;
        if(b_onTrack) {
            if(f_mVelocity > f_mMaxVelocity) f_mVelocity = f_mVelocity-f_acceleration*1.1f*Time.deltaTime;
            if(f_mVelocity < -f_mMaxVelocity*0.5f) f_mVelocity = f_mVelocity+f_acceleration*1.1f*Time.deltaTime;
        }
        else {
            if(f_mVelocity > f_mMaxVelocity*0.75f) f_mVelocity = f_mVelocity-f_acceleration*1.1f*Time.deltaTime;
            if(f_mVelocity < -f_mMaxVelocity*0.5f*0.75f) f_mVelocity = f_mVelocity+f_acceleration*1.1f*Time.deltaTime;
        }
        if((b_onTrack && (f_zAngleDelta)-f_zAngle*0.01f > f_angleBreak) || (!b_onTrack && f_ySample-0.75f > f_bumperY))
            state = KartState.airborne;
    }
Exemple #13
0
 public void SetState(KartState _state)
 {
     state = _state;
 }
Exemple #14
0
 public void UnFreeze()
 {
     networkState = new Stopped(this.gameObject);
 }
Exemple #15
0
 public void Freeze()
 {
     networkState = new Frozen(this.gameObject);
 }
Exemple #16
0
 public void Init()
 {
     f_ySample = c_terrainGen.SampleTerrain(v2_pos,c_terrainGen.f_trackRoughness)*c_terrainGen.i_yRes+5f+f_radius;
     transform.position = new Vector3(transform.position.x,f_ySample,transform.position.z);
     state = KartState.grounded;
 }
Exemple #17
0
    // Update is called once per frame
    void Update()
    {
        if (!lateInitialized)
        {
            if (transform.Find("Kart") != null)
                LateInitialize();
            else
                return;
        }
        if (networkState != null)
        {
            state = networkState;
            networkState = null;
        }

        //state
        KartState tempState = state.UpdateState();
        if (tempState != null)
            state = tempState;

        //speedometer
        trueSpeedTimer += Time.deltaTime;
        if (trueSpeedTimer > 0.25f)
        {
            trueSpeed = Vector3.Distance(transform.position, oldPosition) / trueSpeedTimer;
            trueSpeedTimer = 0;
            oldPosition = transform.position;
        }
        Vector3 cameraPos = transform.Find("Main Camera").localPosition;
        if(!GetComponent<Placement>().gameFinished)
        transform.Find("Main Camera").localPosition = Vector3.Lerp(cameraPos, targetCameraPos, Time.deltaTime);
        if(transform.position.y <= -200)
        {
            Reset(0, true);
        }

        turnWheels();
    }
Exemple #18
0
 public void SetState(KartState _state)
 {
     state = _state;
 }
Exemple #19
0
 public void UnFreeze()
 {
     networkState = new Stopped(this.gameObject);
 }
Exemple #20
0
 public Ragdoll(GameObject _kart, KartState _lastState, float time) : base(_kart, _lastState)
 {
     timer = 0;
     maxTime = time;
     name = "ragdoll";
 }
Exemple #21
0
 public void Freeze()
 {
     networkState = new Frozen(this.gameObject);
 }
Exemple #22
0
 public Ragdoll(GameObject _kart, KartState _lastState, float time) : base(_kart, _lastState)
 {
     timer   = 0;
     maxTime = time;
     name    = "ragdoll";
 }