コード例 #1
0
ファイル: Player.cs プロジェクト: lagrangespot/SimpleCraft
        /// <summary>
        /// Shows the item's resource cost in a scrollView
        /// </summary>
        /// <param name="building">Building.</param>
        void DrawCostView(CraftableItem building)
        {
            RectTransform Content;

            Content = _costScrollView.GetComponent <ScrollRect> ().content;

            _costText.text  = building.ItemName;
            _costText.text += "\nCost (required/inventory)";

            foreach (CraftableItem.Cost buildingCost in building.BuildingCost)
            {
                if (!inventory.ContainsKey(buildingCost.resource))
                {
                    _costText.text += "\n" + Enum.GetName(typeof(Resource.Type), buildingCost.resource);
                    _costText.text += " (" + buildingCost.amount + "/0)";
                }
                else
                {
                    _costText.text += "\n" + Enum.GetName(typeof(Resource.Type), buildingCost.resource);
                    _costText.text += " (" + buildingCost.amount + "/" + inventory[buildingCost.resource] + ")";
                }
            }

            Content.GetComponent <RectTransform>().sizeDelta   = new Vector2(0, (building.BuildingCost.Length + 2) * 30);
            _costText.GetComponent <RectTransform>().sizeDelta = new Vector2(160, (building.BuildingCost.Length + 2) * 30);
        }
