////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
VECTOR move_vec;
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// camera:
var cam_height = 25;
// crawling:
var crouch_timer = 0;
var crawling = 0;
var allow_stand = 1;
var crawl_old = 0;
// gravity:
var my_height;
var jump_time = 1;
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
ENTITY* c_hull;
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
function handle_crawl()
{
VECTOR temp;
vec_set(temp.x, my.x);
temp.z = my.z + 20; // top point of BBOX
trace_mode = IGNORE_ME | IGNORE_PASSABLE | USE_BOX;
c_trace(my.x, temp, trace_mode);
draw_line3d(my.x,vector(255,255,255),100);
draw_line3d(temp,vector(255,255,255),100);
if(trace_hit)
{
allow_stand = 0;
}
else
{
allow_stand = 1;
}
if(key_ctrl) // if ctrl is pressed
{
/* if(crouch_timer > -25)
{
crouch_timer -= 5 * time_step;
if(crouch_timer > 100){crouch_timer = 100;}
}
else
{
crawling = 1;
}
camera.z += crouch_timer;
*/
crawling=1;
}
else
{
/* if(crouch_timer < 0)
{
if(allow_stand == 1)
{
crawling = 0;
crouch_timer += 5 * time_step;
camera.z += crouch_timer;
}
else
{
camera.z += crouch_timer;
}
}
*/
crawling=0;
}
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
function handle_camera()
{
camera.pan -= 1 * mickey.x * time_step;
camera.tilt -= 1 * mickey.y * time_step;
camera.tilt = clamp(camera.tilt, -90, 90);
//vec_set(camera.x, vector(my.x, my.y, my.z + cam_height));
camera.x = my.x + fcos((camera.pan), fcos(camera.tilt, -100));
camera.y = my.y + fsin((camera.pan), fcos(camera.tilt, -100));
camera.z = my.z + fsin(camera.tilt, -100);
pXent_rotate(c_hull, vector(camera.pan, 0, 0), nullvector);
// crawling:
handle_crawl();
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
function apply_gravity()
{
VECTOR temp;
VECTOR vec_min;
vec_set(temp, my.x);
temp.z -= 1000;
trace_mode = IGNORE_ME | IGNORE_FLAG2 | IGNORE_PASSABLE | USE_BOX;
c_trace(my.x, temp, trace_mode);
vec_for_min(vec_min, my);
my_height = my.z + vec_min.z - target.z;
if(my_height > 20)
{
accelerate(move_vec.z, -6 * time_step, -0.5);
}
else
{
if(jump_time == 1)
{
move_vec.z = -(my_height / 1.2) + 2;
move_vec.z = clamp(move_vec.z, -5, 5);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
action hero()
{
player = my;
set(my,TRANSLUCENT);
set(my,PASSABLE);
wait(1);
c_setminmax(my);
// create bbox shapes:
ENTITY* c_stand = ent_create("bbox.mdl", vector(0, 0, -10000), NULL);
ENTITY* c_crawl = ent_create("bbox.mdl", vector(0, 0, -10000), NULL);
// scale it down:
c_crawl.scale_z = 0.5;
// make them passable and transparent:
set(c_stand, PASSABLE | TRANSLUCENT);
set(c_crawl, PASSABLE | TRANSLUCENT);
// unregistered in physiX:
pXent_settype(my, 0, 0);
pXent_enable(my,0);
// attach all parts:
c_hull = c_stand;
vec_set(c_hull.x, my.x);
vec_set(c_hull.pan, my.pan);
pXent_settype(c_crawl, 0, 0);
pXent_settype(c_stand, PH_CHAR, PH_CAPSULE);
wait(1);
while(1)
{
DEBUG_VAR(crawling, 20);
vec_set(my.x, c_hull.x);
// crawling shape:
if(crawl_old != crawling)
{
if(crawling == 1)
{
c_hull = c_crawl;
vec_set(c_hull.x, my.x);
vec_set(c_hull.pan, my.pan);
pXent_settype(c_stand, 0, 0);
pXent_settype(c_crawl, PH_RIGID, PH_CAPSULE);//***********
pXent_setbodyflag(c_crawl,NX_BF_FROZEN_ROT , 1);//************
}
if(crawling == 0)
{
c_hull = c_stand;
vec_set(c_hull.x, my.x);
vec_set(c_hull.pan, my.pan);
pXent_settype(c_crawl, 0, 0);
pXent_settype(c_stand, PH_CHAR, PH_CAPSULE);//************
}
crawl_old = crawling;
}
// movement:
if(crawling == 0)
{
move_vec.x = 10 * (key_w - key_s) * time_step * (1 + key_shift * 0.5);
move_vec.y = 10 * (key_a - key_d) * time_step * (1 + key_shift * 0.5);
}
else
{
move_vec.x = 2 * (key_w - key_s) * time_step;
move_vec.y = 2 * (key_a - key_d) * time_step;
}
// gravity:
if(!crawling) apply_gravity();//****************
// apply all forces:
pXent_move(c_hull, move_vec, nullvector);
// attach camera:
handle_camera();
// attach model parts:
vec_set(c_stand.x, c_hull.x);
vec_set(c_stand.pan, c_hull.pan);
vec_set(c_crawl.x, c_hull.x);
vec_set(c_crawl.pan, c_hull.pan);
// attach visible model:
vec_set(my.x, c_hull.x);
my.pan = camera.pan;
wait(1);
}
// DEATH HERE
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////