Exemplo n.º 1
0
    public override void SetSpeed(float speed, float movement)
    {
        for (int i = 0; i < wheelJoints.Length; i++)
        {
            WheelJoint2D wheelJoint2D = wheelJoints[i];
            if ((bool)wheelJoint2D)
            {
                JointSuspension2D suspension = wheelJoint2D.suspension;
                suspension.frequency    = Variables.instance.wheelSuspensionFrequency;
                suspension.dampingRatio = Variables.instance.wheelSuspensionDampening;
                wheelJoint2D.suspension = suspension;
                JointMotor2D motor = wheelJoint2D.motor;
                motor.motorSpeed   = speed;
                wheelJoint2D.motor = motor;
            }
        }
        for (int j = 0; j < chains.Length; j++)
        {
            chains[j].wheelSpeed = speed;
        }
        float emission = 0f;

        if (movement != 0f)
        {
            emission = Mathf.Lerp(0f, Variables.instance.maxDustPartices, Mathf.Clamp01(1f - Mathf.Abs(speed) / Variables.instance.maxDustParticleSpeed));
        }
        SetDustEmission(emission, movement * 10f);
        SetFumeEmission(Variables.instance.defaultFumes + Variables.instance.speedFumes * Mathf.Abs(movement));
        if (audioSourceEngine != null)
        {
            audioSourceEngine.pitch  = Mathf.Clamp(audioSourceEngine.pitch + (float)((movement != 0f) ? 1 : (-2)) * Variables.instance.engineSoundAlteringSpeed * Time.deltaTime, 1f, (tankType == PlayerTankType.Arachno) ? GetComponent <Walker>().engineMaxPitch : Variables.instance.engineMaxPitch);
            audioSourceEngine.volume = Mathf.Clamp(audioSourceEngine.volume + (float)((movement != 0f) ? 1 : (-2)) * Variables.instance.engineSoundAlteringSpeed * Time.deltaTime, (tankType == PlayerTankType.Arachno) ? 0f : Variables.instance.engineMinVolume, Variables.instance.engineMaxVolume);
        }
    }
Exemplo n.º 2
0
    public void AttachPart(GameObject carObj)
    {
        WheelJoint2D joint = carObj.AddComponent <WheelJoint2D>();
        GameObject   wheel = Instantiate(wheelModel, transform.position, Quaternion.identity, carObj.transform);

        joint.connectedBody = wheel.GetComponent <Rigidbody2D>();
        joint.anchor        = transform.position;

        JointSuspension2D suspension = new JointSuspension2D {
            frequency = wheelSuspention, angle = 90, dampingRatio = .7f
        };

        joint.suspension = suspension;

        JointMotor2D motor = new JointMotor2D {
            motorSpeed = wheelSpeed, maxMotorTorque = 10000
        };

        joint.motor = motor;

        joint.useMotor = false;

        if (isMotorWheel)
        {
            carObj.GetComponent <CarMover>().AddMotorWheel(joint);
        }
    }
 protected override void OnExecute()
 {
     _motor      = agent.motor;
     _suspension = agent.suspension;
     SetProperties();
     EndAction();
 }
Exemplo n.º 4
0
    LocomotionComponent AttachLeg(GameObject socket)
    {
        // make WheelJoint2D on Vehicle body
        WheelJoint2D legJoint = gameObject.AddComponent <WheelJoint2D>();
        GameObject   l        = Instantiate(leg) as GameObject;

        l.transform.parent        = transform;
        l.transform.localPosition = socket.transform.localPosition;
        legJoint.connectedBody    = l.GetComponent <Rigidbody2D>();
        legJoint.connectedAnchor  = l.transform.localPosition;
        legJoint.anchor           = socket.transform.localPosition;
        legJoint.useMotor         = true;
        JointSuspension2D sus = new JointSuspension2D();

        sus.frequency       = 5;
        sus.dampingRatio    = 1;
        legJoint.suspension = sus;
        JointMotor2D newMotor = new JointMotor2D();

        newMotor.motorSpeed     = -300;
        newMotor.maxMotorTorque = 100;
        legJoint.motor          = newMotor;

        SpinningLeg spinleg = l.GetComponent <SpinningLeg>();

        spinleg.SetRefToJoint(legJoint);
        return(spinleg);
    }
Exemplo n.º 5
0
    // Add a WheelJoint2D to the wheel contained in the array in a specific position
    public void addWheelJoint2D(int wheelPosition)
    {
        // Add the WheelJoint2D component to the wheel
        WheelJoint2D wheelJoint = gameObject.AddComponent <WheelJoint2D>() as WheelJoint2D;

        // Verify if the wheel has a rigidbody2D.
        if (!wheels[wheelPosition].GetComponent <Rigidbody2D>())
        {
            // Add a rigidbody2D to the wheel.
            wheels[wheelPosition].AddComponent <Rigidbody2D>();
        }
        // Set the rigidbody2D of the car to the wheelJoint2D
        wheelJoint.connectedBody = wheels[wheelPosition].GetComponent <Rigidbody2D>();
        // Create a new JointSuspension2D and set the variables to get a cool and real suspension
        JointSuspension2D suspension = new JointSuspension2D();

        // Set the angle of the suspension to 90 degrees.
        suspension.angle = 90.0f;
        // Set the damping ratio to the configurated option.
        suspension.dampingRatio = wheelOptions.dampingRatio;
        // Set the frequency to the configurated option.
        suspension.frequency = wheelOptions.frequency;
        // Set the JointSuspension created to the WheelJoint2D
        wheelJoint.suspension = suspension;
        // Adjust the anchor to the position of the wheel.
        wheelJoint.anchor = wheels[wheelPosition].transform.localPosition;
        // Set the mass of the rigidbody2D of the wheel
        wheels[wheelPosition].GetComponent <Rigidbody2D>().mass = wheelOptions.mass;
        // Set the gravity scale of the rigidbody2D to the wheel.
        wheels [wheelPosition].GetComponent <Rigidbody2D> ().gravityScale = wheelOptions.gravityScale;
    }
