private void UpdateFollowCamera(CarComponent carData, Translation translationToFollow)
    {
        var translatedZ = this.followCamera.transform.position.z + ((carData.Speed / Settings.KMToTranslationUnit) * Time.deltaTime);
        var translatedX = Mathf.Lerp(this.followCamera.transform.position.x, translationToFollow.Value.x, Settings.FollowCameraTurn * Time.deltaTime);

        this.followCamera.transform.position = new Vector3(translatedX, this.followCamera.transform.position.y, translatedZ);
    }
예제 #2
0
    public void SaveBuildInCategories()
    {
        if (_placedBlocks.Count == 0)
        {
            Debug.Log("No blocks have been placed down. No saving has occured.");
            return;
        }

        //Setup the dictionary for every possible type
        Dictionary <CarComponents.Type, List <BlockSave> > saveme = new Dictionary <CarComponents.Type, List <BlockSave> >();

        foreach (CarComponents.Type type in (CarComponents.Type[])System.Enum.GetValues(typeof(CarComponents.Type)))
        {
            saveme.Add(type, new List <BlockSave>());
        }

        //foreach block placed, check the type and put it in the correct slot
        foreach (GameObject block in _placedBlocks.Values)
        {
            CarComponent component = block.GetComponent <BlockProperties>().data;
            BlockSave    save      = new BlockSave(component.name, block.transform.position - buildOffset, block.transform.rotation);

            //add to saveme dict
            saveme[component.type].Add(save);
        }

        string json = JsonConvert.SerializeObject(saveme, Formatting.Indented);

        File.WriteAllText(Application.dataPath + @"\PlacedBlocksCatergories" + _player + ".txt", json);
    }
예제 #3
0
    public void PreviewStatDisplay()
    {
        CarComponent component    = BuildController.GetComponent(_selectedBlock);
        CarStats     previewStats = UpdateCarStats(component, carStats, true);

        float  accelerationDif = previewStats.acceleration - carStats.acceleration;
        string acceleration    = accelerationDif.ToString();

        if (accelerationDif > 0f)
        {
            acceleration = acceleration.Insert(0, "+");
        }
        if (acceleration.Length > previewStatDisplayLength)
        {
            acceleration = acceleration.Substring(0, previewStatDisplayLength);
        }
        PaccelerationText.SetText(acceleration);

        float  speedDif = previewStats.maxSpeed - carStats.maxSpeed;
        string speed    = speedDif.ToString();

        if (speedDif > 0f)
        {
            speed = speed.Insert(0, "+");
        }
        if (speed.Length > previewStatDisplayLength)
        {
            speed = speed.Substring(0, previewStatDisplayLength);
        }
        PspeedText.SetText(speed);

        float  breakSpeedDif = previewStats.breakSpeed - carStats.breakSpeed;
        string breakSpeed    = breakSpeedDif.ToString();

        if (breakSpeedDif > 0f)
        {
            breakSpeed = breakSpeed.Insert(0, "+");
        }
        if (breakSpeed.Length > previewStatDisplayLength)
        {
            breakSpeed = breakSpeed.Substring(0, previewStatDisplayLength);
        }
        PbreakSpeedText.SetText(breakSpeed);

        float  steeringDif = previewStats.steerSpeed - carStats.steerSpeed;
        string steering    = steeringDif.ToString();

        if (steeringDif > 0f)
        {
            steering = steering.Insert(0, "+");
        }
        if (steering.Length > previewStatDisplayLength)
        {
            steering = steering.Substring(0, previewStatDisplayLength);
        }
        PsteeringText.SetText(steering);
    }
예제 #4
0
    private void OnTriggerEnter(Collider other)
    {
        CarComponent car = other.GetComponentInParent <CarComponent>();

        if (car)
        {
            car.Finish();
        }
    }
    private void HandleCloseCalls(CarComponent data)
    {
        var isInCloseCall = data.CarInCloseCall != -1 && this.carInCloseCallLastFrame != data.CarInCloseCall;

        if (isInCloseCall)
        {
            carInCloseCallLastFrame = data.CarInCloseCall;
            this.OnCloseCall();
        }
    }
예제 #6
0
    private void OnTriggerEnter(Collider other)
    {
        //Debug.Log("Trigger " + gameObject.name + " " + other.name);
        CarComponent car = other.GetComponentInParent <CarComponent>();

        if (car == null)
        {
            return;
        }
        car.ChangeWaypoint(Circuits[(int)Random.Range(0, Circuits.Count)]);
    }
예제 #7
0
    public static new CarComponent GetComponent(string name)
    {
        CarComponent result = null;

        foreach (CarComponent component in _carComponents)
        {
            if (component.name == name)
            {
                result = component;
            }
        }
        return(result);
    }
