Code snippets |
Top Previous Next |
Morrowing
Another month, another Morrowing chunk for you. This time I have added an inventory and a lightning weapon, to name only the most important features. I had to use some of the models and the graphics from my Aum18 inventory but the menu code is completely different and has many more features.
panel main_up_pan // the upper part of the main panel
panel main_slot1_pan // the first inventory slot
panel main_slot2_pan // the second inventory slot
panel main_slot3_pan // the third inventory slot
panel main_slot4_pan // the fourth inventory slot
panel main_down_pan // the lower part of the main panel
The inventory panel is composed of 6 small panels that blend together nicely. I chose to do that because each slot is a separate panel that can be clicked and I needed separated "on_click" functions for each slot.
panel shield_pan // displays the shield on the panel
panel mace_pan // displays the mace on the panel
panel armor_pan // displays the armor on the panel
panel flare_pan // displays the flare on the panel
These definitions are used for the pictures of the items (look at the armor and the shield in the picture above to see an example).
starter init_player()
This starter function initializes the player, setting its strength, its experience (increases when the player kills skeletons) and its mana. The player needs at least 70 mana points to use its lightning attack; you can see that the mana increases slowly and is limited to 74 (the width of the cutout bitmap on the hud).
function place_item1()
This function runs when the player clicks the first panel slot; it checks if the player has picked an object or not (mouse_map = pointer_pcx) and if the slot is occupied or not (item1_placed = 1). If mouse_map is shield_pcx, the player has picked up the shield, so we display the shield_pan panel on the inventory panel. The things repeat for the rest of the items, which are always placed at x = 960, y = 165 for the first slot. We make the correct panel visible (I'm using a panel pointer named item_ptr for that), we occupy the slot and then we restore the initial bitmap that was used for the mouse pointer (arrow_pcx). Finally, we hide the mouse pointer because we aren't interested in seeing it for now. I'm using similar functions for the rest of the slots so I won't discuss them.
action shield_init
function pick_shield()
The shield is sensitive to clicking, its event function being named "pick_shield". We wait until the player is created in the level, and then we animate the shield using its "idle" animation until the player picks it up. As soon as that happens we remove the shield model. The event function doesn't do anything if the mouse pointer that has clicked the shield isn't the standard "pointer_pcx" arrow; this way, we can't click more than an item without placing it on the inventory panel first.
The shield can't be picked up if the player isn't close than 100 quants to it. If the player has got the shield, we set player.shield (just another name for player.skill1) to 1, we show the mouse pointer and then we replace the arrow pointer with a picture of the shield. Our place_item1()... place_item4() functions will do the rest of the job.
function use_shield()
The function above will run when the player clicks the inventory panel with a pointer_pcx mouse pointer. We remove the shield from the panel, and then we set the corresponding item_placed variable to zero; this frees the slot, allowing the player to place another item there (when it finds a new item). The shield must be created and attached to the player, and that's exactly what the following function does:
function attach_shield()
The shield model is passable and exists for as long as the player exists in the level. The shield uses the same position, angle and animation frames with the player. The functions for the rest of the items are similar; the only significant difference appears in the function below:
function attach_flare()
This function attaches a flare sprite to the top of the mace, telling the player that it can use the lightning attack (if it has got enough mana points). We use a loop to get the coordinates for the tip of the mace (vertex 41) every frame, and then we set the proper position for the flare. Some of the older Morrowing scripts went through some changes; let's examine the major additions to the player.wdl file:
if ((mace_on == 1) && (mouse_mode == 0) && (mouse_left == 1))
This piece of code was added inside player's while loop. If we have got and have used the mace, and the mouse pointer is hidden and we press the left mouse button, we trace 100 quants in front of the player, ignoring its mace and shield (which are passable models) and if we have hit an entity (not a wall) we play a fire_wav sound and we decrease its health by 35 points. Finally, we play the"attack" animation.
// got the mace and the pointer is hidden and the RMB is pressed and the flare is used and the player has enough mana?
If the player has got the mace and has used it (mace_on = 1) and the right mouse button was pressed and the flare (lightning) was picked up and used and the player has enough mana, we set player.mana to zero, we play a flare_wav sound, and then we trace 500 quants in front of the player. We generate the lightning effect using the tip of the mace as the starting point and the point that was computed 500 quants in front of the player as the destination point. If we hit something, we decrease 100 health points from its health, and this kills any skeleton. We set the last "attack" frame for the player and we make sure that the lightning / tracing lasts for 1-2 seconds, until skill48 grows above 100.
The lightning effect can be found inside the particles.wdl file; it's an improved version of the old particle_line function. The code creates several lightning segments of 100 quants each, placing a particle every 5 quants. The particles live for a single frame; feel free to play with the numbers until you get an even better looking effect.
Have fun! |