Exemplo n.º 6
0
    static int _CreateJointSuspension2D(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 0);
        JointSuspension2D obj = new JointSuspension2D();

        LuaScriptMgr.PushValue(L, obj);
        return(1);
    }
Exemplo n.º 7
0
    public void ReadyWheelsForDrive()
    {
        foreach (WheelJoint2D wheel in wheels)
        {
            JointSuspension2D suspension = wheel.suspension;
            suspension.dampingRatio = drivingWheelDampingRatio;
            suspension.frequency    = drivingWheelFrequency;

            wheel.suspension = suspension;
        }
    }
Exemplo n.º 8
0
    private void AddWheel(Part part, Part attached)
    {
        WheelJoint2D wheel = part.gameObject.AddComponent <WheelJoint2D>();

        wheel.connectedBody = attached.GetComponent <Rigidbody2D>();

        Vector2 connectedAnchor = wheel.connectedAnchor;

        connectedAnchor.y     = -1;
        wheel.connectedAnchor = connectedAnchor;

        JointSuspension2D suspension = wheel.suspension;

        suspension.frequency = 40;
        wheel.suspension     = suspension;
    }
Exemplo n.º 9
0
    public static int constructor(IntPtr l)
    {
        int result;

        try
        {
            JointSuspension2D jointSuspension2D = default(JointSuspension2D);
            LuaObject.pushValue(l, true);
            LuaObject.pushValue(l, jointSuspension2D);
            result = 2;
        }
        catch (Exception e)
        {
            result = LuaObject.error(l, e);
        }
        return(result);
    }
Exemplo n.º 10
0
        public override void OnEnter()
        {
            GameObject ownerDefaultTarget = base.Fsm.GetOwnerDefaultTarget(gameObject);

            if (ownerDefaultTarget != null)
            {
                _wj2d = ownerDefaultTarget.GetComponent <WheelJoint2D>();
                if (_wj2d != null)
                {
                    _motor      = _wj2d.motor;
                    _suspension = _wj2d.suspension;
                }
            }
            SetProperties();
            if (!everyFrame)
            {
                Finish();
            }
        }
Exemplo n.º 11
0
//	public Square map;

    // Use this for initialization
    void Start()
    {
        map = new List <GameObject>();
        RNG_obstacleList = new List <int>();

        GenerateRandomMobs(RNG_obstacleList, 5);


        perlinNoiseStep = Random.Range(0.0001f, 0.11f);
        print(perlinNoiseStep);
        xSoundPos = Random.Range(0.1f, 1000000f);
        GenerateMap(genCount);
        GenerateLeftCollider();
        _mainCam = Camera.main;

        //Instantiate Car
        GameObject car = Instantiate(carPrefab, carSpawn.position, Quaternion.identity);

        //setting camera to this OBJ
        _mainCam.GetComponent <CameraController>().target = car.transform;


        //backwheel setup, loading from save data
        JointSuspension2D back_susp = new JointSuspension2D()
        {
            angle     = car.GetComponent <CarMover>().backwheel.suspension.angle,
            frequency = GameController.controller.carInfo.back_freq, dampingRatio = GameController.controller.carInfo.back_damp
        };

        car.GetComponent <CarMover>().backwheel.suspension = back_susp;

        //front wheel setup, loading from save data
        JointSuspension2D front_susp = new JointSuspension2D()
        {
            angle     = car.GetComponent <CarMover>().frontwheel.suspension.angle,
            frequency = GameController.controller.carInfo.front_freq, dampingRatio = GameController.controller.carInfo.front_damp
        };

        car.GetComponent <CarMover>().frontwheel.suspension = front_susp;
    }
Exemplo n.º 12
0
 void Grabbing()
 {
     foreach (Collider2D collider in Physics2D.OverlapCircleAll(transform.position, 0.2f))
     {
         if (collider.gameObject.transform.parent != transform.parent && IsGrabbing == false && Input.GetMouseButton(0))
         {
             IsGrabbing = true;
             gameObject.AddComponent <WheelJoint2D>();
             gameObject.GetComponent <WheelJoint2D>().connectedBody   = collider.GetComponent <Rigidbody2D>();
             gameObject.GetComponent <WheelJoint2D>().connectedAnchor = collider.transform.InverseTransformPoint(transform.position);
             JointSuspension2D newWheel = gameObject.GetComponent <WheelJoint2D>().suspension;
             newWheel.dampingRatio = 1;
             newWheel.frequency    = 10000;
             gameObject.GetComponent <WheelJoint2D>().suspension = newWheel;
             break;
         }
     }
     if (!Input.GetMouseButton(0))
     {
         Destroy(GetComponent <WheelJoint2D>());
         IsGrabbing = false;
     }
 }
Exemplo n.º 13
0
    static int get_angle(IntPtr L)
    {
        object o = LuaScriptMgr.GetLuaObject(L, 1);

        if (o == null)
        {
            LuaTypes types = LuaDLL.lua_type(L, 1);

            if (types == LuaTypes.LUA_TTABLE)
            {
                LuaDLL.luaL_error(L, "unknown member name angle");
            }
            else
            {
                LuaDLL.luaL_error(L, "attempt to index angle on a nil value");
            }
        }

        JointSuspension2D obj = (JointSuspension2D)o;

        LuaScriptMgr.Push(L, obj.angle);
        return(1);
    }
