Fragen aus dem Forum

Top  Previous  Next

F: Wenn der Spieler in die Wand schießt, soll ein Einschussloch erscheinen.

A: Hier ist ein Codeausschnitt:

 

var video_mode = 7; // 800x600 pixels

var video_depth = 32; // 32 bit mode

var video_screen = 1; // start the engine in full screen mode

 

string test_wmb = <test.wmb>;

string hole_pcx = <hole.pcx>;

 

function fire_bullets(); // fires the bullets

function bullet_hole(); // places bullet holes on the walls

 

function main()

{

       level_load (test_wmb);

}

 

action players_code

{

       player = my; // I'm the player

       my.invisible = on; // 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(10 * (key_w - key_s) * time, 6 * (key_a - key_d) * time, 0), nullvector, glide);

               if (mouse_left == on) // 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; // rotate the camera around by moving the mouse

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

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

               wait (1);

       }

}

 

function fire_bullets()

{

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

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

       var trace_coords;

       vec_set(trace_coords.x, vector(5000, 0, 0)); // trace 5000 quants in front of the player

       // rotate "temp" in the direction (angles) given by the camera (the player)

       vec_rotate(trace_coords.x, camera.pan);

       vec_add(trace_coords.x, my.x); // add the resulting vector to player's position

       if (c_trace (player.x, trace_coords.x, ignore_me + use_box) > 0) // hit something?

       {

               ent_create (hole_pcx, target.x, bullet_hole); // then create a bullet hole!

       }

}

 

function bullet_hole

{

       my.transparent = on;

       my.flare = on;

       my.passable = on;

       my.oriented = on;

       my.roll = random(360); // choose a random roll angle for each bullet hole

       vec_to_angle (my.pan, normal.x); // rotate the bullet hole sprite properly

       vec_add (my.x, normal.x); // and move it a bit away from the wall

}

 

 

F: Haben Sie ein Beispiel für ein Radar?

A: Ja, nutzen Sie den folgenden Code.

 

var video_mode = 7; // 800x600 pixels

var video_depth = 32; // 32 bit mode

var video_screen = 1; // start the engine in full screen mode

 

string test_wmb = <test.wmb>;

 

view radar_view

{

       layer = 15;

       size_x = 150; // size of the radar window on the x axis

       size_y = 150; // size of the radar window on the y axis

       arc = 90; // zoom factor, play with this value to see a larger portion of the screen

       flags = visible; // show the radar view

}

 

function main()

{

       level_load (test_wmb);

       while (1)

       {

               // always keep the radar view in the proper position no matter what the video resolution is

               radar_view.pos_x = screen_size.x - 150;

               radar_view.pos_y = 0; // place the radar in the top right corner of the screen

               wait (1);

       }

}

 

action players_code

{

       player = my; // I'm the player

       my.invisible = on; // 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(10 * (key_w - key_s) * time, 6 * (key_a - key_d) * time, 0), nullvector, glide);

               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; // rotate the camera around by moving the mouse

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

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

               wait (1);

       }

}

 

starter init_radar()

{

       while (player == null) {wait (1);}

       while (1)

       {

               radar_view.x = player.x;

               radar_view.y = player.y;

               // place the radar view 1000 quants above player's origin; play with this value

               radar_view.z = player.z + 1000;

               radar_view.pan = player.pan;

               radar_view.tilt = -90; // tilt the radar view downwards

               wait (1);

       }

}

 

 

F: Ich habe eine Frage zum anpassbaren Interface aus AUM 39. Wie kann ich das Panel bewegen und seine Position sichern, auch nachdem die Engine beendet wurde?

A: Hier ist ein Beispiel.

 

var panel_handle;

 

bmap hud_pcx = <hud.pcx>;

bmap pointer_pcx = <pointer.pcx>;

 

function drag_panel();

 

panel hud_pan

{

       bmap = hud_pcx;

       layer = 20;

       pos_x = 0; // for now

       pos_y = 0; // for now

       on_click = drag_panel;

       flags = overlay, refresh, visible;

}

 

starter init_mouse()