예제 #8
0
    private void OnTriggerExit(Collider other)
    {
        CarComponent car = other.GetComponentInParent <CarComponent>();

        if (car != null)
        {
            DM.Break(1);
            car.DamageMade += 1;

            //Debug.Log(gameObject.name + " durability: " + durability);
        }
        Debug.Log(other.gameObject.name);
    }
예제 #9
0
 // Start is called before the first frame update
 void Start()
 {
     ControllerManager = GameObject.Find("Controller Manager");
     if (ControllerManager != null)
     {
         Debug.Log("Controller Manager Found!");
         controllerManagerComponent = ControllerManager.GetComponent(typeof(ControllerManagerComponent)) as ControllerManagerComponent;
         car = GetComponent <CarComponent>();
     }
     else
     {
         Debug.Log("Controller Manager Not Found!");
     }
 }
예제 #10
0
 private void RemoveBlock()
 {
     if (currentCollisions.Count != 0)
     {
         //find collision entry
         List <Collider> removeMe = new List <Collider>();
         removeMe.AddRange(currentCollisions);
         foreach (Collider coll in removeMe)
         {
             GameObject go = coll.gameObject;
             if (coll.CompareTag("childmesh"))
             {
                 go = go.transform.parent.gameObject;
             }
             currentCollisions.Remove(coll);
             if (_placedBlocks.ContainsValue(go))
             {
                 _placedBlocks.Remove(_blocksPlaced[go]);
                 _blocksPlaced.Remove(go);
             }
             CarComponent component = go.GetComponent <BlockProperties>().data;
             BPText.SetText(BuildController.ModifyBuildPoints(component.buildPoints, _player).ToString());
             //CarStats
             carStats = UpdateCarStats(component, carStats, false);
             UpdateStatDisplay();
             PreviewStatDisplay();
             Destroy(go);
         }
     }
     if (_placedBlocks.ContainsKey(_cursorPos))
     {
         GameObject removeMe = _placedBlocks[_cursorPos];
         _blocksPlaced.Remove(removeMe);
         _placedBlocks.Remove(_cursorPos);
         CarComponent component = removeMe.GetComponent <BlockProperties>().data;
         BPText.SetText(BuildController.ModifyBuildPoints(component.buildPoints, _player).ToString());
         //CarStats
         carStats = UpdateCarStats(component, carStats, false);
         UpdateStatDisplay();
         PreviewStatDisplay();
         Destroy(removeMe);
     }
     if (currentCollisions.Count == 0)
     {
         HologramColor(new Color(1f, 1f, 1f, .5f));
     }
 }
예제 #11
0
파일: God.cs 프로젝트: caxapexac/Jams
    public void SpawnCar()
    {
        SpawnerPoint point = spawners[(int)Random.Range(0, spawners.Count)];
        GameObject   go    = Instantiate(CarLogic, point.transform.position, point.transform.rotation);

        go.transform.Rotate(Vector3.down * 90);
        CarComponent car = go.GetComponent <CarComponent>();

        car.ChangeWaypoint(point.Waypoint);

        switch (point.Type)
        {
        case SpawnerPoint.SpawnerType.Goverment:
            go.GetComponent <CarComponent>().SetupMesh(GovermentCars[(int)Random.Range(0, GovermentCars.Count)]);
            break;

        case SpawnerPoint.SpawnerType.Cilivian:
            go.GetComponent <CarComponent>().SetupMesh(CivilianCars[(int)Random.Range(0, CivilianCars.Count)]);
            break;
        }
    }
예제 #12
0
파일: KillZone.cs 프로젝트: caxapexac/Jams

        
    private void EndGameIfCollided(CarComponent heroCar)
    {
        var didEnd = heroCar.IsCollided;

        if (!didEnd)
        {
            return;
        }

        this.carSound.Stop();
        this.crashSound.Play();

        this.CancelInvoke();
        this.enabled = false;

        World.DefaultGameObjectInjectionWorld.QuitUpdate = true;

        this.lossPanel.SetActive(true);

        this.closeCallCountText.transform.parent.parent.gameObject.SetActive(true);
        this.closeCallCountText.text = this.closeCallCount.ToString();

        this.UpdateDistanceText();
    }
