Hi
I have used the walking function in the very latest (7.1) help file to demonstrate a very simple program for people like me who are starting to learn Lite-C. This program enables a user to walk cbabe around the terrain or level. I hope it helps. I need stuff like this myself to help me progress.
Copy and paste into SED, ensure you have cbabe.mdl or other and a terrain or a level created in WED in the same folder. Make sure also that you save file as ***.C ie do NOT have a .WDL extension as this is C Script.
#include <acknex.h>
#include <default.c>
// Conitec's function in a program for walking over ground
// control the player with the WASD keys
// player origin must be at the model center
// bounding box must be smaller than the player!
action player_walk()
{
// if necessary, adjust the bounding box to give the entity 'floor room' (see remarks)
// my.min_z *= 0.5;
var speed_down = 0; // downward speed by gravity
var anim_percent = 0; // animation percentage
VECTOR vFeet;
vec_for_min(vFeet,me); // vFeet.z = distance from player origin to lowest vertex
set(me,SHADOW); // you may want shadows ??
while (1)
{
// rotate the player using the [A] and [D] keys
my.pan += 5*(key_a-key_d)*time_step;
// determine the ground distance by a downwards trace
var dist_down;
if (c_trace(my.x,vector(my.x,my.y,my.z-5000),IGNORE_ME | IGNORE_PASSABLE | USE_BOX) > 0)
dist_down = my.z + vFeet.z - target.z; // get distance between player's feet and the ground
else
dist_down = 0;
// apply gravity when the player is in the air
if (dist_down > 0) // above floor, fall down with increasing speed
dist_down = clamp(dist_down,0,accelerate(speed_down,5,0.1));
else // on or below floor, set downward speed to zero
speed_down = 0;
// move the player using the [W] and [S] keys
var dist_ahead = 5*(key_w-key_s)*time_step;
dist_ahead = sign(dist_ahead)*(abs(dist_ahead) + 0.5*dist_down); // adapt the speed on slopes
c_move(me,vector(dist_ahead,0,0),vector(0,0,-dist_down),IGNORE_PASSABLE | GLIDE); // move the player
// animate the player according to its moved distance
if (dist_ahead != 0) // player is moving ahead
{
anim_percent += 1.3*dist_ahead; // 1.3 = walk cycle percentage per quant
ent_animate(me,"walk",anim_percent,ANM_CYCLE); // play the "walk" animation
}
else // player stands still
{
anim_percent += 5*time_step;
ent_animate(me,"stand",anim_percent,ANM_CYCLE); // play the "stand" animation
}
wait(1);
}
}
function main() // start of program
{
video_mode = 7;
//video_screen =1;
shadow_stencil = 1;
wait(1);
level_load("small.hmp");
wait(2);
ent_create("cbabe.mdl", vector(0,0,20), player_walk); // assumes you're using cbabe
//handle = media_loop("miami.mp3",NULL,20); // how about some nice music, 20 = vol level
video_window(NULL,NULL,0,"My simple demo game"); // change the window text from GameStudio
}
Last edited by dracula; 07/11/08 16:16.