{

       panel_handle = file_open_read("panel.txt");

       if (panel_handle == 0) // "panel.txt" doesn't exist?

       {

               hud_pan.pos_x = 0; // then place the panel in the upper left corner

               hud_pan.pos_y = 0; // (the default position)

       }

       else // the file was created, so we can read from it?

       {

               hud_pan.pos_x = file_var_read (panel_handle); // read the previously saved pos_x

               hud_pan.pos_y = file_var_read (panel_handle); // read the previously saved pos_y

               file_close(panel_handle);        

       }

       mouse_map = pointer_pcx;

       while (1)

       {

               if (key_space == on)

               {

                       mouse_mode = 2;

               }

               else

               {

                       mouse_mode = 0;

               }

               mouse_pos.x = pointer.x;

               mouse_pos.y = pointer.y;

               wait (1);

       }

}

 

function drag_panel()

{

       while (mouse_left == on)

       {

               hud_pan.pos_x = pointer.x - bmap_width(hud_pcx) / 2;

               hud_pan.pos_y = pointer.y - bmap_height(hud_pcx) / 2;                

               wait (1);

       }

       // the player has stopped moving the panel here, so let's write the coordinates to a file

       panel_handle = file_open_write("panel.txt"); // open (or create and open) this file

       file_var_write (panel_handle, hud_pan.pos_x); // write the x position of the panel to file

       file_var_write (panel_handle, hud_pan.pos_y); // write the y position of the panel to file

       file_close(panel_handle);        

}

 

 

F: Wie kann ich aus der Engine heraus den Namen eines Models feststellen (z.B. “pizza.mdl”)?

A: Hier ist ein Codeausschnitt, der das leistet.

 

string temp_str[50];

string entity_str[50];

 

text name_txt

{

       layer = 10;

       pos_x = 5;

       pos_y = 5;

       string = entity_str;

       flags = visible;

}

 

starter get_names()

{

       while (1)

       {

               if (mouse_ent != null)

               {

                       str_for_entname (temp_str, mouse_ent);

                       you = ptr_for_name(temp_str);

                       str_for_entfile(entity_str, you);

               }

               else

               {

                       str_cpy (entity_str, "No entity detected");

               }

               wait (1);

       }

}

 

 

F: Ich benötige zwei verschiedene Steuerungsarten; die Bewegung soll mit Hilfe der WASD Tasten und die Kamera soll mit den Pfeiltasten gesteuert werden.

A: Hier ist ein Beispiel.

 

var video_mode = 7; // 800x600 pixels

var video_depth = 32; // 32 bit mode

var video_screen = 1; // start the engine in full screen mode

 

var camera_offset_x = 0;

var camera_offset_y = 0;

 

string test_wmb = <test.wmb>;

 

function main()

{

       level_load (test_wmb);

}

 

action players_code

{

       var anim_percentage;

       player = my; // I'm the player

       while (1)

       {

               player.pan += 3 * (key_a - key_d) * time; // rotate the player using the A and D keys

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

               camera.x = player.x + camera_offset_x;

               camera.y = player.y + camera_offset_y;

               camera.z = player.z + 400; // place the camera 400 quants above the player

               camera.tilt = -90; // this is a top view camera

               c_move (my, vector(10 * (key_w - key_s) * time, 0, 0), nullvector, glide);

               if ((key_w == off) && (key_s == off)) // the player isn't moving?

               {

                       ent_animate(my, "stand", anim_percentage, anm_cycle); // play the "stand" animation

               }

               else // the player is moving?

               {

                       ent_animate(my, "walk", anim_percentage, anm_cycle); // play the "walk" animation

               }

               anim_percentage += 5 * time; // "5" controls the animation speed

               anim_percentage %= 100;

               if (key_cud == on)

               {

                       camera_offset_x -= 5 * time;

               }

               if (key_cuu == on)

               {

                       camera_offset_x += 5 * time;

               }

               if (key_cul == on)

               {

                       camera_offset_y += 5 * time;

               }

               if (key_cur == on)

               {

                       camera_offset_y -= 5 * time;

               }

               wait (1);

       }

}

 

 

F: Ich hätte gern ein Login-Modul für mein Spiel, wo Spieler ein Profil erstellen und individuelle Einstellungen sichern können. Diese sollten in einer Textdatei gespeichert werden, damit nichts verloren geht.

A: Sehen Sie sich diesen Ausschnitt gut an.

 