예제 #14
0
    //TODO terrainMultiplier is not really necessary here i think, but good thing to check over.
    public CarStats UpdateCarStats(CarComponent component, CarStats stats, bool positive)
    {
        CarStats newStats = new CarStats(stats.maxSpeed, stats.acceleration, stats.breakSpeed, stats.steerSpeed, stats.terrainMultiplier);

        foreach (ComponentModifier modifier in component.modifiers)
        {
            switch (modifier.stat)
            {
            case CarStat.MaxSpeed:
                switch (modifier.math)
                {
                case MathOperator.plus:
                    newStats.maxSpeed += modifier.value * (positive ? 1 : -1);
                    break;

                case MathOperator.minus:
                    newStats.maxSpeed -= modifier.value * (positive ? 1 : -1);
                    break;
                }
                break;

            case CarStat.Acceleration:
                switch (modifier.math)
                {
                case MathOperator.plus:
                    newStats.acceleration += modifier.value * (positive ? 1 : -1);
                    break;

                case MathOperator.minus:
                    newStats.acceleration -= modifier.value * (positive ? 1 : -1);
                    break;
                }
                break;

            case CarStat.BreakSpeed:
                switch (modifier.math)
                {
                case MathOperator.plus:
                    newStats.breakSpeed += modifier.value * (positive ? 1 : -1);
                    break;

                case MathOperator.minus:
                    newStats.breakSpeed -= modifier.value * (positive ? 1 : -1);
                    break;
                }
                break;

            case CarStat.SteerSpeed:
                switch (modifier.math)
                {
                case MathOperator.plus:
                    newStats.steerSpeed += modifier.value * (positive ? 1 : -1);
                    break;

                case MathOperator.minus:
                    newStats.steerSpeed -= modifier.value * (positive ? 1 : -1);
                    break;
                }
                break;
            }
        }
        return(newStats);
    }
 void Awake()
 {
     CreateCommands();
     car = GetComponent <CarComponent>();
     Debug.Assert(MovementKeys.Length == commands.Length);
 }
    private void UpdateTopViewCamera(CarComponent carData)
    {
        var translatedZ = this.mainCamera.transform.position.z + ((carData.Speed / Settings.KMToTranslationUnit) * Time.deltaTime);

        this.mainCamera.transform.position = new Vector3(this.mainCamera.transform.position.x, this.mainCamera.transform.position.y, translatedZ);
    }
예제 #17
0
    private void PlaceBlock()
    {
        if (_placedBlocks.ContainsKey(_cursorPos))
        {
            return;
        }
        CarComponent component = BuildController.GetComponent(_selectedBlock);

        if (BuildController.GetBuidPoints(_player) < component.buildPoints)
        {
            return;
        }
        BPText.SetText(BuildController.ModifyBuildPoints(-component.buildPoints, _player).ToString());

        GameObject placedBlock;

        if (component.BuildGFX != null)
        {
            placedBlock = Instantiate(component.GFX, _cursorPos + buildOffset, _cursorBlock.transform.rotation, Car.transform);
        }
        else
        {
            placedBlock = Instantiate(component.GFX, _cursorPos + buildOffset, _cursorBlock.transform.rotation, Car.transform);
        }
        placedBlock.GetComponent <MeshRenderer>().sortingOrder = -50;
        placedBlock.AddComponent <BlockProperties>().data      = component;
        //if(component.customMeshChecker != null){
        if (placedBlock.GetComponentInParent <MeshCollider>())
        {
            foreach (MeshCollider coll in placedBlock.GetComponentsInChildren <MeshCollider>())
            {
                Destroy(coll);
            }
        }
        GameObject meshChild = new GameObject("Mesh");

        meshChild.tag = "childmesh";
        MeshCollider cColl = meshChild.AddComponent <MeshCollider>();

        cColl.convex = true;
        if (component.customMeshChecker != null)
        {
            cColl.sharedMesh = component.customMeshChecker;
        }
        else
        {
            Mesh sm = placedBlock.GetComponent <MeshCollider>().sharedMesh;
            cColl.sharedMesh = sm;
        }
        meshChild.transform.SetParent(placedBlock.transform);
        meshChild.transform.localPosition = component.colliderOffset + (cColl.sharedMesh.bounds.extents / 10f) * .8f;
        meshChild.transform.localRotation = Quaternion.identity;
        meshChild.transform.localScale    = new Vector3(.8f, .8f, .8f);

        if (placedBlock.GetComponent <MeshCollider>())
        {
            Destroy(placedBlock.GetComponent <MeshCollider>());
        }

        /*if(placedBlock.GetComponentInParent<MeshCollider>()){
         *      foreach(MeshCollider coll in placedBlock.GetComponentsInChildren<MeshCollider>()){
         *              Destroy(coll);
         *      }
         * }*/
        //}

        _placedBlocks.Add(_cursorPos, placedBlock);
        _blocksPlaced.Add(placedBlock, _cursorPos);

        //CarStats
        carStats = UpdateCarStats(component, carStats, true);
        UpdateStatDisplay();
        PreviewStatDisplay();

        HologramColor(new Color(1f, 0f, 0f, .5f));
    }
