-
Notifications
You must be signed in to change notification settings - Fork 0
2. Code Basics & Gameplay
startMenu.gd holds the main framework of the title screen/start menu. It contains mainly node signal functions that react upon being pressed/changed/toggled when pressing the buttons on the start menu.
- Audio: There are 3 different audio buses: master, music, and sound effects (SFX). Each are a slider in the options menu that can be changed to adjust a specific sound’s volume. Each sound can also be toggled to mute that sound entirely
- Quit Button: When pressed the game will be closed
-
New Game Button: When pressed, the game will transition to a to the game screen scene (
start_map.tscn) - Load Game Button: When pressed, the player will be prompt with a window to choose from save files and then when a file is selected there will be a transition to the game screen loaded with the save file contents.
Upon clicking the options button, the user can edit the volume or choose to fullscreen the game as options.
LoadGamePopup.gd holds all the content for handling the opening the window prompt for choosing files and then transitioning to the game scene.
Tiles represent each individual square on the SimCoast map. Tiles have a large number of member variables to keep track of their status (such as zoning, buildings, utilities, height, water, etc…). The following are the current member variables:
- i and j - position of the tile on the map. (0,0) is the top-most tile on the map. i increases towards the right side of the map, whereas j increases towards the left.
- baseHeight - the land height of the tile
- waterHeight - the height of the water on the tile
- base - indicates the type of base of the tile (dirt, sand, ocean, rock)
- zone - indicates the zoning type on the tile (or lack thereof)
- inf - indicates what is on the tile (nothing, road, building, trees, etc…)
- cube - a Area2D node that represents the “physical” tile. See Tile Cube for more details
-
data - an array of 5 values that keep track of building/population status
- [0] - number of houses present on the tile
- [1] - maximum number of houses the tile can hold
- [2] - number of people living on a tile
- [3] - maximum number of people the tile can hold
-
[4] - the status of the tile
- 0 - unoccupied
- 1 - occupied
- 2 - damaged
- 3 - severe damage
- 4 - abandoned
- utilities - boolean value that indicates whether the tile has utilities
Tile objects also have a large number of member functions. These functions are self-explanatory as they are “getters” and “setters” and thus need no explanation.
Currently, the tiles themselves are drawn in godot using their native draw_polygon() and draw_polyline() functions. The buildings and structures on top of the tile are images that can be found in the assets/building_assets/2d assets. All the drawing and updating of the assets is done in TileCube.gd.
Tile Cube represents one Tile as it appears on the screen as is responsible for drawing the Tile to the screen and any assets related to that tile. The most important functions are:
This function draws the tile (base and water) and any objects on the tile to the screen. If the tile has some building, that will be in the buildingSprite variable and will be drawn. Roads and Zones have multiple assets to be drawn at a time which will be stored in listOfBuildings.
This function figures out where and what to draw based on the stored tile’s information. Each tile cube has the i and j position of the tile they relate to that is stored in Global.tileMap. It retrieves whether that tile is marked as having some infrastructure on it and if so, where the images position should be and which image to use. It does not actually draw the image and just stores it in the correct variable. It is called at the beginning of _draw().
Tiles can be zoned/marked with a certain type of zone, which decide what buildings will be built on it.
-
Residential: Residential zones provide housing for people.
- Light Residential: Up to 4 different single-family homes can be built in this zone.
- Heavy Residential: An apartment complex building can be built here.
-
Commercial: Commercial zones will have stores selling goods and gaining profit.
- Light Commercial: Small shops/stores and restaurants will be built here
- Heavy Commercial: Department stores and malls containing a variety of stores providing entertainment and goods
Roads are essential tiles that provide both residential and commercial zones with water/electricity from utilities plants. The transfer of utilities to zones only works if roads are adjacent tiles to the zoned tile.
Service buildings are tiles that provide an essential service to the city and its residents.
- Utilities Plants: generate electricity and water, which can be transferred to residents by Road tiles.
Decorative tiles are cosmetic tiles that the player can use to provide more aesthetic to their city.
- Parks
- Beach rocks
- Beach grass
Global.gd is a singleton that is one of the first loaded singletons. Like other singletons, Global.gd can be accessed with any other scripts. Global.gd contains constants for key elements of SimCoast gameplay:
- MAP_EDGE_BUFFER - sets how far the camera can go beyond the map
- TILE_WIDTH - how wide tiles are for drawing purposes
- TILE_HEIGHT - how tall tiles are for drawing purposes
- MAX_HEIGHT - maximum vertical height for any tile
- MIN_MAP_SIZE - minimum map size
- MAX_MAP_SIZE - maximum map size
- MAX_CONNECTION_HEIGHT - largest amount of height difference allowed to consider tiles connected to one another.
- TICK_DELAY - time between ticks
Global.gd also initializes and stores the TileMap for SimCoast. This is the representation of the entire map. TileMap is a 2D array whose entries are Tiles. As Global.gd is a singleton, this TileMap can be accessed in other scripts to update the game states, draw it to the screen, etc.
Other values stored in Global.gd include the current mapTool selected (see User Input), the current oceanHeight, and values for drawing and rotating the map.
Start_Map.gd is the heart of the main gameplay in SimCoast. Start_Map.gd handles several things:
- Initializing the camera in initCamera()
- Handling user input in _unhandled_input(event) (see User Input section for details)
- Updating the game state and graphics at a set tick rate (see Updating Graphics and Updating Game State)
User input is handled in _unhandled_input(event) in start_map.gd.
- If the input is a click, the Tile clicked on is selected and depending on the current map tool selected, the game state is updated appropriately.
- If the input is a valid key press, the corresponding action is taken (for instance, pressing “z” will calculate flood and erosion damage.
- If the input is simply mouse motion, the tile hovered over will have its information displayed on the HUD.
SimCoast works off of ticks that occur at a fixed rate. These ticks are kept track of in _process(delta) in start_map.gd. Essentially, there is a timer (TICK_DELAY) that reduces with the time passed. When this timer reaches 0, a tick occurs, updates occur (update_game_state(), update_graphics()), and the timer is reset. If the game is paused, the timer does not decrement. If fast forward is enabled, the tick delay is reduced causing faster ticks.
Just like in other video games, SimCoast uses a tick rate system to coordinate the timing of game state and graphics updating. If we didn’t implement this, the game would become throttled by redundant calls to update game state/graphics on every iteration of start_map.gd’s process() function (the main game loop).
The tick delay is set as a constant within Global.gd. As of writing this documentation, this rate is set to one tick every 0.05 seconds, or roughly 20 ticks-per-second. After every iteration of the process() function, the current tick delay is decremented by delta (the time elapsed since the previous iteration). Once this value becomes zero, which corresponds to 0.05 seconds, the game’s state and graphics are updated.
While the game is running, players can pause the game freezing time and all active updates during the game. Players can also speed up the game to pass time and increase the rate at which active updates to the game are done.
update_game_state() is a function called in _process(delta) in start_map.gd. This function calls functions from various other singletons that specifically focus on updating one aspect of the game state. The following are the current update functions:
- UpdateWaves.update_waves() - calls the update_waves() function of the singleton UpdateWaves. This function slowly increases and decreases the water level of the ocean, leading to waves being generated.
- UpdatePopulation.update_population() - calls the update_population() function of the singleton UpdateWaves. This function determines whether buildings will be built on zoned land and whether or not people will move in/out of said buildings.
- Econ.collectTaxes() - calls the collectTaxes() function of the singleton Econ. This function determines the amount of money the player will collect in taxes from zoned tiles and adds it to their funds.
update_graphics() is a function called in _process(delta) in start_map.gd. This function simply calls UpdateGraphics.update_graphics(). This function calls update() on the cube member variable of every Tile in the TileMap. This triggers every Tile to be redrawn. As the graphics update occurs after the game state update, the tiles are drawn to reflect any new changes.