1 / 9

Inventory Logic

Inventory Logic. Chapter 13. Layers. Can have multiple layers that can be turned on and off, that is drawn or not Depth parameter gives draw order. Inventory Manager. #pragma strict var iMode = false; // local flag for whether inventory mode is off or on

geoff
Download Presentation

Inventory Logic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inventory Logic Chapter 13

  2. Layers • Can have multiple layers that can be turned on and off, that is drawn or not • Depth parameter gives draw order

  3. Inventory Manager #pragma strict variMode = false; // local flag for whether inventory mode is off or on internal varcontrolCenter : GameObject; varfPController : GameObject; varfPCamera : GameObject; function Start () { camera.enabled = false; controlCenter = GameObject.Find("Control Center");// access the control center iMode = controlCenter.GetComponent(GameManager).iMode; } function Update () { if (Input.GetKeyDown("i")) ToggleMode(); // call the function if i key is pressed } // toggle inventory visibility function ToggleMode () { if (iMode) { // if you are in inventory mode, turn it off camera.enabled = false;// turn off the camera // unblock navigation fPController.GetComponent(CharacterMotor).enabled = true; // turn on navigation fPController.GetComponent(FPAdventurerInputController).enabled = true; // turn on navigation fPController.GetComponent(MouseLookRestricted).enabled = true; // turn on navigation fPCamera.GetComponent(MouseLookRestricted).enabled = true; iMode = false; // change the flag controlCenter.GetComponent(GameManager).iMode = false; // inform the manager } else { // else it was off so turn it on camera.enabled = true;// turn on the camera // block navigation fPController.GetComponent(CharacterMotor).enabled = false; // turn off navigation fPController.GetComponent(FPAdventurerInputController).enabled = false; // turn off navigation fPController.GetComponent(MouseLookRestricted).enabled = false; // turn off navigation fPCamera.GetComponent(MouseLookRestricted).enabled = false; iMode = true; // change the flag controlCenter.GetComponent(GameManager).iMode = true; // inform the manager } }

  4. Fixing Inventory Mode • Should not be able to navigate • Should not be able to pick items • Use iMode flag in Interactor script • GUIButton used to enter and exit inventory mode

  5. 2D Object Handling • 2D objects have three modes • Not in scene • Is in inventory • Is cursor • When 2D object is picked from inventory, it may go back into inventory or disappear • When 2D cursor picks 2D object, objects must switch

  6. 2D Object Handling

  7. 2D Object Handling //handle 2D objects function Handle2D () { // Not in scene -> Is Cursor if (previousState == 0 && currentState == 2) { controlCenter.GetComponent(GameManager).currentCursor = guiTexture.texture; gameObject.guiTexture.enabled = false; } // Not in scene -> In Inventory if (previousState == 0 && currentState == 1) { controlCenter.SendMessage("AddToInventory", gameObject); gameObject.guiTexture.enabled = true; } // Is Cursor -> Not in scene if (previousState == 2 && currentState == 0) { controlCenter.SendMessage("ResetCursor"); yield; gameObject.SetActive(false); // deactivate the object immediately } // Is Cursor -> In Inventory if (previousState == 2 && currentState == 1) { controlCenter.SendMessage("AddToInventory", gameObject); gameObject.guiTexture.enabled = true; controlCenter.SendMessage("ResetCursor"); } // In Inventory -> Not in scene if (previousState == 1 && currentState == 0) { gameObject.guiTexture.enabled = false; controlCenter.SendMessage("RemoveFromInventory", gameObject); yield; gameObject.SetActive(false); // deactivate the object immediately } // In Inventory -> Is Cursor if (previousState == 1 && currentState == 2) { gameObject.guiTexture.enabled = false; controlCenter.SendMessage("RemoveFromInventory", gameObject); controlCenter.GetComponent(GameManager).currentCursor = guiTexture.texture; } }

  8. Raycasting • An alternative to having colliders and scripts for all objects you may interact with

  9. Summary In this chapter, you got your first look at using Layers to be able to composite multiple camera views together. Armed with this new knowledge, you created a camera and layer to house the Inventory screen as an overlay of the regular scene. Adding a new flag, iMode, you devised a means of toggling your new Inventory mode off and on with both a keyboard and onscreen icon. You also repurposed the test GUI Style, Box Style Label, to use for the inventory toggle button. In electing to keep your scene running in the background while accessing inventory, you discovered that you needed to disable navigation and disable mouseover functionality and picks from your 3D action objects yet allow interaction with your 2D inventory and keyboard. With the introduction of a 2D inventory object, you discovered that by adding a new flag to your action objects, objectIs3D, you could make a few changes to your Interactor script and use it for both types of objects. You specified that icon objects would only have three possible states and that enabled you to streamline their processing. For that processing, you blocked in a few new functions, AddToInventory and RemoveFromInventory. With the addition of the fledgling cursor code, you finished the code that passes the correct cursor texture name off to the LookupState function. With that in place, you were able to check the processing with multiple cursors. If no match was found at all, you generated a semi-custom message using the cursor name and the name of the object it picked. Finally, you found that to monitor picks on objects without OnMouseDown functions, you needed to use a raycast when the left mouse button was picked, to find out what object was under the cursor. Once found, you could get its information though a local variable of type RaycastHit. Armed with the new knowledge, you added the functionality to drop a cursor back into its inventory state from a non–Action Object pick. In the next chapter, you will add the functionality that will manage the inventory’s visual representation on screen and allow for as many objects as you need in your game.

More Related