예제 #18
0
    private void SelectBlock(int index)
    {
        if (index >= BuildController.GetComponentCount())
        {
            _selectedBlock = 0;
        }
        else if (index < 0)
        {
            _selectedBlock = BuildController.GetComponentCount() - 1;
        }
        else
        {
            _selectedBlock = index;
        }

        if (_cursorBlock != null)
        {
            Destroy(_cursorBlock);
        }

        CarComponent component = BuildController.GetComponent(_selectedBlock);

        if (component.BuildGFX == null)
        {
            _cursorBlock = Instantiate(component.GFX, _cursorPos + buildOffset, component.GFX.transform.rotation, BuildCursor.transform);
        }
        else
        {
            _cursorBlock = Instantiate(component.BuildGFX, _cursorPos + buildOffset, component.BuildGFX.transform.rotation, BuildCursor.transform);
        }
        //Make material a hologram
        HologramColor(new Color(1f, 1f, 1f, .5f));

        //If it has a collider, remove it and put it on the checker
        if (component.customMeshChecker != null)
        {
            meshChecker.sharedMesh = component.customMeshChecker;
            meshChecker.isTrigger  = true;
            if (_cursorBlock.GetComponent <MeshCollider>())
            {
                Destroy(_cursorBlock.GetComponent <MeshCollider>());
            }
            if (_cursorBlock.GetComponentInChildren <MeshCollider>())
            {
                foreach (MeshCollider collider in _cursorBlock.GetComponentsInChildren <MeshCollider>())
                {
                    Destroy(collider);
                }
            }
        }
        else
        {
            if (_cursorBlock.GetComponent <MeshCollider>())
            {
                MeshCollider org = _cursorBlock.GetComponent <MeshCollider>();
                meshChecker.sharedMesh = org.sharedMesh;
                meshChecker.isTrigger  = true;
                Destroy(org);
            }
            if (_cursorBlock.GetComponentInChildren <MeshCollider>())
            {
                foreach (MeshCollider collider in _cursorBlock.GetComponentsInChildren <MeshCollider>())
                {
                    Destroy(collider);
                }
            }
        }
        meshChecker.transform.localPosition = component.colliderOffset;

        _cursorBlock.transform.Rotate(Vector3.up * component.initialRotation, Space.Self);
        meshChecker.transform.rotation = _cursorBlock.transform.rotation;

        PartCostText.SetText(component.buildPoints.ToString());

        PreviewStatDisplay();
    }
예제 #19
0
 public override void Execute()
 {
     CarComponent.On();
 }
예제 #20
0
 public override void Undo()
 {
     CarComponent.Off();
 }
