|Top Previous Next|
The clock code below allows you to set the alarm time using hours and minutes; the alarm will yell its annoying sound for a minute.
The main panel displays the clock picture and the four buttons (arrows) used to set the alarm time: + - hours and + - minutes.
panel main_pan // main panel
button = 260, 260, arrowup_pcx, arrowup_pcx, arrowup_pcx, null, hours_up, null;
flags = overlay, refresh, d3d, visible;
Clock_text shows the current time like this: hh:mm ss and alarm_text show the alarm time like this hh:mm. You know that h comes from hours, m = minutes, s = seconds, right?
The most important part of the code is included in main:
This main function isn't loading any level; we set the background color to rgb = 000, we use mouse_pcx for the mouse pointer and we show the cursor. The first 2 lines inside the while loop make sure that we can move the mouse on the panel.
We read sys_hours, we convert it from number to string and we put it in clock_string. We add ":" to the string; so far we have obtained "hh:". If sys_minutes < 10, the clock would display something like this: "23: 2" so we add a zero after ":" to make the clock show something like this: "23:02". Now we can read sys_minutes, we convert it to string, we add the minutes to clock_string and then we leave a space " " before adding the seconds. If sys_seconds < 10 we add another zero to the string and then we read sys_seconds, we convert it to string and then we add it to clock_string, obtaining something like this: "23:02 45".
Let's see the code that takes care of the alarm:
str_for_num(alarm_string, alarm_hours); // convert alarm_hours to string
if ((alarm_hours == sys_hours) && (alarm_minutes == sys_minutes)) // sound the alarm
I have set these (default) values for alarm_hours and alarm_minutes: 12 and 30. We convert alarm_hours to string, we add ":", we add a zero if alarm_minutes < 10, we convert alarm_minutes to string and then we add it to alarm_string. If alarm_hours (set by us) = sys_hours (the pc clock) and alarm_minutes (set by us) = sys_minutes (the pc clock) the alarm will start and it will run for a minute, until alarm_minutes != sys_minutes.
Function start_alarm is easy:
We don't allow multiple instances of this function to run and we play a sound in a loop, waiting for it to finish before starting to play it again.
The rest of the code is used to set the alarm time when we press one of the buttons (arrows):
There's nothing special here; if you click the hours up arrow, alarm_hours will be incremented until it reaches 23. The rest of the functions are similar.
If you are looking for a simple method to make your models react in different ways depending on the bullet impact area, you came to the right place. It is quite normal to have an enemy soldier dead if we have managed to curl its hair with a head shot, right? On the other hand, it isn't normal to kill an enemy by shooting its leg once.
I'm going to reveal my method - it is fast and it works flawlessly. I like to call it the (put something here) method.
The idea is simple: we set some key vertices on our enemy model and then we get their height in a while loop. When a bullet hits the model, we get the height of the bullet and then we compare this value with the height of every key vertex. The closest vertex will trigger its specific animation. The code is as simple as it sounds (is this good news or bad news?)
I will use a text to display a string on the screen and two panels: one for the crosshair and one for the key vertices and for bullet's coordinate.
Function main disables the "D" (debug panel) key because we will use WSAD to move the player and then it loads the level:
This is a standalone project so we need an action for the player:
We don't need to see the player because we are in 1st person mode; the crosshair panel is placed in the center of the screen. The camera is placed at player's position and changes its pan and tilt by moving the mouse. The player inherits its pan and tilt from the camera and moves using the keys WSAD. The movement code includes gravity; we trace 1000 quants below player's feet and then we move the player with its feet on the ground. If we press the left mouse button (LMB) we create a bullet, a sound is played and then we wait until the mouse button is released because we want to disable autofire.
Let's see the code associated to the enemy:
The enemy is sensitive to impact, shoot or other entities; if one of these events happens, it will run function compute_shot_pos. As long as the enemy hasn't been shot (skill5 = 0), it will stand still. I have used a single frame for "idle" but you can use a real "idle" animation here. I chose 3 key vertices for head, belly and leg using Med but you can choose as many as you want; their z coords are stored in head_pos (another name for skill1), middle_pos (skill2) and leg_pos (skill3). We need to get these values in a while loop because if the enemy moves (jumps, crawls, etc) these coords will change.
Ok, let's imagine that our bullet has hit the enemy -> function compute_shot_pos() will run:
Skill5 will be set to 1, stopping the while loop in action enemy. We store bullet's coords before they get lost (before the bullet disappears), we wait for a frame and then we set my.event to null.
if (abs(bullet_pos.z - my.head_pos) < abs(bullet_pos.z - my.middle_pos)) // we've got a head shot, no need to check the legs
If the distance between the bullet and the head vertex is smaller than the distance between the bullet and the middle vertex, we have got a head shot so we play the "head" animation.
else // middle or leg shot
If the distance between the bullet and the head vertex is bigger than the distance between the bullet and the middle vertex, it could be a middle (belly) shot or a leg shot so we play the correct animation. I have animated the model so beware... you'll see some scary stuff!
The last two functions create the bullets and remove them - here's the code:
The bullet is sensitive to other entities and level blocks. When it collides with another entity (our enemy) or a level block, it will be removed after a frame, allowing us to get its coords before it disappears. The bullet has the same orientation with the player and the camera.
my.skill20 = 0;
while (my.skill20 < 250)
We set skill20 to zero and then we increase this value inside a while loop until it reaches 250. If the bullet hasn't hit anything when skill20 >= 250, it will be removed because we don't want to waste cpu power with it. Skill1..3 are used as a local var to move the bullet. A small trick: the bullet is created inside the player model so it has to be made passable at the beginning because otherwise it would collide with the player. When skill20 >= 1 we can assume that the bullet is far enough and then we will make it impassable.
You can use more key vertices if you like; you could add another key vertex to the other leg, 2 more on the arms, etc.