Questions from the forum

Top  Previous  Next

Q: How do I make my entities to react to bullets only and not to the actual player?

A: Here's an example.

 

function remove_bullets() // this function runs when the bullet collides with something

{

       wait (1); // wait a frame to be sure (don't trigger engine warnings)

       ent_remove (my); // and then remove the bullet

}

 

function move_bullets()

{

       var bullet_speed[3]; // this var will store the speed of the bullet

       my.skill30 = 1; // I'm a bullet

       my.emask |= (ENABLE_BLOCK | ENABLE_ENTITY);

       my.event = remove_bullets; // when it collides with something, its event function (remove_bullets) will run

       my.pan = camera.pan; // the bullet has the same pan

       my.tilt = camera.tilt; // and tilt with the camera

       bullet_speed[0] = 50 * time_step; // adjust the speed of the bullet here; it will move in the direction given by the camera

       bullet_speed[1] = 0; // the bullet doesn't move sideways

       bullet_speed[2] = 0; // or up / down on the z axis

       while (my) // this loop will run for as long as the bullet exists (it isn't "null")

       {

               c_move (my, bullet_speed, nullvector, IGNORE_YOU); // move the bullet ignoring its creator (the player)

               wait (1);

       }

}

 

function fire_bullets()

{

       var temp_pos[3];

       proc_kill(4); // don't allow more than 1 instance of this function to run

       while (mouse_left) {wait (1);} // wait until the player releases the mouse button

       vec_set(temp_pos, vector(50, 0, 0));

         vec_rotate(temp_pos, camera.pan);

       vec_add(temp_pos, camera.x);

       ent_create ("bullet.mdl", temp_pos, move_bullets); // create the bullet at camera's position and attach it the "move_bullets" function

}

 

action my_player()

{

       player = my; // I'm the player

       set (my, INVISIBLE); // no need to see player's model in 1st person mode

       while (1)

       {

               // move the player using the "W", "S", "A" and "D" keys; "10" = movement speed, "6" = strafing speed

               c_move (my, vector(20 * (key_w - key_s) * time_step, 6 * (key_a - key_d) * time_step, 0), nullvector, GLIDE);

               if (mouse_left) // if the player presses the left mouse button

               {

                       fire_bullets(); // call this function

               }

               vec_set (camera.x, player.x); // use player's x and y for the camera as well

               camera.z += 30; // place the camera 30 quants above the player on the z axis (approximate eye level)

               camera.pan -= 5 * mouse_force.x * time_step; // rotate the camera around by moving the mouse

               camera.tilt += 3 * mouse_force.y * time_step; // on its x and y axis

               player.pan = camera.pan; // the camera and the player have the same pan angle

               wait (1);

       }

}

 

function got_shot()

{

       if (you.skill30 != 1) {return;} // didn't collide with a bullet? Then nothing should happen

       my.skill99 = 1;

}

 

action my_entity() // attach this action to your entities

{

       var anim_percentage = 0;

       my.skill99 = 0;

       my.emask |= (ENABLE_IMPACT | ENABLE_ENTITY);

       my.event = got_shot; // and runs this function when it is hit

       while (my.skill99 == 0)

       {

               ent_animate(my, "stand", anim_percentage, ANM_CYCLE); // play the "stand" aka idle animation

               anim_percentage += 3 * time_step; // "3" controls the animation speed

               wait (1);

       }

       my.event = NULL;

       anim_percentage = 0;

       while (anim_percentage < 100) // the loop runs until the "death" animation percentage reaches 100%        

       {

               ent_animate(my, "deatha", anim_percentage, NULL); // play the "die" animation only once

               anim_percentage += 3 * time_step; // "3" controls the animation speed

               wait (1);

       }

       set (my, PASSABLE); // allow the player to pass through the corpse now

}

 

 

Q: I am trying to get collision events for entities. Can someone show me a small example?

A: There you go.

 

SOUND* beep1_wav = "beep1.wav";

 

function collision_event()

