Difference between revisions of "Code How To's"
From TACWiki
(→Create a New Minigame) |
|||
Line 1: | Line 1: | ||
This page provides tutorials for some of the most common actions a programmer may wish to implement. | This page provides tutorials for some of the most common actions a programmer may wish to implement. | ||
+ | |||
+ | ==Coding Conventions== | ||
+ | |||
+ | * Place the name of the file, authors, and copyright info at the top of each file | ||
+ | * All variables are lowerCamelCase | ||
+ | * All functions and properties (sometimes called "smart fields") are UpperCamelCase | ||
+ | * Member variables are preceded with "m" | ||
+ | * This code is intended to be viewed by many people who do not have strong programming backgrounds, therefore documentation is '''very''' important | ||
+ | ** Document the purpose of each class's member variables | ||
+ | ** Create a summary documentation block for every function | ||
+ | *** This can be done by placing the cursor above a function definition and typing "///" | ||
+ | *** This block shows up in Intellisense when writing that function in code | ||
+ | ** Add a summary to any properties that are possibly confusing or not straightforward | ||
+ | ** Document the purpose of each block of code unless the block is absolutely self-explanatory | ||
+ | ** See MeteorMadness.cs for an example of the appropriate level of documentation | ||
==Tweak Game Parameters== | ==Tweak Game Parameters== |
Revision as of 13:15, 10 October 2007
This page provides tutorials for some of the most common actions a programmer may wish to implement.
Contents
- 1 Coding Conventions
- 2 Tweak Game Parameters
- 3 Create a New Minigame
- 4 Display a Static Image
- 5 Display an Animation
- 6 Create a Game Object
- 7 Get User Input
- 8 Manage a Heads Up Display
- 9 Play a Sound
- 10 Display Text
- 11 Use a Particle Effect
- 12 Create a New Particle Effect
- 13 Keep Score and Give Resources to the Simulator Mode
- 14 Write to the Experimental Log
- 15 Output Data Through the Parallel Port
- 16 Use the Game Script
Coding Conventions
- Place the name of the file, authors, and copyright info at the top of each file
- All variables are lowerCamelCase
- All functions and properties (sometimes called "smart fields") are UpperCamelCase
- Member variables are preceded with "m"
- This code is intended to be viewed by many people who do not have strong programming backgrounds, therefore documentation is very important
- Document the purpose of each class's member variables
- Create a summary documentation block for every function
- This can be done by placing the cursor above a function definition and typing "///"
- This block shows up in Intellisense when writing that function in code
- Add a summary to any properties that are possibly confusing or not straightforward
- Document the purpose of each block of code unless the block is absolutely self-explanatory
- See MeteorMadness.cs for an example of the appropriate level of documentation
Tweak Game Parameters
It is often helpful to adjust variables during runtime. This can be accomplished by adding them to the game's cheat menu, which can be activated in game by pressing ctrl+alt+c.
- Add the variable to the Game Manager
- Declare a new member variable in ColonyGameManager next to the other tweakable variables
- Create a getter and setter for that variable
- Add the variable to the Control Panel
- Open ColonyControlPanel.cs[Design]
- If the minigame using this variable does not yet have a group, do the following
- Create a new radio button in the upper left corner in the same manner as the other games
- Create a new group anywhere in the right area (only the left most 250 px or so are visible in the game)
- Add a new if block to butUpdate_Click(object sender, EventArgs e)
- Double click on the form to bring up the code
- If this creates a new empty function, just delete the new function
- Move the group out of view in hidePanels()
- Double-click the new radio button to create the radNewminingame_CheckedChanged(...) function
- Fill this function with code similar to radColony_CheckedChanged(...)
- Add a new if block in UpdateMode(...)
- Add a label and text box to the minigame's group in the same manner as the existing games
- Add the line ColonyBaseApplication.GameManager.NewVariable = ...; to butUpdate_Click(...)
- Add the line txtNewVariable.Text = "" + ColonyBaseApplication.GameManager.NewVaraible; to ControlPanel_Load(...)
- Use the variable in the minigame
- The value can be accessed with ColonyBaseApplication.GameManager.NewVariable
- Set the default value for the variable in the config file
- The config file is not yet implemented, so for now just the default value in ColonyGameManager
Create a New Minigame
- Declare the class
- Create a new folder under ColonySimulator
- Create a new file in that folder named NewMinigame.cs where NewGame is the name of the new minigame
- Place all new minigame-specific classes in that folder
- Have the class derive from MiniGame
- class NewGame : MiniGame {...}
- Implement the Constructor
- Initialize game variables
- Declare a ScoreCard (see Scoring tutorial)
- Log the GameBegin code (see the Logging tutorial)
- Implement Update()
- public override void Update(ContentManager content){...}
- Do not assume a constant call rate
- This function will almost always be called 60 times per second, but that rate may vary depending on CPU load
- At the beginning of each cycle, get the time since the last cycle like this:
- float dt = ColonyBaseApplication.GameManager.ElapsedSeconds;
- DO NOT use XNA.Framework.GameTime.ElapsedGameTime as tAC_Engine implements update timing differently than a standard XNA application!
- Build the time into all movement and timers, for example:
- myEntity.LifeSpan -= dt;
- myEntity.Position += myEntity.Velocity * dt;
- Implement Draw()
- public override void Draw(SpriteBatch spriteBatch){...}
- Loop through all the game's entities and call their Draw() functions
- Consider using a generalized render-queue such as GenericWorldSpace (not-yet-implemented as of Oct 2007)
- Create a new game mode
- Add a new entry to ColonyBaseApplication.Modes = { ModeSelector, ColonySim, ...}
- Add the appropriate case to ColonyBaseApplication.SwitchMode(...)
- Add the appropriate case to ColonyBaseApplication.Update(...)
- Add the appropriate case to ColonyBaseApplication.Draw(...)
- As of Oct 2007, it is necessary to add a new case to ModeSelector.Update and .Draw, but that will eventually not be necessary
- End the game
- Flush the particle system (see the particles tutorial) if any emitters were created
- Log the GameEndSuccess/GameEndFailure code
- Flush the log (forces it to write any cached codes to disk)
- Show the score
- Switch the mode to ColonySim
- ColonyBaseApplication.SwitchMode(ColonyBaseApplication.Modes.ColonySim);