コード例 #2
0
        public static bool CheckObjective(GameObject building)
        {
            if (building.GetComponent <CraftableItem> () == null)
            {
                return(false);
            }

            CraftableItem b = building.GetComponent <CraftableItem> ();

            foreach (CraftableItem objective in _manager.objectiveBuilding)
            {
                if (b.ItemName == objective.ItemName)
                {
                    _manager.objectiveBuilding.Remove(objective);
                    if (_manager.objectiveBuilding.Count == 0)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            return(false);
        }
コード例 #3
0
ファイル: Player.cs プロジェクト: lagrangespot/SimpleCraft
 /// <summary>
 /// Takes the item's cost resources out of the inventory.
 /// </summary>
 /// <param name="building">Building.</param>
 void TakeResources(CraftableItem craftableItem)
 {
     foreach (CraftableItem.Cost buildingCost in craftableItem.BuildingCost)
     {
         AddResource(buildingCost.resource, -buildingCost.amount);
     }
     this.DrawCostView(craftableItem);
 }
コード例 #4
0
ファイル: Player.cs プロジェクト: lagrangespot/SimpleCraft
 /// <summary>
 /// Check if there are enough resources on the inventory in order
 /// to craft the item.
 /// </summary>
 /// <returns><c>true</c>, if resources was hased, <c>false</c> otherwise.</returns>
 /// <param name="building">Building.</param>
 bool HasResources(CraftableItem craftableItem)
 {
     foreach (CraftableItem.Cost buildingCost in craftableItem.BuildingCost)
     {
         if (!inventory.ContainsKey(buildingCost.resource))
         {
             return(false);
         }
         if (inventory [buildingCost.resource] < buildingCost.amount)
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #5
0
ファイル: Player.cs プロジェクト: lagrangespot/SimpleCraft
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                ShowPauseMenu();
            }

            if (_menuMode)
            {
                return;
            }

            if (Input.GetKeyDown(KeyCode.B))
            {
                _buildingMode = !_buildingMode;
                if (_itemObj == null)
                {
                    _itemObj = Manager.GetBuilding(_itemIdx);
                }
                if (_itemObj != null)
                {
                    _itemObj.SetActive(_buildingMode);
                    _actionText.text = "";
                    _costScrollView.SetActive(_buildingMode);
                    _currentItem = _itemObj.GetComponent <CraftableItem> ();
                    this.DrawCostView(_currentItem);
                }
            }

            if (_buildingMode)
            {
                OnBuildingMode();
            }
            else
            {
                if (Input.GetMouseButtonDown(0) && _toolHandler.CurrentTool != null)
                {
                    _toolHandler.Attack();
                }

                if (Input.GetKeyDown(KeyCode.E) && _interactionObj != null)
                {
                    if (_interaction == Interaction.GrabTool)
                    {
                        _tools.Add(Manager.GetTool(_interactionObj.GetComponent <Tool> ().ItemName));
                        Destroy(_interactionObj);
                        _interactionObj = null;
                    }
                    else if (_interaction == Interaction.GrabResource)
                    {
                        Resource resource = _interactionObj.GetComponent <Resource> ();
                        this.AddResource(resource.ResType, resource.Amount);
                        resource.Gather(resource.Amount);
                        _interactionObj = null;
                    }
                }

                if (Input.GetKeyDown(KeyCode.G))
                {
                    _tools.Remove(_toolHandler.ToolObject);
                    _toolHandler.Drop();
                }

                if (Input.GetAxis("Mouse ScrollWheel") != 0)
                {
                    if (_tools.Count != 0)
                    {
                        if (Input.GetAxis("Mouse ScrollWheel") > 0)
                        {
                            if (_toolIdx < _tools.Count - 1)
                            {
                                _toolIdx += 1;
                            }
                            else
                            {
                                _toolIdx = 0;
                            }
                        }
                        else if (Input.GetAxis("Mouse ScrollWheel") < 0)
                        {
                            if (_toolIdx > 0)
                            {
                                _toolIdx -= 1;
                            }
                            else
                            {
                                _toolIdx = _tools.Count - 1;
                            }
                        }

                        _toolHandler.ChangeTool(_tools[_toolIdx]);
                    }
                }

                if (Input.GetKeyDown(KeyCode.I))
                {
                    _scrollView.SetActive(!_scrollView.activeSelf);
                    if (_scrollView.activeSelf)
                    {
                        this.DrawInventory();
                    }
                }

                CheckPlayerFocus();
            }
        }
コード例 #6
0
ファイル: Player.cs プロジェクト: lagrangespot/SimpleCraft
        void OnBuildingMode()
        {
            if (_currentItem == null)
            {
                return;
            }

            _raycaster.position = transform.position + transform.forward * _currentItem.Offset;
            _raycaster.position = new Vector3(_raycaster.position.x, _raycaster.position.y + 5, _raycaster.position.z);

            if (Physics.Raycast(_raycaster.position, _raycaster.forward, out _hit, 20, _CraftLayers.value))
            {
                if (Vector3.Distance(_hit.point, this.gameObject.transform.position) >= _currentItem.Offset)
                {
                    if (UnityEngine.AI.NavMesh.SamplePosition(_hit.point, out _hitTerrain, 100.0f, UnityEngine.AI.NavMesh.AllAreas))
                    {
                        _itemObj.transform.position = new Vector3(_hitTerrain.position.x, _hitTerrain.position.y, _hitTerrain.position.z);
                    }
                }
            }

            if (_currentItem != null)
            {
                if (!_currentItem.CanBuild())
                {
                    _actionText.text = "Can't build there!";
                }
                else if (!HasResources(_currentItem))
                {
                    _actionText.text = "Lack of the required resources!";
                }
                else
                {
                    _actionText.text = "";
                }
            }

            if (Input.GetAxis("Mouse ScrollWheel") != 0)
            {
                if (Input.GetAxis("Mouse ScrollWheel") > 0)
                {
                    if (_itemIdx < Manager.GetBuildingLength() - 1)
                    {
                        _itemIdx += 1;
                    }
                    else
                    {
                        _itemIdx = 0;
                    }
                }
                else if (Input.GetAxis("Mouse ScrollWheel") < 0)
                {
                    if (_itemIdx > 0)
                    {
                        _itemIdx -= 1;
                    }
                    else
                    {
                        _itemIdx = Manager.GetBuildingLength() - 1;
                    }
                }

                _itemObj.SetActive(false);
                Destroy(_itemObj);
                _itemObj = null;
                _itemObj = Manager.GetBuilding(_itemIdx);
                _itemObj.SetActive(true);

                _currentItem = _itemObj.GetComponent <CraftableItem> ();
                this.DrawCostView(_currentItem);

                if (_currentItem.HasRigidBody && _itemObj.GetComponent <Rigidbody> () != null)
                {
                    _itemObj.GetComponent <Rigidbody> ().detectCollisions = false;
                }
            }

            //Rotate item on mouse click
            if (Input.GetMouseButton(1))
            {
                _itemObj.transform.Rotate(0, 30 * Time.deltaTime, 0);
            }
            else if (Input.GetMouseButton(0))
            {
                _itemObj.transform.Rotate(0, -30 * Time.deltaTime, 0);
            }

            //try to place the item
            if (Input.GetKeyDown(KeyCode.E))
            {
                if (this.HasResources(_currentItem) && _currentItem.CanBuild())
                {
                    TakeResources(_currentItem);
                    GameObject g = Instantiate(_itemObj);

                    if (Manager.CheckObjective(_itemObj))
                    {
                        _objectiveText.text = "Objective Completed";
                    }

                    if (_currentItem.HasRigidBody && g.GetComponent <Rigidbody> () != null)
                    {
                        g.GetComponent <Rigidbody> ().detectCollisions = true;
                    }
                }
            }
        }