예제 #21
0
    public GameObject Construct(int player)
    {
        if (!File.Exists(Application.dataPath + @"\PlacedBlocksCatergories" + player + ".txt"))
        {
            Debug.Log("No Save-File could be found! Are you sure you saved something?");
            return(null);
        }

        //Get JSON
        string json = File.ReadAllText(Application.dataPath + @"\PlacedBlocksCatergories" + player + ".txt");
        Dictionary <string, BlockSave[]>             stringDict = JsonConvert.DeserializeObject <Dictionary <string, BlockSave[]> >(json);
        Dictionary <CarComponents.Type, BlockSave[]> enumDict   = new Dictionary <CarComponents.Type, BlockSave[]>();
        List <BlockSave> list = new List <BlockSave>();

        foreach (string enumName in stringDict.Keys)
        {
            CarComponents.Type type = (CarComponents.Type)Enum.Parse(typeof(CarComponents.Type), enumName, true);
            enumDict.Add(type, stringDict[enumName]);
            list.AddRange(stringDict[enumName]);
        }

        //Create car
        GameObject car = new GameObject("Car (Player" + player + ")");

        car.transform.position = new Vector3(0f, 5f, 0f);
        Transform hull = new GameObject("Hull").transform;

        hull.SetParent(car.transform);
        hull.position = Vector3.zero;

        //CarStats!
        CarStats carStats = new CarStats();

        foreach (BlockSave blockSave in list)
        {
            GameObject prefab = ReturnBlockToPlace(blockSave.name);
            GameObject block  = Instantiate(prefab, StringToVector3(blockSave.position), StringToQuaternion(blockSave.rotation), hull);

            CarComponent component = (CarComponent)BuildController.GetComponent(blockSave.name);
            foreach (ComponentModifier modifier in component.modifiers)
            {
                switch (modifier.stat)
                {
                case CarStat.MaxSpeed:
                    switch (modifier.math)
                    {
                    case MathOperator.plus:
                        carStats.maxSpeed += modifier.value;
                        break;

                    case MathOperator.minus:
                        carStats.maxSpeed -= modifier.value;
                        break;
                    }
                    break;

                case CarStat.Acceleration:
                    switch (modifier.math)
                    {
                    case MathOperator.plus:
                        carStats.acceleration += modifier.value;
                        break;

                    case MathOperator.minus:
                        carStats.acceleration -= modifier.value;
                        break;
                    }
                    break;

                case CarStat.BreakSpeed:
                    switch (modifier.math)
                    {
                    case MathOperator.plus:
                        carStats.breakSpeed += modifier.value;
                        break;

                    case MathOperator.minus:
                        carStats.breakSpeed -= modifier.value;
                        break;
                    }
                    break;

                case CarStat.SteerSpeed:
                    switch (modifier.math)
                    {
                    case MathOperator.plus:
                        carStats.steerSpeed += modifier.value;
                        break;

                    case MathOperator.minus:
                        carStats.steerSpeed -= modifier.value;
                        break;
                    }
                    break;
                }
            }

            if (BuildController.GetComponent(blockSave.name).type == CarComponents.Type.Wheel)
            {
                if (StringToQuaternion(blockSave.rotation).eulerAngles.y == 90)
                {
                    wheelsL.Add(block);
                }
                else if (StringToQuaternion(blockSave.rotation).eulerAngles.y == 270)
                {
                    wheelsR.Add(block);
                }
            }
        }
        if (overideStats)
        {
            carStats = editorStats;
        }

        //Combine all meshes into one
        MeshFilter[]      meshFilters = car.GetComponentsInChildren <MeshFilter>();
        CombineInstance[] combine     = new CombineInstance[meshFilters.Length];
        for (int i = 0; i < meshFilters.Length; i++)
        {
            combine[i].mesh      = meshFilters[i].sharedMesh;
            combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
            if (meshFilters[i].GetComponent <MeshCollider>())
            {
                meshFilters[i].GetComponent <MeshCollider>().enabled = false;
            }
        }
        Mesh combinedMesh = new Mesh();

        combinedMesh.CombineMeshes(combine);

        /*BoxCollider boxCollider = hull.gameObject.AddComponent<BoxCollider>();
         * boxCollider.size = combinedMesh.bounds.extents * 2f;
         * boxCollider.center = combinedMesh.bounds.center;*/
        MeshCollider combinedCollider = hull.gameObject.AddComponent <MeshCollider>();

        combinedCollider.sharedMesh = combinedMesh;
        combinedCollider.convex     = true;

        //Setting up Car Controls
        Rigidbody hullRB = hull.gameObject.AddComponent <Rigidbody>();

        hullRB.interpolation          = RigidbodyInterpolation.Extrapolate;
        hullRB.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
        hullRB.angularDrag            = 30f;
        hullRB.mass             = .5f;
        hullRB.gameObject.layer = 9;
        CarController cCont = hull.gameObject.AddComponent <CarController>();

        cCont.player   = player;
        cCont.carStats = carStats;
        cCont.collider = combinedCollider;

        cCont._drive = true;

        GameObject cam = GameObject.FindGameObjectWithTag("player" + player);
        Vector3    pos = cam.transform.position;

        cam.transform.SetParent(hull);
        cam.transform.position = new Vector3(0f, pos.y, pos.z);

        car.transform.position += new Vector3(player * 25f, 0f, 0f);

        // endpoint of Construct.
        return(car);
    }
예제 #22
0
 public override void Undo()
 {
     CarComponent.Down();
 }
예제 #23
0
 public AirRideDecorator(CarComponent baseComponent)
     : base(baseComponent)
 {
     this.m_Name = "AirRide";
     this.m_Price = 6000.0;
 }
예제 #24
0
 protected Decorator(CarComponent baseComponent)
 {
     m_BaseComponent = baseComponent;
 }