Exemplo n.º 14
0
    static int set_angle(IntPtr L)
    {
        object o = LuaScriptMgr.GetLuaObject(L, 1);

        if (o == null)
        {
            LuaTypes types = LuaDLL.lua_type(L, 1);

            if (types == LuaTypes.LUA_TTABLE)
            {
                LuaDLL.luaL_error(L, "unknown member name angle");
            }
            else
            {
                LuaDLL.luaL_error(L, "attempt to index angle on a nil value");
            }
        }

        JointSuspension2D obj = (JointSuspension2D)o;

        obj.angle = (float)LuaScriptMgr.GetNumber(L, 3);
        LuaScriptMgr.SetValueObject(L, 1, obj);
        return(0);
    }
Exemplo n.º 15
0
 // Start is called before the first frame update
 void Start()
 {
     motorJ.maxMotorTorque = maxMotorForce;
     frontSpensionJ        = frontWheel.suspension;
     backSpensionJ         = backWheel.suspension;
 }
Exemplo n.º 16
0
 internal void Set(JointSuspension2D s)
 {
     angle        = s.angle;
     dampingRatio = s.dampingRatio;
     frequency    = s.frequency;
 }
Exemplo n.º 17
0
    void Update()
    {
        /*
         * if (PhotonNetwork.isMasterClient == false) {
         *      return;
         * } else {
         *      cameraPlayer.GetComponent<CameraController> ().target = gameObject.transform;
         * }*/
        cameraPlayer.GetComponent <CameraController> ().target = gameObject.transform;

        /***************************
         * FIREING RANGE *
         * ***************************/

        if (jetEngine == 0)
        {
            jetEngineTimer = 0.25f;
        }
        else if (jetEngine == 1)
        {
            jetEngineTimer = 0.5f;
        }
        else if (jetEngine == 2)
        {
            jetEngineTimer = 0.75f;
        }
        else if (jetEngine == 3)
        {
            jetEngineTimer = 1f;
        }
        else if (jetEngine == 4)
        {
            jetEngineTimer = 1.25f;
        }
        else if (jetEngine == 5)
        {
            jetEngineTimer = 1.5f;
        }

        int jetFuel = 0;

        if (PlayerPrefs.HasKey("PlayerTurbo"))
        {
            jetFuel = PlayerPrefs.GetInt("PlayerTurbo");
        }


        if (Input.GetKey(KeyCode.LeftShift))
        {
            if (jetEngine > 0)
            {
                if (jetFuel > 0)
                {
                    if (timerJet > 0)
                    {
                        timerJet -= Time.fixedDeltaTime;
                    }
                    else
                    {
                        timerJet = jetEngineTimer;
                        jetFuel--;
                        PlayerPrefs.SetInt("PlayerTurbo", jetFuel);
                    }
                    if (jetFuel < 10)
                    {
                        LowJetFuel.SetActive(true);
                    }
                    else
                    {
                        LowJetFuel.SetActive(false);
                    }
                    gameObject.GetComponent <Rigidbody2D> ().AddForce(Vector2.right * 1000 * Time.deltaTime);
                    JetEngineSound.Play();
                }
            }
        }
        else
        {
            JetEngineSound.Pause();
        }

        /***************************
         * FIREING RANGE END*
         * ***************************/


        movement = -CrossPlatformInputManager.GetAxis("Vertical") * speed;
        rotation = -CrossPlatformInputManager.GetAxis("Horizontal");

        weight     = cameraPlayer.GetComponent <Player> ().weight;
        suspension = cameraPlayer.GetComponent <Player> ().suspension;
        engine     = cameraPlayer.GetComponent <Player> ().engine;
        jetEngine  = cameraPlayer.GetComponent <Player> ().jetEngine;
        tires      = cameraPlayer.GetComponent <Player> ().tires;
        armor      = cameraPlayer.GetComponent <Player> ().armor;
        weapon     = cameraPlayer.GetComponent <Player> ().weapon;
        bullets    = cameraPlayer.GetComponent <Player> ().bullet * 10;

        if (weight == 0)
        {
            gameObject.GetComponent <Rigidbody2D> ().mass = 10;
        }
        else if (weight == 1)
        {
            gameObject.GetComponent <Rigidbody2D> ().mass = 9;
        }
        else if (weight == 2)
        {
            gameObject.GetComponent <Rigidbody2D> ().mass = 8;
        }
        else if (weight == 3)
        {
            gameObject.GetComponent <Rigidbody2D> ().mass = 7;
        }
        else if (weight == 4)
        {
            gameObject.GetComponent <Rigidbody2D> ().mass = 6;
        }
        else if (weight == 5)
        {
            gameObject.GetComponent <Rigidbody2D> ().mass = 5;
        }

        JointSuspension2D susp = new JointSuspension2D();

        susp.dampingRatio = 0.7f;
        susp.angle        = 90;
        susp.frequency    = 4;

        if (suspension == 0)
        {
            susp.frequency = 4;
        }
        else if (suspension == 1)
        {
            susp.frequency = 5;
        }
        else if (suspension == 2)
        {
            susp.frequency = 6;
        }
        else if (suspension == 3)
        {
            susp.frequency = 7;
        }
        else if (suspension == 4)
        {
            susp.frequency = 8;
        }
        else if (suspension == 5)
        {
            susp.frequency = 10;
        }
        backWheel.suspension  = susp;
        frontWheel.suspension = susp;

        if (engine == 0)
        {
            engineTimer = 0.25f;
        }
        else if (engine == 1)
        {
            engineTimer = 0.5f;
        }
        else if (engine == 2)
        {
            engineTimer = 0.75f;
        }
        else if (engine == 3)
        {
            engineTimer = 1f;
        }
        else if (engine == 4)
        {
            engineTimer = 1.25f;
        }
        else if (engine == 5)
        {
            engineTimer = 1.5f;
        }

        if (tires == 0)
        {
            Tires.friction = 0.25f;
        }
        else if (tires == 1)
        {
            Tires.friction = 0.5f;
        }
        else if (tires == 2)
        {
            Tires.friction = 0.75f;
        }
        else if (tires == 3)
        {
            Tires.friction = 1f;
        }
        else if (tires == 4)
        {
            Tires.friction = 1.5f;
        }
        else if (tires == 5)
        {
            Tires.friction = 2.5f;
        }

        if (armor == 0)
        {
            armorTimer = 0.1f;
        }
        else if (armor == 1)
        {
            armorTimer = 0.25f;
        }
        else if (armor == 2)
        {
            armorTimer = 0.5f;
        }
        else if (armor == 3)
        {
            armorTimer = 0.75f;
        }
        else if (armor == 4)
        {
            armorTimer = 1f;
        }
        else if (armor == 5)
        {
            armorTimer = 1.5f;
        }

        if (timerArmor > 0)
        {
            timerArmor -= Time.deltaTime;
        }

        int bulletAmount = 0;

        if (PlayerPrefs.HasKey("PlayerBulletAmount"))
        {
            bulletAmount = PlayerPrefs.GetInt("PlayerBulletAmount");
        }
        else
        {
            PlayerPrefs.SetInt("PlayerBulletAmount", bullets);
            bulletAmount = bullets;
        }

        BulletCount.text = "x" + bulletAmount.ToString();
    }
