Saturday, 16 January 2016

Save/Load Game

Brief Description

In order to create a game that anyone would want to play I need to implement a feature allowing the user to save their restaurant upon leaving the game and then loading it when the game is relaunched. 

I decided quickly I wanted to implement this feature by saving all relevant data into a text file and then read the text file into the game to load in the relevant game objects.

I wanted to create my save feature like this so it would have little impact on memory and would be very quick to save and load up games.

Saving a Game

Saving my game is simply the result of writing data to a text file using the stream writer asset included within Unity's System.IO framework. The stream writer class requires a path, location where the file will be saved, and a file name. I only allow one save file per user therefore the name of the file is simply savefile.txt.

With a stream writer open to the desired location I now start adding string data to the text file. The data I need for my game assets are it's position, rotation, texture index mapping to my texture database (used to save wallpaper/tile textures), the item's spawned status (determines if walls were pre-placed or user placed) and the assets name. 

This data would be used to locate the prefab via the asset name, instantiate the game object using the position data, rotate the object with the rotation data, locate the objects texture from my database using the texture index so it looks exactly the same and set whether the object can be sold using the spawned value.

Once I have written all the lines of text I want to the text file I need to close the open channel to the text file by simply calling the streamwriter.close method.
Figure 1

You'll notice in figure 1 that my data is broken up by apostrophes. The reason I have formatted my text like this is so that when I read the data back into my game I have an obvious character I can use to break the line back into multiple strings. I'm able to read in all the characters until the apostrophe is reached and then add the next characters to a new string value.

Loading a Game

Loading in my game is virtually identical to saving my game expect in reverse. This time I will be using the stream reader asset from Unity's System.IO framework. The stream reader reads in all the data from the text file line by line and I store it to a string. In order to separate the data I create a new array of strings to separate the file into lines using '/n' as the separator. This means that whenever a new line, represented by /n, is detonated I split the the string and add it to my array.

Once I have gone through the text file and separated out all the lines of text I need to separate the values. I used an if statement to cycle through the array of lines and then separated the line into a new array using an apostrophe as the separator.

I know the first string separated will always contain the asset type therefore I can create a series of if statements to identify the type of asset the line has data for by comparing the string to asset names. For example If 'assetname == "Wall"' returns true I know the line contains data for a wall and can handle the data accordingly. 

Lastly, all I need to do within these if statements is instantiate a new prefab populated with the data read from the file. Assuming all is as expected when a user saves their game all data will be written to a text file and read back in at a later date. The game should appear to be exactly the same therefore fulfilling the effect the game state has been saved.

Friday, 15 January 2016

Customer

Brief Description

In this section of the blog I have many things to explain so you'll fully comprehend and understand what I've achieved therefore I hope you'll be patient as I will explain everything in detail eventually. 

My customers walk using a node based, four directional, algorithm I developed. The customer spawns at a randomly placed door in the scene and will quickly identify an unoccupied table. The customer will travel t'wards the table using the shortest possible route.

If there aren't any unoccupied tables the customer will wander randomly about the restaurant for a duration of thirty seconds before leaving. If a table becomes unoccupied during this thirty second period, the customers timer will be paused and they'll travel t'wards the table. If, unluckily, the table becomes occupied as they're travelling t'wards it the customers timer will resume and they'll wander randomly once again.

The customer doesn't contain advanced AI allowing them to navigate around objects. This is a necessity I hope to implement soon and will keep the blog updated of my attempts. 

Once the customer occupies a table they'll order, at random, a meal from the menu and then wait for a period of thirty seconds to be served. If they aren't served within thirty seconds they'll leave. If they're served within thirty seconds they'll wait for their meal, eat the meal, pay and then leave.

Node based Pathfinding

I mentioned in my opening description that my customer's artificial intelligence follows a node based, four directional, pathfinding algorithm. This means I've placed nodes throughout the game, one per tile, invisible to the user which contains positional data essential for my code.

'Four directional' pathfinding means the customer can only move in four directions. The customer can move forward, backwards, left and right. I wrote my code this way to provide a tile based looking game as I want my game to appear two dimensional.

My code works by reading in the active waypoints, finding an unoccupied table, locating the node which shares the same X and Z position and finally setting the waypoint as the target. 

Next, I find the nodes right, left, forward and backwards from the customers position and identify the closest node to the target. The customer travels t'wards the closest node and repeats this step until the target is found.

At the beginning of every cycle I check the target's occupancy. If the target is occupied the customer locates a random active waypoint to travel t'wards. If thirty seconds have passed without sitting at an unoccupied table or the customer has finished their meal the code sets the target to a random door and leaves. Once at the door the customers are destroyed.