예제 #25
0
    private void LoadBuild()
    {
        if (!File.Exists(Application.dataPath + @"\PlacedBlocksCatergories" + _player + ".txt"))
        {
            Debug.Log("No Save-File could be found! Are you sure you saved something?");
            return;
        }

        //If there are already blocks placed down, this will clear the placed ones.
        if (_placedBlocks.Count > 0)
        {
            for (int b = 0; b < _placedBlocks.Count; b++)
            {
                Destroy(_placedBlocks.ElementAt(b).Value);
            }

            _placedBlocks.Clear();
            _blocksPlaced.Clear();
        }

        //Get JSON
        string json = File.ReadAllText(Application.dataPath + @"\PlacedBlocksCatergories" + _player + ".txt");
        Dictionary <string, BlockSave[]>             stringDict = JsonConvert.DeserializeObject <Dictionary <string, BlockSave[]> >(json);
        Dictionary <CarComponents.Type, BlockSave[]> enumDict   = new Dictionary <CarComponents.Type, BlockSave[]>();
        List <BlockSave> list = new List <BlockSave>();

        foreach (string enumName in stringDict.Keys)
        {
            CarComponents.Type type = (CarComponents.Type)Enum.Parse(typeof(CarComponents.Type), enumName, true);
            enumDict.Add(type, stringDict[enumName]);
            list.AddRange(stringDict[enumName]);
        }

        foreach (BlockSave blockSave in list)
        {
            CarComponent component   = BuildController.GetComponent(blockSave.name);
            GameObject   prefab      = ReturnBlockToPlace(blockSave.name);
            GameObject   placedBlock = Instantiate(prefab, Vector3.zero, StringToQuaternion(blockSave.rotation), Car.transform);
            placedBlock.transform.localPosition = StringToVector3(blockSave.position);
            placedBlock.GetComponent <MeshRenderer>().sortingOrder = -50;
            placedBlock.AddComponent <BlockProperties>().data      = component;
            //if(component.customMeshChecker != null){
            if (placedBlock.GetComponentInParent <MeshCollider>())
            {
                foreach (MeshCollider coll in placedBlock.GetComponentsInChildren <MeshCollider>())
                {
                    Destroy(coll);
                }
            }
            GameObject meshChild = new GameObject("Mesh");
            meshChild.tag = "childmesh";
            MeshCollider cColl = meshChild.AddComponent <MeshCollider>();
            cColl.convex = true;
            if (component.customMeshChecker != null)
            {
                cColl.sharedMesh = component.customMeshChecker;
            }
            else
            {
                Mesh sm = placedBlock.GetComponent <MeshCollider>().sharedMesh;
                cColl.sharedMesh = sm;
            }
            meshChild.transform.SetParent(placedBlock.transform);
            meshChild.transform.localPosition = component.colliderOffset + (cColl.sharedMesh.bounds.extents / 10f) * .8f;
            meshChild.transform.localRotation = Quaternion.identity;
            meshChild.transform.localScale    = new Vector3(.8f, .8f, .8f);

            if (placedBlock.GetComponent <MeshCollider>())
            {
                Destroy(placedBlock.GetComponent <MeshCollider>());
            }
            _placedBlocks.Add(StringToVector3(blockSave.position), placedBlock);
            _blocksPlaced.Add(placedBlock, StringToVector3(blockSave.position));

            //CarStats
            carStats = UpdateCarStats(component, carStats, true);
            UpdateStatDisplay();
            PreviewStatDisplay();
        }
    }