Exemplo n.º 18
0
    public RenderedCar RenderCar(Car car, Vector3 chassisPos, bool isGame, bool flipped, Game game, int player, bool playerControls)
    {
        GameObject chassis     = null;
        GameObject attack1     = null;
        GameObject attack2     = null;
        GameObject forkliftObj = null;
        GameObject frontWheel  = null;
        GameObject backWheel   = null;

        Vector3[] attack1Pos      = new Vector3[4];
        Vector3   attack2Pos      = Vector3.zero;
        Vector3   forkliftPos     = Vector3.zero;
        Vector3   frontWheelPos   = Vector3.zero;
        Vector3   backWheelPos    = Vector3.zero;
        Vector3   anchorPos       = Vector3.zero;
        Vector3   connectedAnchor = Vector3.zero;

        Vector3 bladeAnchorPos       = Vector3.zero;
        Vector3 bladeConnectedAnchor = Vector3.zero;

        float speed = (flipped) ? -300f : 300f;

        if (car.chassis != null)
        {
            switch (car.chassis.id)
            {
            case 1:
                chassis = Instantiate(chassis1, chassisPos, Quaternion.identity);

                if (!flipped)
                {
                    attack1Pos[0] = new Vector3(1.21f, -0.14f);
                    attack1Pos[1] = new Vector3(1.21f, -0.14f);
                    attack1Pos[2] = new Vector3(0.5f, -0.17f);
                    attack1Pos[3] = new Vector3(1.21f, -0.14f);

                    forkliftPos     = new Vector3(-0.62f, -0.7f);
                    anchorPos       = new Vector3(1.84f, 0f);
                    connectedAnchor = new Vector3(0.3f, -0.7f);

                    frontWheelPos = new Vector3(0.55f, -1.24f);
                    backWheelPos  = new Vector3(-0.55f, -1.24f);

                    bladeAnchorPos       = new Vector3(-1.25f, -0.09f);
                    bladeConnectedAnchor = new Vector3(0.585f, -0.185f);
                }
                else
                {
                    attack1Pos[0] = new Vector3(-1.16f, -0.11f);
                    attack1Pos[1] = new Vector3(-1.16f, -0.11f);
                    attack1Pos[2] = new Vector3(-0.55f, -0.16f);
                    attack1Pos[3] = new Vector3(-1.16f, -0.11f);

                    forkliftPos     = new Vector3(0.63f, -0.67f);
                    anchorPos       = new Vector3(-1.78f, 0f);
                    connectedAnchor = new Vector3(-0.26f, -0.67f);

                    frontWheelPos = new Vector3(-0.48f, -1.24f);
                    backWheelPos  = new Vector3(0.53f, -1.24f);

                    bladeAnchorPos       = new Vector3(1.35f, -0.09f);
                    bladeConnectedAnchor = new Vector3(-0.485f, -0.155f);
                }

                break;

            case 2:

                chassis = Instantiate(chassis2, chassisPos, Quaternion.identity);

                if (!flipped)
                {
                    attack1Pos[0] = new Vector3(1.65f, 0.3f);
                    attack1Pos[1] = new Vector3(1.65f, 0.3f);
                    attack1Pos[2] = new Vector3(0.97f, 0.24f);
                    attack1Pos[3] = new Vector3(1.65f, 0.3f);

                    forkliftPos     = new Vector3(-0.29f, -0.08f);
                    anchorPos       = new Vector3(1.83f, 0f);
                    connectedAnchor = new Vector3(0.625f, -0.08f);

                    frontWheelPos = new Vector3(0.55f, -0.48f);
                    backWheelPos  = new Vector3(-0.91f, -0.48f);

                    bladeAnchorPos       = new Vector3(-1.29f, -0.09f);
                    bladeConnectedAnchor = new Vector3(1.005f, 0.255f);
                }
                else
                {
                    attack1Pos[0] = new Vector3(-1.61f, 0.29f);
                    attack1Pos[1] = new Vector3(-1.61f, 0.29f);
                    attack1Pos[2] = new Vector3(-0.99f, 0.25f);
                    attack1Pos[3] = new Vector3(-1.61f, 0.29f);

                    forkliftPos     = new Vector3(0.27f, -0.09f);
                    anchorPos       = new Vector3(-1.8f, 0f);
                    connectedAnchor = new Vector3(-0.63f, -0.09f);

                    frontWheelPos = new Vector3(-0.49f, -0.48f);
                    backWheelPos  = new Vector3(0.92f, -0.48f);

                    bladeAnchorPos       = new Vector3(1.33f, -0.09f);
                    bladeConnectedAnchor = new Vector3(-0.945f, 0.245f);
                }
                break;

            case 3:

                chassis = Instantiate(chassis3, chassisPos, Quaternion.identity);

                if (!flipped)
                {
                    attack1Pos[0] = new Vector3(1.77f, 0.18f);
                    attack1Pos[1] = new Vector3(1.77f, 0.18f);
                    attack1Pos[2] = new Vector3(1.13f, 0.14f);
                    attack1Pos[3] = new Vector3(1.77f, 0.18f);

                    attack2Pos = new Vector3(-0.2f, 0.08f);

                    forkliftPos     = new Vector3(-0.05f, -0.1f);
                    anchorPos       = new Vector3(1.8f, 0f);
                    connectedAnchor = new Vector3(0.85f, -0.1f);

                    frontWheelPos = new Vector3(1.07f, -0.57f);
                    backWheelPos  = new Vector3(-0.17f, -0.57f);

                    bladeAnchorPos       = new Vector3(-1.26f, -0.11f);
                    bladeConnectedAnchor = new Vector3(1.14f, 0.125f);
                }
                else
                {
                    attack1Pos[0] = new Vector3(-1.77f, 0.23f);
                    attack1Pos[1] = new Vector3(-1.77f, 0.23f);
                    attack1Pos[2] = new Vector3(-1.11f, 0.18f);
                    attack1Pos[3] = new Vector3(-1.77f, 0.23f);

                    attack2Pos = new Vector3(0.26f, 0.1f);

                    forkliftPos     = new Vector3(0.1f, -0.15f);
                    anchorPos       = new Vector3(-1.78f, 0f);
                    connectedAnchor = new Vector3(-0.79f, -0.15f);

                    frontWheelPos = new Vector3(-1.03f, -0.57f);
                    backWheelPos  = new Vector3(0.2f, -0.57f);

                    bladeAnchorPos       = new Vector3(1.33f, -0.11f);
                    bladeConnectedAnchor = new Vector3(-1.105f, 0.175f);
                }
                break;
            }

            if (flipped)
            {
                chassis.GetComponent <SpriteRenderer>().flipX = true;
            }

            if (isGame)
            {
                chassis.AddComponent <Rigidbody2D>().mass = 5;
                chassis.AddComponent <PolygonCollider2D>();
                WallAttackDetection wallAttack = chassis.AddComponent <WallAttackDetection>();
                wallAttack.game   = game;
                wallAttack.player = player;

                chassis.tag = "Chassis";
            }
        }

        if (car.attack1 != null)
        {
            switch (car.attack1.id)
            {
            case 7:
                attack1 = Instantiate(blade, new Vector3(0, 0), Quaternion.identity);
                attack1.transform.position = chassisPos + attack1Pos[0];
                break;

            case 8:
                attack1 = Instantiate(chainsaw, new Vector3(0, 0), Quaternion.identity);
                attack1.transform.position = chassisPos + attack1Pos[1];
                break;

            case 9:
                attack1 = Instantiate(rocket, new Vector3(0, 0), Quaternion.identity);
                attack1.transform.position = chassisPos + attack1Pos[2];
                break;

            case 10:
                attack1 = Instantiate(stinger, new Vector3(0, 0), Quaternion.identity);
                attack1.transform.position = chassisPos + attack1Pos[3];
                break;
            }

            attack1.transform.SetParent(chassis.transform);
            attack1.transform.localScale = new Vector3(0.5f, 0.5f);
            attack1.GetComponent <SpriteRenderer>().sortingOrder = 1;

            if (flipped)
            {
                attack1.GetComponent <SpriteRenderer>().flipX = true;
            }

            if (isGame)
            {
                if (car.attack1.id == 7)
                {
                    HingeJoint2D joint = attack1.AddComponent <HingeJoint2D>();
                    joint.connectedBody   = chassis.GetComponent <Rigidbody2D>();
                    joint.anchor          = bladeAnchorPos;
                    joint.connectedAnchor = bladeConnectedAnchor;

                    JointMotor2D motor = joint.motor;
                    motor.motorSpeed     = 200;
                    motor.maxMotorTorque = 50;
                    joint.motor          = motor;

                    attack1.GetComponent <Rigidbody2D>().mass = 0.3f;
                }
                else
                {
                    FixedJoint2D joint = attack1.AddComponent <FixedJoint2D>();
                    joint.connectedBody = chassis.GetComponent <Rigidbody2D>();
                    attack1.GetComponent <Rigidbody2D>().mass = 0.1f;
                }

                PolygonCollider2D polygonCollider = attack1.AddComponent <PolygonCollider2D>();
                polygonCollider.isTrigger = true;

                if (car.attack1.id != 9)
                {
                    AttackDetection attackDetection = attack1.AddComponent <AttackDetection>();
                    attackDetection.game   = game;
                    attackDetection.player = player;
                }
            }
        }

        if (car.frontWheel != null)
        {
            float radius = -1;

            switch (car.frontWheel.id)
            {
            case 4:
                radius     = 0.73f;
                frontWheel = Instantiate(wheel1, new Vector3(0, 0), Quaternion.identity);
                break;

            case 5:
                radius     = 0.61f;
                frontWheel = Instantiate(wheel2, new Vector3(0, 0), Quaternion.identity);
                break;
            }

            frontWheel.transform.SetParent(chassis.transform);

            frontWheel.transform.position   = chassisPos + frontWheelPos;
            frontWheel.transform.localScale = new Vector3(0.5f, 0.5f);
            frontWheel.GetComponent <SpriteRenderer>().sortingOrder = 1;

            if (flipped)
            {
                frontWheel.GetComponent <SpriteRenderer>().flipX = true;
            }

            if (isGame)
            {
                WheelJoint2D wheelJoint2D = frontWheel.AddComponent <WheelJoint2D>();
                wheelJoint2D.connectedBody   = chassis.GetComponent <Rigidbody2D>();
                wheelJoint2D.connectedAnchor = frontWheelPos;

                JointSuspension2D jointSuspension = wheelJoint2D.suspension;
                jointSuspension.dampingRatio = 0.9f;
                jointSuspension.frequency    = 10;
                wheelJoint2D.suspension      = jointSuspension;


                wheelJoint2D.useMotor = true;
                JointMotor2D motor = wheelJoint2D.motor;
                motor.motorSpeed   = speed;
                wheelJoint2D.motor = motor;

                wheelJoint2D.useMotor = !playerControls;

                CircleCollider2D circleCollider = frontWheel.AddComponent <CircleCollider2D>();
                circleCollider.radius = radius;
            }
        }

        if (car.backWheel != null)
        {
            float radius = -1;

            switch (car.backWheel.id)
            {
            case 4:
                radius    = 0.73f;
                backWheel = Instantiate(wheel1, new Vector3(0, 0), Quaternion.identity);
                break;

            case 5:
                radius    = 0.61f;
                backWheel = Instantiate(wheel2, new Vector3(0, 0), Quaternion.identity);
                break;
            }

            backWheel.transform.SetParent(chassis.transform);

            backWheel.transform.position   = chassisPos + backWheelPos;
            backWheel.transform.localScale = new Vector3(0.5f, 0.5f);
            backWheel.GetComponent <SpriteRenderer>().sortingOrder = 1;

            if (flipped)
            {
                backWheel.GetComponent <SpriteRenderer>().flipX = true;
            }

            if (isGame)
            {
                WheelJoint2D wheelJoint2D = backWheel.AddComponent <WheelJoint2D>();
                wheelJoint2D.connectedBody   = chassis.GetComponent <Rigidbody2D>();
                wheelJoint2D.connectedAnchor = backWheelPos;

                JointSuspension2D jointSuspension = wheelJoint2D.suspension;
                jointSuspension.dampingRatio = 0.9f;
                jointSuspension.frequency    = 10;
                wheelJoint2D.suspension      = jointSuspension;


                wheelJoint2D.useMotor = true;
                JointMotor2D motor = wheelJoint2D.motor;
                motor.motorSpeed   = speed;
                wheelJoint2D.motor = motor;

                wheelJoint2D.useMotor = !playerControls;

                CircleCollider2D circleCollider = backWheel.AddComponent <CircleCollider2D>();
                circleCollider.radius = radius;
            }
        }

        if (car.forklift != null)
        {
            forkliftObj = Instantiate(forklift);
            forkliftObj.transform.position   = chassisPos + forkliftPos;
            forkliftObj.transform.localScale = new Vector3(0.5f, 0.5f);
            forkliftObj.GetComponent <SpriteRenderer>().sortingOrder = 1;
            forkliftObj.transform.SetParent(chassis.transform);

            if (!flipped)
            {
                forkliftObj.GetComponent <SpriteRenderer>().flipX = true;
            }

            if (isGame)
            {
                forkliftObj.AddComponent <PolygonCollider2D>();
                HingeJoint2D joint = forkliftObj.AddComponent <HingeJoint2D>();
                joint.connectedBody   = chassis.GetComponent <Rigidbody2D>();
                joint.anchor          = anchorPos;
                joint.connectedAnchor = connectedAnchor;

                JointMotor2D motor = joint.motor;
                if (player == 0)
                {
                    motor.motorSpeed = -50;
                }
                else
                {
                    motor.motorSpeed = 50;
                }
                motor.maxMotorTorque = 500;
                joint.motor          = motor;

                joint.useMotor = !playerControls;

                forkliftObj.GetComponent <Rigidbody2D>().mass = 0.3f;
            }
        }

        if (car.attack2 != null)
        {
            attack2 = Instantiate(rocket);
            attack2.transform.position   = chassisPos + attack2Pos;
            attack2.transform.localScale = new Vector3(0.5f, 0.5f);
            attack2.GetComponent <SpriteRenderer>().sortingOrder = 1;

            attack2.transform.SetParent(chassis.transform);

            if (flipped)
            {
                attack2.GetComponent <SpriteRenderer>().flipX = true;
            }

            if (isGame)
            {
                FixedJoint2D joint = attack2.AddComponent <FixedJoint2D>();
                joint.connectedBody = chassis.GetComponent <Rigidbody2D>();
                attack2.GetComponent <Rigidbody2D>().mass = 0;
            }
        }

        RenderedCar renderedCar = new RenderedCar();

        renderedCar.chassis    = chassis;
        renderedCar.attack1    = attack1;
        renderedCar.attack2    = attack2;
        renderedCar.forklift   = forkliftObj;
        renderedCar.frontWheel = frontWheel;
        renderedCar.backWheel  = backWheel;

        return(renderedCar);
    }