Waiting for a Meal

When a customer sits at the table it first changes the tables occupancy to occupied. It achieves this by finding the script opponent attached to the table and then sets it's occupied value to true.

Next, to alert waiters they're ready to order the customer has to create and add an order to the list of orders. The order will contain a meal, selected at random from the database of unlocked recipes, and the table's name. The customer will then start it's waiting graphic which shows the user the time remaining before they leave. 
Figure 1a
 Figure 1b

Figure 1b is displayed just above the customer's head to provide the user with visual representation of the customer's waiting time. I'm hoping the user will use this to note the average waiter response time and decide whether they need more staff. 

The green bar is placed over the red and the width is decreased one thirtieth of it's size per second. Once the bar reaches zero they leave and the graphic is removed. If the waiter takes the order before the timer reaches zero, the timer is stopped and the graphic is removed.

Eating a Meal

Eating the meal is essentially a lot like waiting for a meal however the duration in which it takes to consume a meal is dependant on the meal itself. Each recipe, when added to the recipe database, requires a time in which it takes the customer to consume. I take the time from the recipe and apply it to my eating graphic.

Figure 2a
 Figure 2b

Once the timer on the eating graphic reaches zero the cost of the meal, also added to the recipe when added to the recipe database, is added to the users coin count. 

User Interface

Brief Description

My user interface is the most intricate, convulsed and essential part of my entire project. My user interface class is the core class which branches to other classes in order to seamlessly like the user input with all the features of my game.

Game Icons

I have created four user icons which are drawn onto the in game camera which allows the user to navigate through my game.

Store Icons
Figure 1a        Figure 1b

I chose to use a shopping cart on my store icon as a shopping cart is synonymous with a store. I hoped that users would see the icon and instantly recognise that is they pressed this icon they would be transported to the store section of my game where they could buy new objects and change the appearance of the restaurant. The two icons are 64 pixels by 64 pixels reiterating the sentiments I explained in my opening introduction that all my textures will be a multiple of 16 pixels in order to aid in scalable textures.

You'll notice there are two textures that serve the same function. In order to feed back to the user that they are able to click on the icon I have created another texture, figure 1b, to act as a 'mouse over' texture. I achieve the effect by adding rectangular collision box around the icon and then checking whether the cursors position is within the rectangle. If the cursor is within the dimensions of the icons rectangle I swap the textures around to give instant recognition to the user this icon is a button which can be interacted with. 

In my user interface I have used a enum variable, this is a made up variable which allows the coder to create multiple properties the variable can use, to separate my code into different game states. If the game is in a certain state it will only execute the code I have determined to be executed. If the store icon is pressed the game state is set to store which executes store only code and stops executing the code from the previous state.

Options Icons
Figure 2a        Figure 2b

I struggled with what to come up with for the options icon initially. The obvious choice would be to do a cog as that is the symbol most games use and therefore it's become synonymous with the game options however I didn't just want to plain copy other games. The icon I have used is three sliders which I hope people will recognise from options screens where you change sound, music etc... using sliders usually with zero being at the bottom and one hundred percent at the top.

The icons act in the exact same way as the previous icons except that once the button is pressed it will take you to the options screen. I failed to mention above that the store page is a full screen page however when the options screen is initiated it will only take up about half the screen in the centre. I wanted to show the user that the restaurant is still running in the background whilst they are perusing the game options. 

Staff Icons
Figure 3a        Figure 3b

My initial thought when creating an icon to represent the staff screen was to have multiple humanoid looking people on it however as the game is generally cartoony in appearance I didn't want to go the realistic approach therefore I came up with the textures in figure 3. My hope was that when people view the icon they would see the three humanoid looking and assume it would have something to do with people. I hope people would make the leap that it's the icon used to navigate to the staff section of my game where you manage the restaurants employees.

Exit Icons
Figure 4a         Figure 4b

The last game icons are the exit button icons which are used across multiple game states to instantly navigate back to the default in game scene. As the button is used to leave alternative game states this button is not added to the in game scene with the other three icons as it would serve no purpose. The icons design comes directly from the icon used on all major operating systems to close down an application therefore I hoped used would know that clicking this icon would instantly close down any sub screen they are on and navigate directly back to the default in game screen.

Restaurant Open/Closed
Figure 5a                      Figure 5b

In order for my game to work without errors I really need to prevent the users removing/adding game objects whilst the restaurant is open as the AI will be affected by the changes. To aid with this I have added a restaurant open/close boolean which prevents the staff and store icons being pressed while the restaurant is open. 

For example if a staff member was in the middle of fulfilling an order and then the user wanted to fire that staff member the order would be left unfulfilled, the customer forever waiting for the order to be completed and no other waiters could fulfil it as my game code would say it's already being handled.