예제 #26
0
    private GameObject Construct(int player)
    {
        if (!File.Exists(Application.dataPath + @"\PlacedBlocksCatergories" + player + ".txt"))
        {
            Debug.Log("No Save-File could be found! Are you sure you saved something?");             //TODO show on screen.
            return(null);
        }

        //Get JSON
        string json = File.ReadAllText(Application.dataPath + @"\PlacedBlocksCatergories" + player + ".txt");
        Dictionary <string, BlockSave[]>             stringDict = JsonConvert.DeserializeObject <Dictionary <string, BlockSave[]> >(json);
        Dictionary <CarComponents.Type, BlockSave[]> enumDict   = new Dictionary <CarComponents.Type, BlockSave[]>();
        List <BlockSave> list = new List <BlockSave>();

        foreach (string enumName in stringDict.Keys)
        {
            CarComponents.Type type = (CarComponents.Type)Enum.Parse(typeof(CarComponents.Type), enumName, true);
            enumDict.Add(type, stringDict[enumName]);
            list.AddRange(stringDict[enumName]);
        }

        GameObject car = new GameObject("Car P" + player);
        //1. Static meshes (hull)
        GameObject hull = new GameObject("Hull");

        hull.transform.SetParent(car.transform);
        //2. Wheel Visuals (renderers)
        GameObject wheelRenderers = new GameObject("Wheels");

        wheelRenderers.transform.SetParent(car.transform);

        CarStats carStats = new CarStats();

        //Fill the containers
        foreach (BlockSave blockSave in list)
        {
            GameObject   prefab    = ReturnBlockToPlace(blockSave.name);
            CarComponent component = (CarComponent)BuildController.GetComponent(blockSave.name);
            GameObject   block;
            switch (component.type)
            {
            case CarComponents.Type.Wheel:
                block = Instantiate(prefab, StringToVector3(blockSave.position), StringToQuaternion(blockSave.rotation), wheelRenderers.transform);
                break;

            default:
                block = Instantiate(prefab, StringToVector3(blockSave.position), StringToQuaternion(blockSave.rotation), hull.transform);
                break;
            }
            carStats = ModifyCarStats(component, carStats);
        }
        ///Setup rigidbody + collider
        //Combine all meshes into one
        MeshFilter[]      meshFilters = hull.GetComponentsInChildren <MeshFilter>();
        CombineInstance[] combine     = new CombineInstance[meshFilters.Length];
        for (int i = 0; i < meshFilters.Length; i++)
        {
            combine[i].mesh      = meshFilters[i].sharedMesh;
            combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
            if (meshFilters[i].GetComponent <MeshCollider>())
            {
                meshFilters[i].GetComponent <MeshCollider>().enabled = false;
            }
        }
        Mesh combinedMesh = new Mesh();

        combinedMesh.CombineMeshes(combine);
        MeshCollider combinedCollider = car.gameObject.AddComponent <MeshCollider>();

        combinedCollider.sharedMesh = combinedMesh;
        combinedCollider.convex     = true;
        Rigidbody carRb = car.AddComponent <Rigidbody>();

        carRb.mass = 1500;

        //3. Wheel Colliders (colliders)
        List <WheelCollider> wheelColls   = new List <WheelCollider>();
        List <Transform>     wheelCollsTs = new List <Transform>();
        List <Transform>     wheelTrans   = new List <Transform>();

        GameObject wheelColliders = Instantiate(wheelRenderers, wheelRenderers.transform.position, wheelRenderers.transform.rotation, car.transform);

        wheelColliders.name = "WheelCollliders";

        //we need the lists to start with the two front wheels
        Transform front1col = null;
        Transform front2col = null;

        for (int i = 0; i < wheelColliders.transform.childCount; i++)
        {
            Transform child = wheelColliders.transform.GetChild(i);
            if (front1col == null)
            {
                front1col = child;
            }
            else
            {
                if (child.position.z > front1col.position.z)
                {
                    front1col = child;
                }
            }
        }
        for (int i = 0; i < wheelColliders.transform.childCount; i++)
        {
            Transform child = wheelColliders.transform.GetChild(i);
            if (child != front1col)
            {
                if (front2col == null)
                {
                    front2col = child;
                }
                else
                {
                    if (child.position.z > front2col.position.z)
                    {
                        front2col = child;
                    }
                }
            }
        }
        wheelCollsTs.Add(front1col);
        wheelCollsTs.Add(front2col);

        for (int child = 0; child < wheelColliders.transform.childCount; child++)
        {
            Transform wheel = wheelColliders.transform.GetChild(child);

            //Remove all renderers
            List <MeshRenderer> renderers = new List <MeshRenderer>();
            renderers.AddRange(wheel.GetComponents <MeshRenderer>());
            renderers.AddRange(wheel.GetComponentsInChildren <MeshRenderer>());
            foreach (MeshRenderer renderer in renderers)
            {
                Destroy(renderer);
            }
            List <MeshFilter> filters = new List <MeshFilter>();
            filters.AddRange(wheel.GetComponents <MeshFilter>());
            filters.AddRange(wheel.GetComponentsInChildren <MeshFilter>());
            foreach (MeshFilter filter in filters)
            {
                Destroy(filter);
            }

            //Remove all colliders
            List <Collider> colls = new List <Collider>();
            colls.AddRange(wheel.GetComponents <Collider>());
            colls.AddRange(wheel.GetComponentsInChildren <Collider>());

            //Add Wheel Colliders
            WheelCollider wheelColl = wheel.gameObject.AddComponent <WheelCollider>();
            wheelColl.mass               = 10f;
            wheelColl.radius             = 1f;
            wheelColl.wheelDampingRate   = 1f;
            wheelColl.suspensionDistance = .08f;

            if (!wheelCollsTs.Contains(wheel))
            {
                wheelCollsTs.Add(wheel);
            }


            foreach (Collider coll in colls)
            {
                Destroy(coll);
            }
        }
        foreach (Transform wheelColTs in wheelCollsTs)
        {
            wheelColls.Add(wheelColTs.GetComponent <WheelCollider>());
        }

        //Clear components from the wheel renderers
        Transform front1ts = null;
        Transform front2ts = null;

        for (int i = 0; i < wheelRenderers.transform.childCount; i++)
        {
            Transform child = wheelRenderers.transform.GetChild(i);
            if (front1ts == null)
            {
                front1ts = child;
            }
            else
            {
                if (child.position.z > front1ts.position.z)
                {
                    front1ts = child;
                }
            }
        }
        for (int i = 0; i < wheelRenderers.transform.childCount; i++)
        {
            Transform child = wheelRenderers.transform.GetChild(i);
            if (child != front1ts)
            {
                if (front2ts == null)
                {
                    front2ts = child;
                }
                else
                {
                    if (child.position.z > front2ts.position.z)
                    {
                        front2ts = child;
                    }
                }
            }
        }
        wheelTrans.Add(front1ts);
        wheelTrans.Add(front2ts);

        GameObject[] wheelRender = new GameObject[wheelRenderers.transform.childCount];
        for (int i = 0; i < wheelRenderers.transform.childCount; i++)
        {
            wheelRender[i] = wheelRenderers.transform.GetChild(i).gameObject;
        }
        foreach (GameObject wheel in wheelRender)
        {
            GameObject wheelParent = new GameObject("Wheelparent");
            wheelParent.transform.SetParent(wheel.transform.parent);
            wheelParent.transform.position = wheel.transform.position;
            wheelParent.transform.rotation = wheel.transform.rotation;
            wheel.transform.SetParent(wheelParent.transform);
            if (!wheelTrans.Contains(wheel.transform))
            {
                wheelTrans.Add(wheel.transform);
            }

            //Remove all colliders
            List <Collider> colls = new List <Collider>();
            colls.AddRange(wheelParent.GetComponents <Collider>());
            colls.AddRange(wheelParent.GetComponentsInChildren <Collider>());
            foreach (Collider coll in colls)
            {
                Destroy(coll);
            }
        }

        NewCarController controller = car.AddComponent <NewCarController>();

        controller.wheelColliders  = wheelColls.ToArray();
        controller.wheelTransforms = wheelTrans.ToArray();
        controller.player          = player;
        controller.particleSystems = car.GetComponentsInChildren <ParticleSystem>();
        controller.rb       = carRb;
        controller.carStats = carStats;

        if (player == 1)
        {
            car.transform.position = spawnPosP1;
            controller.spawnPos    = spawnPosP1;
        }
        else
        {
            car.transform.position = spawnPosP2;
            controller.spawnPos    = spawnPosP2;
        }

        SetLayerRecursively(car, 9);
        carRb.drag = .5f;
        carStats.terrainMultiplier = 1f;
        //carRb.angularDrag = .5f;
        return(car);
    }