var data_handle;

 

string name_str[30];

 

text login_txt

{

       layer = 10;

       pos_x = 300;

       pos_y = 275;

       string = name_str;

}

 

starter log_me_in()

{

       login_txt.string = "Good morning, Sir. Please enter your name.";

       login_txt.visible = on;

       sleep (3); // show the text for 3 seconds

       login_txt.string = name_str;

       str_cpy(name_str, ""); // reset the string

       while (str_cmpi (name_str, "") == 1) // run this loop until the player has typed something

       {                

               inkey (name_str); // store player's input in name_str

               wait (1);

       }

       data_handle = file_open_write("data.txt"); // open (or create and open) this file

       file_str_write (data_handle, name_str); // write the name of the player to the file

       file_close (data_handle);        

}

 

 

F: Kann man den Mauszeiger ausblenden, wenn ich auf eine Texteingabe warte?

A: Hier ist Code, der das kann.

 

starter hide_mouse()

{

       while (1)

       {

               if (inkey_active == on) // inkey is active?

               {

                       mouse_mode = 0; // then hide the mouse pointer

               }

               else // inkey isn't active?

               {

                       mouse_mode = 2; // then show the mouse pointer

               }

               wait (1);

       }

}

 

 

F: Ich möchte, dass sich ein Model anders verhält, wenn es getroffen wird oder wenn der Spieler in der Nähe ist und eine Taste drückt. Können Sie mir helfen?

A: Hier ist ein Beispiel.

 

function got_shot() // got hit by a "c_trace" instruction with "activate_shoot"?

{

       my.pan = random(360); // put your own code for the shooting part here

}

 

action my_model

{

       my.enable_shoot = on;

       my.event = got_shot;

       // make sure that your player action has the "player = my;" line at its beginning

       while (player == null) {wait (1);} // wait until the player is created

       while (1)

       {

               // the player is closer than 200 quants and the "P" key is pressed?

               if ((vec_dist (player.x, my.x) < 200) && (key_p == on))

               {

                       my.ambient = random(100); // put your own code for the key pressing here

               }

               wait (1);

       }

}

 

 

F: Wie programmiere ich es, dass Dinge auf Tastendruck reagieren? Ich möchte eine Tür durch Drücken einer Taste in der Nähe öffnen können.

A: Hier ist ein Codeausschnitt für Sie.

 

action my_door

{

       while (player == null) {wait (1);} // wait until the player appears in the level

       while (1)

       {

               // the player has come closer than 100 quants to the door and the "space" key is pressed?

               if ((vec_dist (player.x, my.x) < 100) && (key_space == on))

               {

                       my.skill1 = my.z; // store the initial height of the door

                       while (my.z < my.skill1 + 200) // the door will move upwards 200 quants

                       {

                               my.z += 5 * time; // 5 = door speed

                               wait (1);

                       }

               }

               wait (1);

       }

}

 

 

F: Ich benötige eine Kamera, die dem Spieler folgt und sich mit ihm dreht.

A: Hier finden Sie ein Beispiel dafür.

 

action players_code

{

       player = my; // I'm the player

       var anim_percentage;

       camera.tilt = -15; // initial tilt value

       while (1)

       {

               // move the player using the "W" and "S" keys; "10" = movement speed

               c_move (my, vector(10 * (key_w - key_s) * time, 0, 0), nullvector, glide);

               // rotate the camera around by moving the mouse or "A" and "D"

               camera.pan -= 5 * mouse_force.x * time - 5 * (key_a - key_d) * time;

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

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

                // 250 = distance between the player and the camera, play with this value

               camera.x = player.x - 250 * cos(player.pan);

               camera.y = player.y - 250 * sin(player.pan); // use the same value (250) here

               camera.z = player.z + 150; // place the camera above the player, play with this value

               if ((key_w == off) && (key_s == off)) // the player isn't moving?

               {

                       ent_animate(my, "stand", anim_percentage, anm_cycle); // play the "stand" animation

               }

               else // the player is moving?

               {

                       ent_animate(my, "walk", anim_percentage, anm_cycle); // play the "walk" animation

               }

               anim_percentage += 3 * time;

               anim_percentage %= 100;

               wait (1);

       }

}