action player_code()
{
VECTOR temp[3];
VECTOR movement_speed[3]; // player's movement speed
var anim_percentage; // animation percentage
var jump_percentage; // animation percentage for jumping
var distance_to_ground; // the distance between player's origin and the ground
var jump_height;
var reached_height;
player = my; // I'm the player
while (1)
{
my.pan += 6 * (key_a - key_d) * time_step; // rotate the player using the "A" and "D" keys
vec_set (temp.x, my.x); // copy player's position to temp
temp.z -= 10000; // set temp.z 10000 quants below player's origin
distance_to_ground = c_trace (my.x, temp.x, IGNORE_ME | USE_BOX);
movement_speed.x = 5 * (key_w - key_s) * time_step; // move the player using "W" and "S"
movement_speed.y = 0; // don't move sideways
if (key_space && !reached_height)
{
jump_height = minv(40, jump_height + 5 * time_step); // 40 sets the height, 5 sets the ascending speed
if (jump_height == 40) // reached the maximum height? Then start descending!
{
reached_height = 1;
jump_height = maxv(0, jump_height - 5 * time_step); // 5 sets the falling speed
}
}
else // space isn't pressed anymore?
{
jump_height = maxv(0, jump_height - 3 * time_step); // use a smaller falling speed (3) for smaller jumps
if (!jump_height && !key_space) // the player has touched the ground?
{
reached_height = 0; // then allow it to jump again
}
}
movement_speed.z = -(distance_to_ground - 17) + jump_height; // 17 = experimental value
movement_speed.z = maxv(-35 * time_step, movement_speed.z); // 35 = falling speed
c_move (my, movement_speed.x, nullvector, GLIDE); // move the player
if (!jump_height) // the player isn't jumping?
{
if (!key_w && !key_s) // 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_step; // 5 = animation speed
jump_percentage = 0; // always start jumping with the first frame
}
else // the player is jumping
{
jump_percentage += 5 * time_step; // 5 = jump animation speed
ent_animate(my, "jump", jump_percentage, ANM_CYCLE); // play the "jump" animation
}
// camera code
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
camera.tilt = -20; // look down at the player
camera.pan = player.pan;
wait (1);
}
}