예제 #27
0
 public NOSDecorator(CarComponent baseComponent)
     : base(baseComponent)
 {
     this.m_Name = "NOS";
     this.m_Price = 2000.0;
 }
예제 #28
0
    //TODO we dont need to add terrainmultiplier here right? :thinking:
    private CarStats ModifyCarStats(CarComponent component, CarStats stats)
    {
        CarStats carStats = new CarStats(stats.maxSpeed, stats.acceleration, stats.breakSpeed, stats.steerSpeed, stats.terrainMultiplier);

        foreach (ComponentModifier modifier in component.modifiers)
        {
            switch (modifier.stat)
            {
            case CarStat.MaxSpeed:
                switch (modifier.math)
                {
                case MathOperator.plus:
                    carStats.maxSpeed += modifier.value;
                    break;

                case MathOperator.minus:
                    carStats.maxSpeed -= modifier.value;
                    break;
                }
                break;

            case CarStat.Acceleration:
                switch (modifier.math)
                {
                case MathOperator.plus:
                    carStats.acceleration += modifier.value;
                    break;

                case MathOperator.minus:
                    carStats.acceleration -= modifier.value;
                    break;
                }
                break;

            case CarStat.BreakSpeed:
                switch (modifier.math)
                {
                case MathOperator.plus:
                    carStats.breakSpeed += modifier.value;
                    break;

                case MathOperator.minus:
                    carStats.breakSpeed -= modifier.value;
                    break;
                }
                break;

            case CarStat.SteerSpeed:
                switch (modifier.math)
                {
                case MathOperator.plus:
                    carStats.steerSpeed += modifier.value;
                    break;

                case MathOperator.minus:
                    carStats.steerSpeed -= modifier.value;
                    break;
                }
                break;

            case CarStat.Offroad:
                carStats.offroad += modifier.value;
                break;
            }
        }
        return(carStats);
    }
예제 #29
0
 public HiFiDecorator(CarComponent baseComponent)
     : base(baseComponent)
 {
     this.m_Name = "HiFi";
     this.m_Price = 10000.0;
 }
예제 #30
0
파일: Program.cs 프로젝트: huschste/Pattern
 private static void PrintToScreen(CarComponent product)
 {
     Console.WriteLine();
     Console.WriteLine("Item: {0}, Price: {1}", product.GetName(), product.GetPrice());
        // Console.ReadLine();
 }