added proper jjump-ish movement and tweaked some stats

This commit is contained in:
Alexandre 2024-07-23 15:51:18 +02:00
parent 16977db2d6
commit 74a22a9ff0
17 changed files with 145 additions and 76 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -2,11 +2,21 @@ open Graphics ;;
Random.self_init () ;; Random.self_init () ;;
(* -------------------------------------------------------------------------------------------------------- *)
let __width__ = 1500 let __width__ = 1500
and __height__ = 1000 ;; and __height__ = 1000 ;;
let openstring = " 1500x1000" ;; let openstring = " 1500x1000" ;;
(* --------------------------------------------- *) (* ------------------------------------------------------------------------------------------------------------------------------------ *)
(* ------------------------------------------------------------------------------------------------------------------------------------ *)
(* -----------------------------X=======================================================================X------------------------------ *)
(* -----------------------------| Global/General configs |------------------------------ *)
(* -----------------------------X=======================================================================X------------------------------ *)
(* ------------------------------------------------------------------------------------------------------------------------------------ *)
(* ------------------------------------------------------------------------------------------------------------------------------------ *)
let max_fps = 120. ;;
type tile = Free | Wall | Crate | Exit | Craxit | Camera ;; type tile = Free | Wall | Crate | Exit | Craxit | Camera ;;
@ -15,7 +25,7 @@ and height = 4
and depth = 4 ;; and depth = 4 ;;
(* dimensions *) (* dimensions *)
let render_distance = 7 ;; (* render distance *)
let chunk_dist = 2 ;; let chunk_dist = 2 ;;
let cube_size = 3 ;; let cube_size = 3 ;;
@ -25,9 +35,7 @@ let chunk_size = 6 ;;
let chunk_size_f = float_of_int chunk_size ;; let chunk_size_f = float_of_int chunk_size ;;
(* between 0 (empty) and 100 (full) *) (* between 0 (empty) and 100 (full) *)
let density = 35 ;; let density = 25 ;;
let speed_multiplier = 0.1 ;;
(* money money money *) (* money money money *)
let coins = ref 0 ;; let coins = ref 0 ;;
@ -57,18 +65,34 @@ and az = ref 0.0 ;;
(* -------------------------------------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------------------------------------- *)
(* avg number of chunk generation required before encountering a structure of type 1 *) (* avg number of chunk generation required before encountering a structure of type 1 *)
let structure_1_frequency = 2500 ;; let structure_1_frequency = 5000 ;;
(* 0 ~ 1000 *)
let oreMissChance = 50 ;;
(* avg number of chunk generation required before encountering a structure of type 2 *) (* avg number of chunk generation required before encountering a structure of type 2 *)
(* NOTE : structure 2 attempts to spawn after structure 1, so the odds will be ever so slightly less than this number *) (* NOTE : structure 2 attempts to spawn after structure 1, so the odds will be ever so slightly less than this number *)
let structure_2_frequency = 2500 ;; let structure_2_frequency = 5000 ;;
(* -------------------------------------------------------------------------------------------------------- *) (* related to structures ; chance out of 1000 of a single ore generation failing *)
(* -------------------------------------------------------------------------------------------------------- *) (* 0 ~ 1000 *)
(* -------------------------------------------------------------------------------------------------------- *) let oreMissChance = 50 ;;
(* X and Z speed of player*)
let speed_multiplier = 0.6 ;;
(* Y speed of player*)
let speed_multiplier_Y = 0.4 ;;
(* accuracy for movement *)
let precision = 10 ;;
let precisionf = 10. ;;
(* friction multiplier *)
let f = 1.7 ;;
(* below that value, velocities will be set to 0. *)
let vel_epsilon = 0.005 ;;
(* ------------------------------------------------------------------------------------------------------------------------------------ *)
(* ------------------------------------------------------------------------------------------------------------------------------------ *)
type 'a dynamic = {mutable tab : 'a array ; mutable len : int ; mutable memlen : int} ;; type 'a dynamic = {mutable tab : 'a array ; mutable len : int ; mutable memlen : int} ;;
@ -1310,43 +1334,27 @@ let is_collision_global_G hash cx cy cz =
done; done;
!boo ;; !boo ;;
let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.print_endline " " ; *)match c with let add_velocity = function
| 'z' -> | 'z' ->
camera_xyz.z <- camera_xyz.z +. speed_multiplier *. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.); vz := -. speed_multiplier *. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. speed_multiplier *. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.); vy := -. speed_multiplier_Y *. 1.;
if b && ( vx := speed_multiplier *. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
is_collision_global_2 hash cx cy cz
) then move_cam_hash_2 hash cx cy cz false 's'
| 'q' ->
camera_xyz.z <- camera_xyz.z +. speed_multiplier *. Float.cos (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. speed_multiplier *. Float.sin (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
if b && (
is_collision_global_2 hash cx cy cz
) then move_cam_hash_2 hash cx cy cz false 'd'
| 's' ->
camera_xyz.z <- camera_xyz.z -. speed_multiplier *. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x -. speed_multiplier *. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
if b && (
is_collision_global_2 hash cx cy cz
) then move_cam_hash_2 hash cx cy cz false 'z'
| 'd' -> | 'd' ->
camera_xyz.z <- camera_xyz.z +. speed_multiplier *. Float.cos (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.); vz := speed_multiplier *. Float.cos (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. speed_multiplier *. Float.sin (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.); vy := -. speed_multiplier_Y *. 1.;
if b && ( vx := -. speed_multiplier *. Float.sin (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
is_collision_global_2 hash cx cy cz | 's' ->
) then move_cam_hash_2 hash cx cy cz false 'q' vz := speed_multiplier *. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
| 'p' -> vy := -. speed_multiplier_Y *. 1.;
camera_xyz.y <- camera_xyz.y -. speed_multiplier ; vx := -. speed_multiplier *. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
if b && ( | 'q' ->
is_collision_global_2 hash cx cy cz vz := speed_multiplier *. Float.cos (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.);
) then begin Stdlib.print_endline "Nope (p)" ; move_cam_hash_2 hash cx cy cz false 'm' end vy := -. speed_multiplier_Y *. 1.;
| 'm' -> vx := -. speed_multiplier *. Float.sin (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.);
camera_xyz.y <- camera_xyz.y +. speed_multiplier ; | 'p' -> vy := -. speed_multiplier_Y *. 2.5
if b && ( | 'm' -> vx := 0. ; vz := 0.
is_collision_global_2 hash cx cy cz | 'a' -> camera_angle_y := !camera_angle_y + 15
) then begin Stdlib.print_endline "Nope (m)" ; move_cam_hash_2 hash cx cy cz false 'p' end | 'e' -> camera_angle_y := !camera_angle_y - 15
| 'a' -> camera_angle_y := !camera_angle_y + 1
| 'e' -> camera_angle_y := !camera_angle_y - 1
| _ -> () ;; | _ -> () ;;
let init_chunk_all hash mem ch_x ch_y ch_z = let init_chunk_all hash mem ch_x ch_y ch_z =
@ -1676,34 +1684,76 @@ let draw_inventories () =
draw_integer_alignedleft 40 (__height__ - (240 + 50 * (Array.length playerOreInventory -1 -i))) playerOreInventory.(i) 20 draw_integer_alignedleft 40 (__height__ - (240 + 50 * (Array.length playerOreInventory -1 -i))) playerOreInventory.(i) 20
done ;; done ;;
exception CannotPass ;; (* ----------------------------------------| Motion-related functions |---------------------------------------- *)
let move_auto_y hash redraw =
try let move_all_at_once hash redraw =
let (cx, cy, cz) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in let (cx, cy, cz) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in
for i = 1 to 15 do let do_x = ref (!vx <> 0.)
camera_xyz.y <- camera_xyz.y +. !vy /. 15.; and do_y = ref (!vy <> 0.)
and do_z = ref (!vz <> 0.) in
(*Printf.printf "[%b %b %b]\n" !do_x !do_y !do_z ;*)
for i = 1 to precision do
(**)
if !do_x then begin
camera_xyz.x <- camera_xyz.x +. !vx /. precisionf ;
let new_cx = ctcf_one (-. camera_xyz.x) in
if is_collision_global_G hash new_cx cy cz then begin
if i <> 1 then
redraw := true;
camera_xyz.x <- camera_xyz.x -. !vx /. precisionf ;
do_x := false;
vx := 0. *. sign !vx
end
end;
(**)
if !do_y then begin
camera_xyz.y <- camera_xyz.y +. !vy /. precisionf ;
let new_cy = ctcf_one (-. camera_xyz.y) in let new_cy = ctcf_one (-. camera_xyz.y) in
if is_collision_global_G hash cx new_cy cz then begin if is_collision_global_G hash cx new_cy cz then begin
if i <> 1 then if i <> 1 then
redraw := true; redraw := true;
camera_xyz.y <- camera_xyz.y -. !vy /. 15. ; camera_xyz.y <- camera_xyz.y -. !vy /. precisionf ;
raise CannotPass do_y := false;
vy := 0. *. sign !vy
end end
else end;
() (**)
done ; if !do_z then begin
camera_xyz.z <- camera_xyz.z -. !vz /. precisionf ;
let new_cz = ctcf_one (camera_xyz.z) in
if is_collision_global_G hash cx cy new_cz then begin
if i <> 1 then
redraw := true; redraw := true;
() camera_xyz.z <- camera_xyz.z +. !vz /. precisionf ;
with do_z := false;
| CannotPass -> vy := 0.; () ;; vz := 0. *. sign !vz
end
end
done;
if !do_x || !do_y || !do_z then
redraw := true ;;
let friction dt =
vx := !vx -. !vx *. f *. dt ;
if absf !vx < vel_epsilon then vx := 0. ;
let update_gravity dt hash redraw = vy := !vy -. !vy *. f *. dt ;
if absf !vy < vel_epsilon then vy := 0. ;
vz := !vz -. !vz *. f *. dt ;
if absf !vz < vel_epsilon then vz := 0. ;;
let update_velocities dt hash redraw =
(* move the player from T = t to T = t + dt *)
vy := !vy +. gravity *. dt ; vy := !vy +. gravity *. dt ;
Printf.printf "{%f, %f, %f}" !vx !vy !vz; (*Printf.printf "{%f, %f, %f}" !vx !vy !vz;
Stdlib.print_endline " "; Stdlib.print_endline " " ;*)
move_auto_y hash redraw ;; move_all_at_once hash redraw ;
friction dt ;;
(* ----------------------------------------| Main function |---------------------------------------- *)
let gray = rgb 128 128 128 ;;
let play_dos laby = let play_dos laby =
try try
Stdlib.print_endline "Building terrain..."; Stdlib.print_endline "Building terrain...";
@ -1715,8 +1765,10 @@ let play_dos laby =
let memory = Hashtbl.create 100 in let memory = Hashtbl.create 100 in
let dyna = dyn_create {flag = "spawn" ; cube = create_cube 0 0 0 1 ; red = 30 ; green = 30 ; blue = 30} in let dyna = dyn_create {flag = "spawn" ; cube = create_cube 0 0 0 1 ; red = 30 ; green = 30 ; blue = 30} in
Hashtbl.add hash (0, 0, 0) dyna ; dyn_append dyna {flag = "spawn" ; cube = create_rect 0 0 0 chunk_size 1 chunk_size ; red = 234 ; green = 100 ; blue = 234} ;
Hashtbl.add hash (0, 0, 0) dyna ; (* set the spawning chunk to be empty with a bottom platform *)
Hashtbl.add memory (0, 0, 0) 1; Hashtbl.add memory (0, 0, 0) 1;
(* (*
N/A = unloaded chunk N/A = unloaded chunk
<0 = failed structure gen <0 = failed structure gen
@ -1732,9 +1784,9 @@ let play_dos laby =
and ch_y = ref 0 and ch_y = ref 0
and ch_z = ref 0 in and ch_z = ref 0 in
camera_xyz.x <- (-. 2.) ; camera_xyz.x <- (-. 0.5 *. (float_of_int chunk_size)) ;
camera_xyz.y <- (-. 2.) ; camera_xyz.y <- (-. 0.5 *. (float_of_int chunk_size)) ;
camera_xyz.z <- 2. ; camera_xyz.z <- 0.5 *. (float_of_int chunk_size) ;
while true do while true do
let time_s = Unix.gettimeofday() in let time_s = Unix.gettimeofday() in
@ -1759,18 +1811,35 @@ let play_dos laby =
let usr_input = get1char_plus () in let usr_input = get1char_plus () in
if usr_input <> '@' then begin if usr_input <> '@' then begin
Stdlib.print_endline "EEEEE"; (*Stdlib.print_endline "EEEEE";*)
for i = 0 to 15 do (*for i = 0 to 15 do
move_cam_hash_2 hash !ch_x !ch_y !ch_z true usr_input move_cam_hash_2 hash !ch_x !ch_y !ch_z true usr_input
done; done;*)
add_velocity usr_input ;
redraw := true redraw := true
end; end;
Unix.sleepf 0.001;
let time_e = Unix.gettimeofday() in let time_e = Unix.gettimeofday() in
let delta_t = time_e -. time_s in
update_velocities delta_t hash redraw ;
if (1. /. max_fps) -. delta_t > 0. then begin
Unix.sleepf (1. /. max_fps -. delta_t);
set_color black ;
fill_ellipse (__width__ - 100) (__height__ - 30) 100 40 ;
set_color gray ;
draw_integer_alignedleft (__width__ - 150) (__height__ - 30) (int_of_float max_fps) 25
end
else begin
set_color black ;
fill_ellipse (__width__ - 100) (__height__ - 30) 100 40 ;
set_color gray ;
draw_integer_alignedleft (__width__ - 150) (__height__ - 30) (int_of_float (1. /. delta_t)) 25
end
(*Printf.printf "[%f]" (time_e -. time_s); (*Printf.printf "[%f]" (time_e -. time_s);
Stdlib.print_endline " ";*) Stdlib.print_endline " ";*)
update_gravity (time_e -. time_s) hash redraw
done ; done ;
() ()
with with

BIN
display.o

Binary file not shown.

BIN
stone.bmp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 346 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 189 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B