|Top Previous Next|
Many of you have requested some menu code so I have decided to create a simple, fully functional menu example. This menu has the following buttons: New Game, Sound Volume, Video Resolution, Quit. I know that you might need Save, Load, etc but all these functions are implemented in the template menu and you can add them (and their respective buttons) to my code.
I wanted to give you the chance to play with the menu in a standalone project so I have created a simple "game" - a level where you get to stare at some eye-check sprite. If you're confused, please think what must have been inside my head when I have decided to pick up an example like that! The good thing is that you can start the "game", adjust the sound level (and hear the difference), change the resolution (and see it being changed) and quit the game.
The game starts running at 800x600 pixels, in 16 bit mode.
var video_mode = 7; // 800x600
First of all, we need to define some bmaps; we are using two bitmaps for every button and a single bitmap for every panel. The names are pretty suggestive so I won't make any comments here.
bmap main_pcx = <main.pcx>;
I'm using two levels: introlev.wmb is a dummy level, a simple hollowed block with some colored lights inside it and level1.wmb is the first (and only) game level.
string introlevel_wmb = <introlev.wmb>;
The panel definitions are similar; main_pan has four buttons on it; sound_pan and video_pan don't have buttons, but sliders.
If you don't understand what I'm doing with the buttons and sliders, please read their definitions in the manual and compare what I did with the manual.
Let's take a look at:
If you're starting a new project from scratch Esc will exit the engine. We want to use Esc to bring the main menu on, so the first line in main deactivates the built-in (exit) Esc function. We load the dummy level, we wait two frames to make sure that it has loaded and then we run the game_init() function (more on that a little later). Main_pan is made visible, mainmenu_mouse() give us a cursor to play with, the camera is moved in the origin and starts to rotate by changing its pan, tilt and roll angles.
function mainmenu_mouse ()
The first lines in mainmenu_mouse() show the cursor and change it to our maincursor_pcx. We can move the cursor as long as the game hasn't started using the predefined pointer.x and pointer.y.
Let's get back to:
Every panel can be moved using its pos_x and pos_y parameters. We have provided some initial pos_x and pos_y values to our panels, but the panel needs to change its position to make sure that it is positioned in the center of the screen at any resolution. Take a look at the picture below to see how this works:
if (key_esc == 1)
If we press Escape, we have to wait for its release (while loop) because we don't want 50-100 menu changes a second (the changes triggered by Esc would be executed every frame). If main_pan is visible, we remove it and we hide the cursor. If main_pan isn't visible we check to see if sound_pan or video_pan are visible. If none of them is visible (there's no panel on screen) we make main_pan visible, we stop the game and we show the cursor. If sound_pan is visible, we hide it and show main_pan; if video_pan is visible, we do the same thing with it BUT if the resolution has changed we switch the video mode. Our video slider can deliver non-integer values, so we use int (video_value).
I'm using a small action: eye_checker to keep the sprite oriented and a small function mouse_over() to play a sound when the mouse is over one of the buttons:
The long awaited moment has finally arrived: we get to see the functions associated to the menu buttons:
Function start_game() will be executed every time we press the New button. This function hides the main_pan, sets game_started to 1, shows the cursor, loads the game level and then sets the camera.z position and tilt angle.
Function set_volume() hides main_pan and shows sound_pan. If you take a look at the hslider definition in sound_pan, you will see that by changing the slider position we modify sound_vol; this is a predefined variable that changes the volume for all the wav sounds in the game. Move the slider, press Esc and you will hear the difference when you move the mouse over the main_pan buttons.
Function set_resolution hides main_pan and shows video_pan. When you move the slider, video_value is changing and when you press Esc the resolution will be changed - see the code in game_init()
This function will exit the engine right away.
Vec_for_mesh and vec_to_mesh were some of Aum3's hot features. Take a look at this code that generates falling bombs (ok, I've used a rocket model) that create craters when they impact with a terrain mesh.
I have created another standalone project; the example runs at 800x600x16bit, Vertex_dist is an array that will hold the distance between every vertex and hit_coords
var video_mode = 7; // 800x600
Function main loads our test level and sets a weird camera position (if you don't like this position, press zero and move the camera where you want it)
I have added a small model in Wed; its associated action generates random falling bombs:
The first line in this action prevents the bomb from getting stuck inside its creator. A new bomb is generated every 2.5 seconds, in a 400x400 quants area. Every bomb moves according to its move_bomb function:
You can see that the bomb is sensitive to entities and level blocks, has a negative z speed and moves until its explode_me event is triggered:
We need to wait a little to be able to store the impact coords in hit_coords. We play an explosion sound and we remove the bomb.
The terrian does all the job when its create_crate event is triggered:
First of all, we store the impact coords before the bomb disappears
We set the vertex_dist index to 1 and skill2 to a huge distance; this distance will be much smaller for the vertices that are closer and closer to the impact point.
while (my.skill1 < 1090) // vertices 1...1089 for a 33x33 grid
My terrain has a 33x33 grid with 1089 vertices; my.skill1 will be set to 1, 2, 3, ... 1089 in the while loop above. Vec_for_mesh sets temp to vertex 1, 2, 3, ... and we store the distance between vertex1 and hit_coords in vertex_dist, the distance between vertex2 and hit_coords in vertex_dist and so on, like in the picture below:
I couldn't draw blue arrows for all the vertices, but it looks like vertex18 is the winner in my picture, so the distance between vertex18 and hit_coords will be stored in skill2.
Now we need to find out the index for the winner, because this index will be passed to function deform(vertex_number).
my.skill1 = 1;
This time we are searching for the smallest vec_dist stored in vertex_dist and when we find it we store the index in skill3 - this is the vertex that will be moved inwards. It is clear that these while loops can be optimized but they run so fast (without wait) and it isn't necessary to do it.
The final function is:
We set vertex_number as an argument for vec_for_mesh, so temp will be set to vertex_number's coords. Temp will be scaled to 70% of its value and then vec_to_mesh will deform the vertex that was closest to hit_coords.