With the restaurant open state the user would have to close the restaurant to nullify all orders, remove all staff and remove all customers. The user would then be able to add or remove staff which will then be reflected when the staff are loaded back in when the restaurant is opened again. 

The signs act much like the icon buttons as they have a rectangular collision box attached and if the cursor is within the collision rectangle the sign will switch to the alternate texture and a boolean containing the true/false value of the restaurants open status will switch too.

Appearance

Here is how the default in game screen will look like...
Figure 6

Basic Design

Brief Description

In this post I am going to be showing and explaining the basic level design and theory behind how my game will run and look.

Figure 1

As you can hopefully see from figure 1 my starter restaurant will be comprised of multiple square tiles which from to look like a singular floor and two sides of the square floor is covered with walls. Each wall and tile has a script attached in order to allow each game object to react to user interactions. 

The starter restaurant is 7 tiles wide by 7 tiles high giving the restaurant a floor space of 49 tiles. I will eventually add a restaurant upgrade that expands the restaurant to 10 tiles by 10 tiles which will be accessible when the user has reached a certain level.

Lastly, I want to bring to your attention the camera perspective which isn't a typical 3D view-port, as displayed on the in game camera view in the bottom right hand corner of figure 1, as I have changes the camera from perspective to orthographic. The effect this achieves is that the game objects will appear two dimension when in fact they are three dimensional.

How it works

Tiles

The tiles will have dual purpose in my game the first of which is that the user will be able to manipulate the tiles attached renderer to change it's appearance therefore allowing the user to change the floors appearance. 

Secondly, the tiles will be used to display a arrow texture for object placing to show the user the way the game object will face and on what tile it'll be placed. If there is already an object in the same position as a tile the user will not be able to place another on the tile.

Walls

The walls in my game also have a dual purpose. The walls, like my tiles, will be able to be textured by the user to allow the appearance of wallpapering the walls. This is done by manipulating the renderer to add a texture, when the user selects a certain wallpaper, to change to that texture and keep that texture if the user clicks. If the user doesn't wish to keep the texture it will revert to it's previous appearance once the user exits hovering over the wall.

Secondly, the user will be able to place doors in place of the wall by hovering over it, which will change the appearance to green or red depending on whether the door can be placed, and left clicking if the option has been selected. The doors will be used as the spawn point of the customers.

Introduction

Brief Introduction

My name is Daniel Jolley and I'm an aspiring Games Developer. I have previously studied Computer Games Development at the University of Plymouth in the UK. During the past 6-8 months I have spent considerable time working on this game which I am happy to share with the public. 

Firstly, I must mention that this game is only a prototype with basic features and  requires additional work until I have created something I would consider to be a complete game. Creating this game has been a solo project and all game assets, textures and code have been created entirely by myself unless specifically stated otherwise.

Game Specifications

Hardware and Software

I have opted to use the free source Unity engine which is easily obtained online and provides many great assets and packages to aid in helping developers in addition to a fabulous asset store which contains a large catalogue of incredibly useful, user made, game assets and code. 

My 3D modelling tool of preference is Blender 3D. I have opted for this software really down to previous experience and familiarity with the software. The software, once again, is a free-of-charge software easily downloaded from their online website.

In order to create my textures for the game I have opted to use another free source software tool called GIMP 2.0. I have chosen this once again down to experience and familiarity with the tool and I have found that I enjoy using this tool much more than Adobe Photoshop.

SOUND TOOL TBD...

When creating games I always leave the sound creation until the end and therefore haven't yet decided on a sound software creation tool as I haven't began to implement sound.

Target Resolution

My main cause for concern when deciding a target resolution is that there are so many varied screen resolution sizes out there meaning upscaling and downscaling will be a factor. This means I needed to decide on a base resolution which if scaled will still allow my game textures to maintain their aspect ratio. I opted for the 1280 x 720 screen resolution which has an aspect ratio of 16:9 which a majority of the screen resolution share. 

As my textures will always be a multiple of 16 pixels in size I can easily find a percentage of the screens width and height which are equal to 16 pixels, 1280 and 720 are multiples of 16 pixels, and then use that percentage to add in pixel accurate textures which upscale and downscale perfectly on 16:9 aspect ratio screen sizes.

Target Platform

My initial target platform are pc's therefore I will be creating my user input features to work with mouse clicks. I am looking to implement touch screen features also so that in the future I would be able to release this game onto iOS and Android devices.

Network Features

TBD...

Given my base knowledge of networking coming from creating restful applications at university I am not entirely convinced I can create a secure networking feature which means I am reluctant in committing to adding this feature for my game.