prosperon/source/scripts/engine.scm

166 lines
4.4 KiB
Scheme
Raw Normal View History

(define-macro (time expr)
`(let ((start (*s7* 'cpu-time)))
(let ((res (list ,expr)))
(list (car res)
(- (*s7* 'cpu-time) start)))))
(define-macro (funcinfo func info)
`(*function* (funclet ,func) ,info))
(define-macro (funcloc func)
`(format #f "~A:~A" (funcinfo ,func 'file) (funcinfo ,func 'line)))
(define-macro (funcsrc func)
`(funcinfo ,func 'source))
2022-12-20 08:16:26 -06:00
(define (objects->string . objs)
2022-12-20 19:34:22 -06:00
(apply string-append (map (lambda (obj) (object->string obj #f)) objs)))
(define-bacro (glog data lvl)
(let ((file (port-filename))
(line (port-line-number)))
`(log ,lvl ,data
(if (equal? ,file "*stdin*") (*function* (outlet (curlet)) 'file) ,file)
(if (equal? ,file "*stdin*") (*function* (outlet (curlet)) 'line) ,line))))
(define-bacro (loginfo . data)
`(glog (objects->string ,@data) 0))
(define-bacro (logwarn . data)
`(glog (objects->string ,@data) 1))
(define-bacro (logerr . data)
`(glog (objects->string ,@data) 2))
(define-bacro (logcrit . data)
`(glog (objects->string ,@data) 3))
(define (set_fps fps) (settings_cmd 0 (/ 1 fps)))
(define (set_update fps) (settings_cmd 1 (/ 1 fps)))
(define (set_phys fps) (settings_cmd 2 (/ 1 fps)))
2022-12-22 16:58:06 -06:00
(define (zoom! amt) (settings_cmd 5 amt))
2022-12-24 13:18:06 -06:00
(define (win_fulltoggle) (win_cmd 0 0))
(define (win_fullscreen) (win_cmd 0 1))
(define (win_unfullscreen) (win_cmd 0 2))
(define (win_w) (win_cmd 0 3))
(define (win_h) (win_cmd 0 4))
(define (load_level s) (gen_cmd 0 s))
(define (load_prefab s) (gen_cmd 1 s))
2022-12-22 16:58:06 -06:00
(define (newobject) (sys_cmd 7))
2023-01-04 18:09:42 -06:00
(define (clear_level) (sys_cmd 9))
2022-12-29 04:26:21 -06:00
(define (fps) (sys_cmd 8))
(define (quit) (sys_cmd 0))
(define (exit) (quit))
(define (sound_play sound) (sound_cmd sound 0))
(define (sound_pause sound) (sound_cmd sound 1))
(define (sound_stop sound) (sound_cmd sound 2))
(define (sound_restart sound) (sound_cmd sound 3))
2022-12-19 09:12:34 -06:00
2022-12-29 17:44:19 -06:00
(define-macro (anim_play anim)
`(anim_cmd 0 body ,anim))
2022-12-20 19:34:22 -06:00
(define-macro (registertype type . expr)
2022-12-19 09:12:34 -06:00
(let ((f (gensym)))
`(begin
(define (,f) (begin . ,expr))
2022-12-20 19:34:22 -06:00
(register ,(case type
((update) 0)
((gui) 1)) ,f))))
(define-macro (update . expr)
2022-12-22 16:58:06 -06:00
(let ((f (gensym)))
`(begin
(define (,f dt) (begin . ,expr))
(register 0 ,f))))
2022-12-19 18:15:38 -06:00
2022-12-20 19:34:22 -06:00
(define-macro (gui . expr)
2022-12-24 13:18:06 -06:00
`(registertype gui
2022-12-27 12:52:25 -06:00
(let ((pos #(0 0))) ,@expr)))
2022-12-20 19:34:22 -06:00
2022-12-19 09:12:34 -06:00
(define-macro (while condition . body)
(let ((loop (gensym)))
`(let ,loop ()
(cond (,condition
(begin . ,body)
(,loop))))))
2022-12-19 18:15:38 -06:00
(define (clamp val min max)
(cond ((< val min) min)
((> val max) max)
(else val)))
(define (lerp s f dt)
(+ s (* (clamp dt 0 1) (- f s))))
(define (deg2rad deg)
(* deg 0.01745329252))
2022-12-26 20:57:45 -06:00
2022-12-23 13:48:29 -06:00
(define-macro (body_type! body type)
`(set_body ,body 1 ,(case type
((static) 2)
((dynamic) 0)
((kinematic) 1))))
2022-12-26 20:57:45 -06:00
(define (body_angle! body angle) (set_body body 0 angle))
(define (body_pos! body pos) (set_body body 2 pos))
(define (body_move! body vec) (set_body body 3 vec))
(define (body_flipx! body flip) (set_body body 5 flip))
(define (body_flipy! body flip) (set_body body 6 flip))
2022-12-20 08:16:26 -06:00
(define (gravity! x y) (phys_set 0 x y))
(define (b2i val) (if (eq? val #f) 0 1))
(define (dbg_draw_phys val) (settings_cmd 3 (b2i val)))
2022-12-22 16:58:06 -06:00
(define (timescale! val) (settings_cmd 4 val))
(define (sim_play) (sys_cmd 1))
(define (sim_stop) (sys_cmd 2))
(define (sim_pause) (sys_cmd 3))
(define (sim_step) (sys_cmd 4))
(define (sim_play?) (sys_cmd 5))
(define (sim_pause?) (sys_cmd 6))
2022-12-22 16:58:06 -06:00
(define (camera! body) (int_cmd 0 body))
(define (bodytype? body) (phys_q body 0))
2022-12-26 20:57:45 -06:00
(define-macro (register-phys type . expr)
2022-12-20 08:16:26 -06:00
(let ((f (gensym)))
`(begin
2022-12-28 16:50:54 -06:00
(define (,f hit norm) (begin . ,expr))
2022-12-26 20:57:45 -06:00
(phys_cmd body ,(case type
((collide) 0)
((separate) 3)) ,f))))
(define-macro (collide . expr)
2022-12-26 20:57:45 -06:00
`(register-phys collide ,@expr))
(define-macro (separate . expr)
2022-12-26 20:57:45 -06:00
`(register-phys separate ,@expr))
(define-macro (not! var)
`(set! ,var (not ,var)))
(define-macro* (define-script name (lets ()) . expr )
`(define* (,name (gameobject -1))
(let ((body gameobject)
,@lets)
,@expr
(curlet))))
2022-12-22 16:58:06 -06:00
(define-macro (input key . expr)
`(define (,(symbol "input_" (symbol->string key))) (begin . ,expr)))
2022-12-26 20:57:45 -06:00
(define-macro (+=! var amt)
2022-12-22 16:58:06 -06:00
`(set! ,var (+ ,var ,amt)))
2022-12-26 20:57:45 -06:00
(define-macro (-=! var amt)
2022-12-22 16:58:06 -06:00
`(set! ,var (- ,var ,amt)))
(define (attach-script script object)
(let-set! script 'body object))