{

       if (event_type == EVENT_ENTITY) // the ball has collided with one of the entities in the level?

       {

                 ent_playsound(my, beep1_wav, 500);

       }

       vec_to_angle(my.pan,bounce); // change direction even if the ball has collided with one of the level blocks

 

action bouncing_ball()

{

       my.emask |= (ENABLE_BLOCK | ENABLE_ENTITY); // make entity sensitive for block and entity collision

       my.event = collision_event;

       while(1)

       {

                 c_move(my, vector(10 * time_step, 0, 0),nullvector, NULL); // 10 gives the ball movement speed

                  wait(1);

       }

}

 

 

Q: I'd like to have a camera that swings out a bit, just like a boat riding a small wave.

A: Here's an example.

 

function swinging_camera_startup()

{

       var temp_cam = 0;

       while (1)

       {

               temp_cam += 3 * time_step; // 3 = speed

               camera.z += 0.5 * sin(temp_cam); // 0.5 = amplitude, replace camera.z with camera.x or camera.y if you need to

               wait (1);

       }

}

 

 

Q: I have a bmp file (grid.bmp) and I would like to show it just a few pixels above my terrain. How I do to it?

A: Use the code below.

 

function control_grid()

{

       set (my, PASSABLE);

       my.tilt = -90;

       my.z = you.max_z + 5; // place the grid 5 quants above the heighest vertex on the terrain

}

 

action my_terrain() // attach this action to your terrain

{

       c_setminmax(my);

       ent_create("grid.bmp", my.x, control_grid);

}

 

 

Q: What action do I need to create (using lite-C) to get a model to follow a predefined path?

A: Attach this action to your model.

 

var entity_speed = 3;

var movement_enabled = 0;

var dist_to_node;

var current_node = 1;

var angle_difference = 0;

 

VECTOR temp_angle;

VECTOR pos_node[3]; // stores the position of the node

 

function move_target()

{

       while(1)

       {

               if(movement_enabled)

               {

                       entity_speed = minv(5, entity_speed + 0.5 * time_step);

                       c_move(my, vector(entity_speed * time_step, 0, 0), nullvector, IGNORE_PASSABLE | GLIDE);

                       vec_to_angle (my.pan, vec_diff (temp_angle, pos_node, my.x));

               }

               wait(1);

       }

}

 

action move_on_path() // attach this action to your model

{        

       move_target();

       result = path_scan(me, my.x, my.pan, vector(360, 180, 1000));

       if (result) {movement_enabled = 1;}

       path_getnode (my, 1, pos_node, NULL);

       vec_to_angle (my.pan, vec_diff (temp_angle, pos_node, my.x)); // rotate towards the node

       while(1)

       {

               dist_to_node = vec_dist(my.x, pos_node);

               if(dist_to_node < 50) // close to the node?

               {

                       current_node = path_nextnode(my, current_node, 1);

                       if (!current_node) {current_node = 1;} // reached the end of the path? Then start over!

                       path_getnode (my, current_node, pos_node, NULL);

               }

               wait(1);

       }

}

 

 

Q: I need to close the level and after one second load another level. I have used game_exit but this doesn't work.

A: The previous level is "closed" automatically as soon as you load a new level, so you can use something like this:

 

STRING* second_wmb = "second.wmb";

 

action new_level_loader() // attach this action to an entity at the end of the first level

{

       while (!player) {wait (1);} // wait until the player is loaded

       while (vec_dist (player.x, my.x) > 100)  // wait until the player comes close to this entity

       {

               wait (1);        

       }

       wait (-1); // wait for a second

       level_load (second_wmb); // now load the second level

}

 

 

Q: My player must move from the far left of the screen to the right, facing the proper way of course. Any press of the left or right keys should rotate the player in the direction he is walking.

A: Here's a snippet that should help you get started.

 

// make sure that your player model is placed properly in Wed (facing the positive part of the x axis)

action player_code()

{

       var anim_percentage;

         player = my; // I'm the player

       camera.tilt = 0;

         camera.pan = 90; // play with this value

         while (1)

         {

                 // move the player using the left and right arrow keys

               if (key_cur)

               {

                       while (my.pan > 0)

                       {

                               my.pan -= 15 * time_step; // 15 = rotation speed

                               wait (1);

                       }

                       my.pan = 0;

               }

               if (key_cul)

               {

                       while (my.pan < 180)

                       {

                               my.pan += 15 * time_step; // 15 = rotation speed

                               wait (1);

                       }

                       my.pan = 180;

               }

            if (key_cur || key_cul) // one of the movement keys is pressed?

            {

                    ent_animate(my, "walk", anim_percentage, ANM_CYCLE);

                       anim_percentage += 7 * time_step; // "7" controls the "walk" animation speed

                    c_move (my, vector(8 * time_step, 0, 0), nullvector, GLIDE); // 8 = speed

             }

                 else // the player is standing still?

            {

                    ent_animate(my, "stand", anim_percentage, ANM_CYCLE);

                       anim_percentage += 1 * time_step; // "1" controls the "stand" animation speed

            }

            vec_set (camera.x, player.x);

            camera.y -= 300; // place the camera 300 quants behind the player (play with this value)

            camera.z += 40; // place the camera 40 quants above the player on the z axis (play with this value)

            wait (1);

       }

}

 

 

Q: How can I swap the bmap of two panels?

A: Take a look at the following example.

 

BMAP* first_pcx = "first.pcx"; // the first bmap

BMAP* second_pcx = "second.pcx"; // the second bmap

 

PANEL* first_pan =

{

       pos_x = 0;

       pos_y = 0;

       bmap = first_pcx;

       flags = VISIBLE;

}

 

PANEL* second_pan =

{

       pos_x = 300;

       pos_y = 0;

       bmap = second_pcx;

       flags = VISIBLE;

}

 

function switch_bitmaps()

{

       first_pan.bmap = second_pcx;

       second_pan.bmap = first_pcx;

}

 

function initkeys_startup()

{

       on_s = switch_bitmaps; // press the "S" key to switch the bitmaps

}

 

 

Q: I have a car code that works ok, but I'd like to attach it sprite-based lights that move and rotate together with the car. How can I do that?

A: There you go:

 

STRING* light_tga = "light.tga";

 

function place_light1()

{

       set (my, PASSABLE);

       my.tilt = 90;

       while (1)

       {

               my.x = you.x + 90 * cos (you.pan) - 60 * sin (you.pan); // 90 and 60 give the offset from the center

               my.y = you.y + 90 * sin (you.pan) + 60 * cos (you.pan); // of the model on the x and y axis

               my.z = you.z + 15; // 15 gives the offset on the z axis

               my.pan = you.pan;

               my.tilt = you.tilt + 90;        

               wait (1);

       }                

}

 

function place_light2()

{

       set (my, PASSABLE);

       my.tilt = 90;

       while (1)

       {

               my.x = you.x + 90 * cos (you.pan) + 60 * sin (you.pan); // 90 and 60 give the offset from the center

               my.y = you.y + 90 * sin (you.pan) - 60 * cos (you.pan); // of the model on the x and y axis

               my.z = you.z + 15; // 15 gives the offset on the z axis

               my.pan = you.pan;

               my.tilt = you.tilt + 90;        

               wait (1);

       }                

}

 

action my_car() // simple action, just for testing

{

       ent_create (light_tga, my.x, place_light1);

       ent_create (light_tga, my.x, place_light2);

       while (1) // use your own car code here

       {

               c_move (my, vector (10 * time_step, 0, 0), nullvector, IGNORE_PASSABLE | GLIDE);

               my.pan += 2 * time_step; // make this car rotate in a circle at all times

               wait (1);

       }

}

 

 

Q: I'm trying to set up a small game with a ship that is controlled using only the space key (press to give the ship some gas, release the key to cut it). Can you give some advice?

A: Use this piece of code.

 

function destroy_ship()

{

       my.skill1 = 0;        

}

 

action my_ship()

{

       my.pan = 0;

       my.tilt = 0;

       my.roll = 0;

       my.emask |= (ENABLE_BLOCK | ENABLE_ENTITY | ENABLE_IMPACT);

       my.event = destroy_ship;

       var ship_speed[3];

       my.skill1 = 1;

       ship_speed[1] = 0;

       ship_speed[2] = 0;

       while (my.skill1 > 0)

       {

               ship_speed[0] = 4 * time_step;

               if (key_space)        

               {

                       ship_speed[2] += 0.5 * time_step;

               }                

               else

               {

                       ship_speed[2] -= 0.2 * time_step;                        

               }

               c_move (my, ship_speed, nullvector, IGNORE_PASSABLE);

               wait (1);

       }

       // explode your ship here, destroy it, etc

}