Exemplo n.º 19
0
    private void InitializeAllWheels()
    {
        int          i;               // pomocna iteracna premenna
        GameObject   wheelPrefab;     // kopia kolesa
        string       wheelPrefabName; // nazov objektu kolesa
        WheelJoint2D wheelJoint;      // spoj medzi autem a kolesem

        /*
         * // nacitanie particle systemu
         * dirt = ; //GameObject.Find("bahno").particleSystem;
         * dirt.enableEmission = false;
         * smoke =; //GameObject.Find("bahno_dym").particleSystem;
         * smoke.enableEmission = false;
         */
        // nacitam si zoznam bodov kde sa pripoja kolesa
        GameObject[] wheelsAnchor = GameObject.FindGameObjectsWithTag("wheelAnchor");

        // inicializujem si pole do ktoreho ulozim vsetky kolesa
        this.wheels = new Wheel[wheelsAnchor.Length];

        for (i = 0; i < wheelsAnchor.Length; i++)
        {
            // vytvoreni nazvu objektu kolesa
            wheelPrefabName = wheelsAnchor[i].GetComponent <WheelAnchor>().wheelType.ToString() + "_" + wheelsLevel.ToString();

            // auto pridam spoj pre koleso
            wheelJoint = this.gameObject.AddComponent <WheelJoint2D>();

            // skopirujem si objekt kolesa
            wheelPrefab = (GameObject)GameObject.Instantiate(Resources.Load("prefabs/wheels/" + wheelPrefabName));

            // vytvoreny objekt bude spadat pod auto
            wheelPrefab.transform.parent = this.transform;

            // nastaveni pozicie kolesa
            wheelPrefab.transform.position = new Vector3(wheelsAnchor[i].transform.position.x, wheelsAnchor[i].transform.position.y, -0.1f);

            // pripojeni kolesa k bodu pripoju na aute
            wheelJoint.connectedBody = wheelPrefab.GetComponent <Rigidbody2D>();

            // nastaveni pozicie bodu pripoju
            wheelJoint.anchor = new Vector2(wheelsAnchor[i].transform.localPosition.x, wheelsAnchor[i].transform.localPosition.y);

            // pripoj bude pripojeny na stred auta
            wheelJoint.connectedAnchor = new Vector2(0, 0);

            // inicializacia kolesa
            wheelPrefab.GetComponent <Wheel>().Wheel2(wheelJoint, i, wheelsAnchor[i].GetComponent <WheelAnchor>().powered);

            // nastaveni parametru tlmicov
            JointSuspension2D newSuspension = wheelJoint.suspension;
            newSuspension.dampingRatio = wheelsAnchor[i].GetComponent <WheelAnchor>().suspensionDampingRatio;
            newSuspension.frequency    = wheelsAnchor[i].GetComponent <WheelAnchor>().suspensionFrequency;
            wheelJoint.suspension      = newSuspension;

            // odstranim si pomocny objekt na zaklade kereho sem vedel kam dat koleso
            Destroy(wheelsAnchor[i]);

            // objekt kolesa transofrmujem do rozsireneho objektu kolesa a ulozim do pola
            this.wheels[i] = wheelPrefab.GetComponent <Wheel>();

            // jedna sa o prve pohanane koleso v ramci auta
            if (this.firstPoweredWheel == -1 && this.wheels[i].IsPowered())
            {
                this.firstPoweredWheel = i;
            }
        }
    }
