From 9712ec712cc6a57d416ef2b1e86d15ef161016df Mon Sep 17 00:00:00 2001 From: rodri Date: Fri, 3 Mar 2023 11:18:16 +0000 Subject: implemented toroidal warping. set a default font. --- dat.h | 1 + musw.c | 3 ++- muswd.c | 4 +++- todo | 5 +++-- universe.c | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/dat.h b/dat.h index 26755c5..9a27f17 100644 --- a/dat.h +++ b/dat.h @@ -141,6 +141,7 @@ struct Universe double t, timeacc; void (*step)(Universe*, double); + void (*collide)(Universe*); void (*reset)(Universe*); }; diff --git a/musw.c b/musw.c index b7ea5e9..32bf155 100644 --- a/musw.c +++ b/musw.c @@ -41,6 +41,7 @@ Image *skymap; Channel *ingress; Channel *egress; NetConn netconn; +char deffont[] = "/lib/font/bit/pelm/unicode.9.font"; char winspec[32]; int doghosting; int debug; @@ -583,7 +584,7 @@ threadmain(int argc, char *argv[]) snprint(winspec, sizeof winspec, "-dx %d -dy %d", SCRWB, SCRHB); if(newwindow(winspec) < 0) sysfatal("newwindow: %r"); - if(initdraw(nil, nil, nil) < 0) + if(initdraw(nil, deffont, "musw") < 0) sysfatal("initdraw: %r"); if((mc = initmouse(nil, screen)) == nil) sysfatal("initmouse: %r"); diff --git a/muswd.c b/muswd.c index 0813e48..fd4f183 100644 --- a/muswd.c +++ b/muswd.c @@ -352,8 +352,10 @@ partywalk: player->oldkdown = player->kdown; } - while(p->u->timeacc >= Δt) + while(p->u->timeacc >= Δt){ p->u->step(p->u, Δt); + p->u->collide(p->u); + } } broadcaststate(); diff --git a/todo b/todo index 6ebcaa8..bfc0a3d 100644 --- a/todo +++ b/todo @@ -1,7 +1,7 @@ [ ] collision detection -[ ] toroidal warping +[✓] toroidal warping [ ] respect bullets's ttl - [ ] communicate this event with the clients + [ ] communicate this event to the clients [ ] explode when the time comes [ ] fuel consumption [ ] hyperjump @@ -13,3 +13,4 @@ [ ] waiting for a player [ ] main game [ ] reduce the amount of data sent on every NSsimstate packet +[ ] the client must try to connect continously diff --git a/universe.c b/universe.c index cf310ef..0b3028f 100644 --- a/universe.c +++ b/universe.c @@ -70,6 +70,38 @@ universe_step(Universe *u, double Δt) u->t += Δt; } +static void +warp(Particle *p) +{ + Rectangle r; + + r = Rect(-SCRW/2, -SCRH/2, SCRW/2, SCRH/2); + + if(p->p.x < r.min.x) + p->p.x = r.max.x; + if(p->p.y < r.min.y) + p->p.y = r.max.y; + if(p->p.x > r.max.x) + p->p.x = r.min.x; + if(p->p.y > r.max.y) + p->p.y = r.min.y; +} + +/* collision resolution */ +static void +universe_collide(Universe *u) +{ + Ship *s; + Bullet *b; + + for(s = u->ships; s < u->ships+nelem(u->ships); s++){ + for(b = s->rounds; b < s->rounds+nelem(s->rounds); b++){ + warp(b); + } + warp(s); + } +} + static void universe_reset(Universe *u) { @@ -130,6 +162,7 @@ newuniverse(void) u = emalloc(sizeof(Universe)); memset(u, 0, sizeof *u); u->step = universe_step; + u->collide = universe_collide; u->reset = universe_reset; return u; } -- cgit v1.2.3