void Start() { turrets = new GUIContent[placeablePrefabs.Length]; //foreach (Object obj in placeablePrefabs) for (int i = 0; i < placeablePrefabs.Length; ++i) { Object prefab = placeablePrefabs [i]; if (prefab is GameObject) { Purchaseable placeable = ((GameObject)prefab).GetComponentsInChildren <Purchaseable> (true) [0]; turrets [i] = new GUIContent(placeable.nameToDisplay, placeable.icon); } } //arcane bit shift that's needed to get the "layer mask", used for raycasting groundLayerMask = 1 << LayerMask.NameToLayer("GroundLayer"); //adjust rotation (since aspectRatio resizes on another axis) rotateObjOnY(lightObj, 90f); lightProj = lightObj.GetComponent <Projector> (); lightProj.orthographic = true; lightTrig = lightObj.GetComponent <Trigger> (); lightProj.enabled = false; }
public bool CanPlace() { Purchaseable purchase = GetComponent <Purchaseable>(); if (purchase) { if (spawnCell) { return((purchase.WasPurchased() || purchase.CanBePurchased()) && spawnCell.IsOpen()); } else { return(false); } } else if (spawnCell) { return(spawnCell.IsOpen()); } else { return(false); } //return spawnCell && (purchase && purchase.WasPurchased() && spawnCell.IsOpen()) || (!purchase && spawnCell.IsOpen()); }
public int GetPurchasedCount(int menuId, Purchaseable purchaseable) { if (menuId != UpgradeMenuId) { return(0); } return(GameManager.Instance.UpgradeManager.PurchasedCount(purchaseable.GetId())); }
public void Initialize(int menuId, PurchaseableListMenuPresenter presenter, Purchaseable purchaseable) { this.menuId = menuId; this.presenter = presenter; this.purchaseable = purchaseable; imgBackground = GetComponent <Image>(); ReloadUI(); }
public bool Purchase(Purchaseable purchaseItem) { if (money >= purchaseItem.GetPrice()) { money -= purchaseItem.GetPrice(); return(true); } return(false); }
public void MakePurchase(Purchaseable purchase) { if (purchase.Price <= Coins) { Coins -= purchase.Price; Purchases.Add(purchase); purchase.Purchased = true; } }
private void UpdateShopUI() { if (IsShopStocked) { Purchaseable selectedItem = shopInventory[selectedShopInventorySlot]; currSelectedItemRawImage.texture = selectedItem.IconTex; priceText.text = "Price: " + selectedItem.Price; descriptionText.text = "Description:\n" + selectedItem.Description; } else { priceText.text = ""; descriptionText.text = "Description:"; } }
GameObject CreateTurret(int turretKind, Vector3 position, float yRotation) { GameObject prefab = (GameObject)placeablePrefabs [turretKind]; //consume coins Purchaseable placeable = prefab.GetComponentsInChildren <Purchaseable> (true) [0]; myGameInfo.coins -= placeable.price; //correct placing position with height Transform pTransform = prefab.GetComponentsInChildren <Transform> (true) [0]; Vector3 placingPosition = new Vector3(position.x, position.y + pTransform.position.y, position.z); //create and place turret Quaternion rotation = Quaternion.Euler(0, yRotation, 0); return((GameObject)Instantiate(prefab, placingPosition, rotation)); }
public void Initialize(int menuId, PurchaseableListMenuPresenter presenter) { this.menuId = menuId; this.presenter = presenter; if (presenter.ShouldDisplayOverallProgressBar(menuId)) { GameObject progressBar = (GameObject)Instantiate(overallProgressPrefab, container.transform); progressBar.transform.localScale = Vector3.one; overallProgressBar = progressBar.GetComponentInChildren <Slider>(); overallProgressBar.interactable = false; overallProgressLabel = progressBar.GetComponentInChildren <Text>(); } Purchaseable[] purchaseables = presenter.GetPurchaseables(menuId); listElements = new PurchaseableListMenuItem[purchaseables.Length]; int tierId = -1; bool hasTiers = presenter.HasTiers(menuId); for (int i = 0; i < purchaseables.Length; i++) { Purchaseable purchaseable = purchaseables[i]; if (hasTiers && tierId != purchaseable.GetTier()) { GameObject tierHeader = (GameObject)Instantiate(purchaseableTierHeaderPrefab, container.transform); tierHeader.transform.localScale = Vector3.one; tierHeader.GetComponent <PurchaseableTierMenuItem>().SetTier(purchaseable.GetTier()); tierId = purchaseable.GetTier(); } GameObject row = (GameObject)Instantiate(purchaseableListItemPrefab, container.transform); row.transform.localScale = Vector3.one; PurchaseableListMenuItem item = row.GetComponent <PurchaseableListMenuItem>(); item.Initialize(menuId, presenter, purchaseable); listElements[i] = item; } }
public PurchaseableListMenu.PurchaseState GetPurchaseState(int menuId, Purchaseable purchaseable) { switch (menuId) { case DeviceMenuId: if (purchaseable.GetId() == GameManager.Instance.GameState.DeviceId) { return(PurchaseableListMenu.PurchaseState.Purchased); } else if (purchaseable.GetId() > GameManager.Instance.GameState.DeviceId) { return(PurchaseableListMenu.PurchaseState.CanPurchase); } break; case StorageUnitMenuId: if (purchaseable.GetId() == GameManager.Instance.GameState.StorageUnitId) { return(PurchaseableListMenu.PurchaseState.Purchased); } else if (purchaseable.GetId() > GameManager.Instance.GameState.StorageUnitId) { return(PurchaseableListMenu.PurchaseState.CanPurchase); } break; case UpgradeMenuId: if (!GameManager.Instance.UpgradeManager.IsTierUnlocked(purchaseable.GetTier())) { return(PurchaseableListMenu.PurchaseState.Locked); } else if (GetPurchasedCount(menuId, purchaseable) < purchaseable.GetQuantity()) { return(PurchaseableListMenu.PurchaseState.CanPurchase); } break; } return(PurchaseableListMenu.PurchaseState.CannotPurchase); }
//sell a given item public void sell(GameObject item) { Upgradable upgrader = item.GetComponent <Upgradable>(); Purchaseable purchase = item.GetComponent <Purchaseable>(); if (purchase) { if (purchase.WasPurchased()) { if (upgrader) { money += upgrader.getSellValue(); } else { money += purchase.GetPrice(); } } Destroy(item); item = null; } }
public void OnPurchase(int menuId, Purchaseable purchaseable) { Debug.Log("OnPurchase(" + menuId + ", " + purchaseable.GetName() + ")"); GameManager.Instance.GameState.StoredBits -= purchaseable.GetCost(); switch (menuId) { case DeviceMenuId: GameManager.Instance.DeviceManager.SetDevice(purchaseable.GetId()); deviceList.ReloadUI(); break; case StorageUnitMenuId: GameManager.Instance.StorageUnitManager.SetStorageUnit(purchaseable.GetId()); storageUnitList.ReloadUI(); break; case UpgradeMenuId: GameManager.Instance.UpgradeManager.PurchaseUpgrade(purchaseable.GetId()); upgradeList.ReloadUI(); break; } }
public void ActionCall(Purchaseable obj) { Call(obj); }
public void Update() { if (dragging) { if (placement) { //select the proper current highlight color Color newHighlightColor; if (placement.CanPlace()) { if (selectable && selectable.IsSelected()) { newHighlightColor = Color.yellow; } else { newHighlightColor = Color.green; } } else { newHighlightColor = Color.red; } if (newHighlightColor != highlightColor) { SetHighlightColor(newHighlightColor); } if (pointer) { if (placement.spawnCell) { pointer.target = placement.spawnCell.transform; } else { pointer.target = null; } } } } Purchaseable purchase = this.GetComponent <Purchaseable>(); if (!purchase.WasPurchased()) { if (costText == null) { costText = GetComponentInChildren <TextDisplay>().Add(0); costText.text = "Cost: " + purchase.GetPrice(); } if (purchase.CanBePurchased()) { if (fundsAlertText) { GetComponentInChildren <TextDisplay>().Remove(fundsAlertText); fundsAlertText = null; } if (costText) { costText.color = Color.green; } } else { if (!fundsAlertText) { fundsAlertText = GetComponentInChildren <TextDisplay>().Add(1); fundsAlertText.text = "INSUFFICIENT FUNDS"; fundsAlertText.color = Color.red; } if (costText) { costText.color = Color.red; } } } }
public bool CanPurchase(Purchaseable purchaseItem) { return(money >= purchaseItem.GetPrice()); }
//this is the placing of the turret void Update() { if (startedPlacing) { GameObject.Find("CameraController").SendMessage("ActivateMainCamera"); // TODO: Create a button that asks to return to 3rd person mode ??? } lastUpdateFrame = Time.frameCount; if (lastUpdateFrame > lastOnGUIFrame) { OnGUICallsThisFrame = 0; } //the first click is the one on the GUI and is thus discarded //TODO: use a "ghost" when positioning the turrets and walls, otherwise it can be placed over a wolf if (placingTurret) { if (startedPlacing) { Object prefab = placeablePrefabs [selectedTurret]; //TODO: disable buttons when there are not enough money Purchaseable placeable = ((GameObject)prefab).GetComponentsInChildren <Purchaseable> (true) [0]; if (myGameInfo.coins < placeable.price) { Debug.Log("Not enough money"); placingTurret = false; startedPlacing = false; GameObject.Find("CameraController").SendMessage("ActivateThirdPersonCamera"); return; } lightProj.enabled = true; //this is to put the turret at the right height from the ground, please do not use negative y Transform pTransform = ((GameObject)prefab).GetComponentsInChildren <Transform> (true) [0]; placeablePosY = pTransform.position.y; //get size of the box enclosing the prefab (MUST have a MeshFilter set for this to work) //NOTE: there is a bug in Unity that sometimes makes the MeshFilter component disappear Renderer pRenderer = ((GameObject)prefab).GetComponentsInChildren <Renderer> (true) [0]; Vector3 boxSize = pRenderer.bounds.size; //align projector to longest dimension of prefab (the x rotation is to make it point downwards) if (boxSize.z > boxSize.x) { lightObj.transform.rotation = Quaternion.Euler(new Vector3(90f, 90f, 0f)); } else { lightObj.transform.rotation = Quaternion.Euler(new Vector3(90f, 0f, 0f)); } //match size of prefab lightProj.orthoGraphicSize = boxSize.x / 2f; lightProj.aspectRatio = boxSize.z / boxSize.x; BoxCollider lightColl = lightObj.GetComponent <BoxCollider> (); //we need this correction to account for the x rotation of the projector lightColl.size = new Vector3(boxSize.z, boxSize.x, boxSize.y); startedPlacing = false; } if (Input.GetKeyDown(KeyCode.Escape)) { placingTurret = false; } //right mouse click to rotate placing position if (Input.GetMouseButtonDown(1)) { rotateObjOnY(lightObj, 90f); } //cast ray from camera to ground, get intersection point with ground layer and move light there Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, groundLayerMask)) { lightObj.transform.position = new Vector3(hitInfo.point.x, hitInfo.point.y + placeablePosY, hitInfo.point.z); //TODO: replace discardClick //allow placing only if zone is free if (Input.GetMouseButtonDown(0) && !discardClick && !lightTrig.triggered) { CreateTurret(selectedTurret, hitInfo.point, lightObj.transform.rotation.eulerAngles.y - 90f); lightProj.enabled = false; placingTurret = false; GameObject.Find("CameraController").SendMessage("ActivateThirdPersonCamera"); //TODO: check what happens to the paths, are they updated when the grid is updated? } } } }