Beginner's corner |
Top Previous Next |
Car demo This code was written for those of you that want to create a car game and need a standalone version of the player_drive action in the templates. action my_car my.enable_block = on; while (1) The car is moving as long as its damage is below 100. The car can rotate (change its pan angle) as long as car_speed != 0 - this way we obtain the "wheels" flag effect in the standard player_drive. The line that computes car_speed is similar to the one in the tutorial (for Tuesday) that comes with the A4 / A5 manual: v -> v + dv. Change 3 to change the speed and 0.2 to change the rotation speed. If we press one of the shift keys, the car will stop (ent_move isn't executed anymore) and we can rotate the car without moving it by pressing two arrow keys (up + right, down + left, etc) at the same time. This is the only way to see my model in its full splendor so don't miss this opportunity! The car can collide with level blocks and entities; when this happens, increase_damage is executed: function increase_damage() You can see that the damage depends on car_speed. If the car collides with blocks, the damage is bigger. Btw, I'm using abs(car_speed) in this function because if you would hit a block with the back of the car, the damage would be lowered :) because of the negative car_speed. If damage is bigger than 20 the car will emit smoke (bigger damage = thicker smoke): function car_smoke() There's nothing special in this function: the particles grow transparent at the end of their life; their size grows up to 1000. You should emit smoke from one of the vertices in the back of the car. The last function is: function move_carview() We've discussed this type of camera in a previous AUM edition; please note that the camera will follow the car with a little delay (change 0.2 to 0.05 or 0.5). I almost forgot: the action stand_still is attached to every guard.mdl in the level and stops their animations. Camera follows a path I have to be honest with you: I got this idea from Doug (I took a good look at his "bird follows path" code). The result of my work is different but he deserves credit here. We don't want to modify the templates, so we define a new view named cutcamera. We have to place an entity (fish.mdl?) in the level and attach it the cutcamera_init action: action cutcamera_init my.x = my._target_x; cutcamera.visible = on; // make cutcamera visible The camera will rotate to face the fish entity all the time. The fish can be placed anywhere in the level because it is invisible and passable so it won't get hurt if you do that. First of all we store the fish coordinates and the maximum speed. We disable the default (camera) view and we set the new cutcamera. The fish will move to the first waypoint in the "campath" path (that's why we store its initial coordinates). while(1) vec_set(my._speed, my._target_x); vec_set (temp, cut_center.x); The fish moves on the 3D path and the cutcamera view is attached to it. There are two things that we must consider:
If we want to rotate the "my" object towards the "you" object we have to use 3 lines of code according to the manual: vec_set (temp, you.x); I have used these lines (I could have used vec_rotate, too) to set the _speed vector in the direction of the next waypoint and to rotate the view towards cut_center (all the red dots point towards cut_center but I didn't want to make a mess of the picture). We normalize the _speed vector; we cut the length and keep its direction. You might wonder what's with these lines: if (vec_to_angle(my.pan, my._speed) < 15) I have used a little trick here: I am assuming that you are lazy and your path has less than a thousand points, so the camera shakes when it reaches the target (one of the points on campath) and has to change its direction (take a look).
What's left to be added to this beautifully working piece of code? We could lower its speed when it reaches a certain area (let's say that we want to take a good look at the nuclear core in our level) then accelerate it back when it has left that area. I have created this type of movement in my office.wmp example when the fish goes below 190 quants (I got this value in Wed) if (my.z < 190) We've stored max_speed in skill20 and now we lower it from 7 (initial value) to 3.5; please note that we aren't simply setting max_speed to 3.5 but we are constantly decreasing it (we multiply it by 0.995) until it reaches 3.5. When we exit our special area (my.z > 190) max_speed is constantly increased (multiplied by 1.005) from 3.5 to 7 which was stored in skill20, remember? Of course that you can add multiple conditions like this: if (my.x < 100 && my.y > 250 && my.y < 300 && my.z > 400) |