Exemplo n.º 20
0
    void loadJsonJoints(JSONNode jsonJoints)
    {
        int JointCount = 0;

        for (int i = 0, numberOfJoints = jsonJoints.Count; i < numberOfJoints; i++)
        {
            JSONNode jsonJoint = jsonJoints[i];
            int      jointType = jsonJoint["jointType"].AsInt;

            GameObject bodyA = loadedObjects[jsonJoint["bodyA"].AsInt];
            GameObject bodyB = loadedObjects[jsonJoint["bodyB"].AsInt];

            JSONNode localAnchorA     = jsonJoint["localAnchorA"];
            Vector2  anchorA          = new Vector2(localAnchorA[0].AsFloat / RATIO, -localAnchorA[1].AsFloat / RATIO);
            JSONNode localAnchorB     = jsonJoint["localAnchorB"];
            Vector2  anchorB          = new Vector2(localAnchorB[0].AsFloat / RATIO, -localAnchorB[1].AsFloat / RATIO);
            bool     collideConnected = jsonJoint["collideConnected"].AsBool;
            string   userData         = jsonJoint["userData"].Value;

            if (jointType == (int)JointTypes.JOINT_DISTANCE || jointType == (int)JointTypes.JOINT_ROPE)
            {
                DistanceJoint2D joint = bodyA.AddComponent <DistanceJoint2D>();
                joint.connectedBody   = bodyB.GetComponent <Rigidbody2D>();
                joint.anchor          = anchorA;
                joint.connectedAnchor = anchorB;

                // distance joint
                if (jsonJoint["length"] != null)
                {
                    joint.distance        = jsonJoint["length"].AsFloat / RATIO;
                    joint.maxDistanceOnly = true;
                }
                // rope joint
                else if (jsonJoint["maxLength"] != null)
                {
                    joint.distance = jsonJoint["maxLength"].AsFloat / RATIO;
                }

                joint.enableCollision = collideConnected;
                joint.name           += '_';
                joint.name           += userData.Length > 0 ? userData : "joint" + JointCount++;
            }
            else if (jointType == (int)JointTypes.JOINT_REVOLUTE)
            {
                HingeJoint2D joint = bodyA.AddComponent <HingeJoint2D>();
                joint.connectedBody   = bodyB.GetComponent <Rigidbody2D>();
                joint.anchor          = anchorA;
                joint.connectedAnchor = anchorB;
                joint.enableCollision = collideConnected;
                joint.name           += '_';
                joint.name           += userData.Length > 0 ? userData : "joint" + JointCount++;

                // limits are not working properly
                bool  enableLimits       = jsonJoint["enableLimit"].AsBool;
                float referenceAngle     = -jsonJoint["referenceAngle"].AsFloat;
                float angleBetweenBodies = Mathf.Atan2(bodyB.transform.position.y - bodyA.transform.position.y,
                                                       bodyB.transform.position.x - bodyA.transform.position.x) * 180 / Mathf.PI;
                float upperAngle     = -jsonJoint["lowerAngle"].AsFloat;
                float lowerAngle     = -jsonJoint["upperAngle"].AsFloat;
                bool  enableMotor    = jsonJoint["enableMotor"].AsBool;
                float motorSpeed     = -jsonJoint["motorSpeed"].AsFloat;
                float maxMotorTorque = jsonJoint["maxMotorTorque"].AsFloat;

                joint.useLimits = enableLimits;
                JointAngleLimits2D limits = new JointAngleLimits2D();
                limits.max     = angleBetweenBodies + upperAngle;
                limits.min     = angleBetweenBodies + lowerAngle;
                joint.limits   = limits;
                joint.useMotor = enableMotor;
                JointMotor2D motor = new JointMotor2D();
                motor.maxMotorTorque = maxMotorTorque;
                motor.motorSpeed     = motorSpeed;
                joint.motor          = motor;
            }
            else if (jointType == (int)JointTypes.JOINT_WHEEL)
            {
                WheelJoint2D joint = bodyA.AddComponent <WheelJoint2D>();
                joint.connectedBody   = bodyB.GetComponent <Rigidbody2D>();
                joint.anchor          = anchorA;
                joint.connectedAnchor = anchorB;
                joint.enableCollision = collideConnected;
                joint.name           += '_';
                joint.name           += userData.Length > 0 ? userData : "joint" + JointCount++;

                bool     enableMotor    = jsonJoint["enableMotor"].AsBool;
                float    motorSpeed     = -jsonJoint["motorSpeed"].AsFloat;
                float    maxMotorTorque = jsonJoint["maxMotorTorque"].AsFloat;
                float    dampingRatio   = jsonJoint["dampingRatio"].AsFloat;
                float    frequency      = jsonJoint["frequencyHZ"].AsFloat;
                JSONNode localAxisA     = jsonJoint["localAxisA"];
                float    angle          = Mathf.Atan2(-localAxisA[1].AsFloat, localAxisA[0].AsFloat) * 180 / Mathf.PI;

                joint.useMotor = enableMotor;
                JointMotor2D motor = new JointMotor2D();
                motor.maxMotorTorque = maxMotorTorque;
                motor.motorSpeed     = motorSpeed;
                joint.motor          = motor;

                JointSuspension2D suspension = new JointSuspension2D();
                suspension.dampingRatio = dampingRatio;
                suspension.frequency    = frequency;
                suspension.angle        = angle;
                joint.suspension        = suspension;
            }
            else if (jointType == (int)JointTypes.JOINT_PRISMATIC)
            {
                SliderJoint2D joint = bodyA.AddComponent <SliderJoint2D>();
                joint.connectedBody   = bodyB.GetComponent <Rigidbody2D>();
                joint.anchor          = anchorA;
                joint.connectedAnchor = anchorB;
                joint.enableCollision = collideConnected;
                joint.name           += '_';
                joint.name           += userData.Length > 0 ? userData : "joint" + JointCount++;

                bool     enableLimits     = jsonJoint["enableLimit"].AsBool;
                float    referenceAngle   = -jsonJoint["referenceAngle"].AsFloat;
                float    upperTranslation = jsonJoint["upperTranslation"].AsFloat / RATIO;
                float    lowerTranslation = jsonJoint["lowerTranslation"].AsFloat / RATIO;
                bool     enableMotor      = jsonJoint["enableMotor"].AsBool;
                float    motorSpeed       = -jsonJoint["motorSpeed"].AsFloat;
                float    maxMotorTorque   = jsonJoint["maxMotorTorque"].AsFloat;
                JSONNode localAxisA       = jsonJoint["localAxisA"];
                float    angle            = Mathf.Atan2(-localAxisA[1].AsFloat, localAxisA[0].AsFloat) * 180 / Mathf.PI;

                joint.useLimits = enableLimits;
                JointTranslationLimits2D limits = new JointTranslationLimits2D();
                limits.max     = upperTranslation;
                limits.min     = lowerTranslation;
                joint.limits   = limits;
                joint.useMotor = enableMotor;
                JointMotor2D motor = new JointMotor2D();
                motor.maxMotorTorque = maxMotorTorque;
                motor.motorSpeed     = motorSpeed;
                joint.motor          = motor;
                joint.angle          = angle;
            }
        }
    }