From c1b4c692565cfc5aa4ad1f47f7a3a4baf0960946 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Sun, 8 Jan 2023 13:14:03 -0800 Subject: [PATCH] Initial landing gear (no detach) --- Flight.vcxproj | 3 +- Flight.vcxproj.user | 2 +- buildsettings.h | 2 +- flight.rdbg | Bin 1141 -> 1329 bytes gamestate.c | 106 ++++++++++++++++++++++++++++++++++++++-- loaded/landing_gear.png | Bin 0 -> 445 bytes main.c | 6 +++ types.h | 6 +++ 8 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 loaded/landing_gear.png diff --git a/Flight.vcxproj b/Flight.vcxproj index 26ad283..3b33e41 100644 --- a/Flight.vcxproj +++ b/Flight.vcxproj @@ -177,12 +177,13 @@ NDEBUG;RELEASE;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)\thirdparty\enet\include;$(ProjectDir)\thirdparty\Chipmunk2D\include\chipmunk;$(ProjectDir)\thirdparty;$(ProjectDir)\thirdparty\Chipmunk2D\include;$(ProjectDir)\thirdparty\minilzo;$(ProjectDir)\thirdparty\opus\include + false Console true true - true + DebugFull $(CoreLibraryDependencies);%(AdditionalDependencies);Ws2_32.lib;winmm.lib;$(ProjectDir)\thirdparty\opus\win32\VS2015\x64\Release\opus.lib diff --git a/Flight.vcxproj.user b/Flight.vcxproj.user index a8c5a48..840ad31 100644 --- a/Flight.vcxproj.user +++ b/Flight.vcxproj.user @@ -4,7 +4,7 @@ false - --host + host=true WindowsLocalDebugger diff --git a/buildsettings.h b/buildsettings.h index 875321d..455f21e 100644 --- a/buildsettings.h +++ b/buildsettings.h @@ -26,7 +26,7 @@ #define DEBUG_TOOLS #define CHIPMUNK_INTEGRITY_CHECK // #define FAT_THRUSTERS -#define NO_GRAVITY +//#define NO_GRAVITY // #define NO_SUNS #else diff --git a/flight.rdbg b/flight.rdbg index 2ea440112ee9e888a958aa6d42f365c925b07d86..41aeac452a28c77ac12c8e522350345e185db978 100644 GIT binary patch delta 204 zcmey$v5{*+0V@Xs1B2JZjgGv|`FX`9MTwbtC3?vg6E_-cTzZ7Do)su70>nxADV4f* z$#5k;nZ+esKyic!NL8w?T~1mP(``HYI6_`2h2d08N=pz hOu>wdtdkX4R3;m+@biE~85tQtZiWepOm<~4003;NG%f%D delta 76 zcmdnU^_62n0V_KL1B1onjf|?3a~TCU{@lkX#RwD;0^-u*)cBH&%;NZz)RNTXlFa

landed_constraint = NULL; + cpSpaceRemoveConstraint(cpBodyGetSpace(body), constraint); + cpConstraintFree(constraint); +} + void destroy_child_shape(cpBody *body, cpShape *shape, void *data) { GameState *gs = (GameState *)data; @@ -447,14 +454,22 @@ void entity_memory_free(GameState *gs, Entity *e) cpShapeFree(e->shape); e->shape = NULL; } + if (e->landed_constraint != NULL) + { + cpSpaceRemoveConstraint(gs->space, e->landed_constraint); + cpConstraintFree(e->landed_constraint); + e->landed_constraint = NULL; + } if (e->body != NULL) { + // need to do this here because body which constraint is attached to can be destroyed + // NOT TRUE: can't do this here because the handle to the constraint cannot be set to NULL. Constraints are freed by the entities that own them + cpBodyEachConstraint(e->body, destroy_constraints, NULL); cpBodyEachShape(e->body, destroy_child_shape, (void *)gs); cpSpaceRemoveBody(gs->space, e->body); cpBodyFree(e->body); e->body = NULL; } - Entity *front_of_free_list = get_entity(gs, gs->free_list); if (front_of_free_list != NULL) flight_assert(!front_of_free_list->exists); @@ -518,6 +533,12 @@ void create_body(GameState *gs, Entity *e) cpBodySetUserData(e->body, (void *)e); } +// must always call this after creating a constraint +void on_create_constraint(Entity *e, cpConstraint* c) +{ + cpConstraintSetUserData(c, (cpDataPointer)e); +} + cpVect player_vel(GameState *gs, Entity *player) { flight_assert(player->is_player); @@ -627,8 +648,6 @@ void create_player(Player *player) #endif } - - void create_orb(GameState *gs, Entity *e) { create_body(gs, e); @@ -1606,6 +1625,41 @@ SerMaybeFailure ser_entity(SerState *ser, GameState *gs, Entity *e) case BoxMissileLauncher: SER_MAYBE_RETURN(ser_f(ser, &e->missile_construction_charge)); break; + case BoxLandingGear: + { + bool is_null = e->landed_constraint == NULL; + SER_VAR(&is_null); + if (!is_null) + { + EntityID from = {0}; + EntityID to = {0}; + cpVect pin = {0}; + if (ser->serializing) + { + from = get_id(gs, cp_body_entity(cpConstraintGetBodyA(e->landed_constraint))); + to = get_id(gs, cp_body_entity(cpConstraintGetBodyB(e->landed_constraint))); + pin = cpPivotJointGetAnchorA(e->landed_constraint); + } + SER_MAYBE_RETURN(ser_entityid(ser, &from)); + SER_MAYBE_RETURN(ser_entityid(ser, &to)); + SER_MAYBE_RETURN(ser_V2(ser, &pin)); + if (!ser->serializing) + { + Entity *from_entity = get_entity(gs, from); + Entity *to_entity = get_entity(gs, to); + if (from_entity == NULL || to_entity == NULL) + { + } + else + { + e->landed_constraint = cpPivotJointNew(from_entity->body, to_entity->body, pin); + cpSpaceAddConstraint(gs->space, e->landed_constraint); + on_create_constraint(e, e->landed_constraint); + } + } + } + break; + } default: break; } @@ -2434,7 +2488,7 @@ void create_initial_world(GameState *gs) create_hard_shell_station(gs, (cpVect){-5.0, 5.0}, BoxCloaking); #endif -#if 1 // scanner box +#if 0 // scanner box bool indestructible = false; enum CompassRotation rot = Right; { @@ -2458,6 +2512,29 @@ void create_initial_world(GameState *gs) #endif +#if 1 // landing gear box + bool indestructible = false; + cpVect from = cpv(4.0 * BOX_SIZE, 0.0); + enum CompassRotation rot = Right; + { + Entity *grid = new_entity(gs); + grid_create(gs, grid); + entity_set_pos(grid, from); + BOX_AT_TYPE(grid, ((cpVect){0.0, 0.0}), BoxLandingGear); + // BOX_AT(grid, ((cpVect){0.0, -BOX_SIZE})); + // BOX_AT_TYPE(grid, ((cpVect){BOX_SIZE, 0.0}), BoxMerge); + entity_ensure_in_orbit(gs, grid); + cpBodySetVelocity(grid->body, cpv(0.1, 0.0)); + } + { + Entity *grid = new_entity(gs); + grid_create(gs, grid); + entity_set_pos(grid, cpvadd(from, cpv(2.0 * BOX_SIZE, 0.0))); + BOX_AT_TYPE(grid, ((cpVect){0.0, 0.0}), BoxHullpiece); + entity_ensure_in_orbit(gs, grid); + } +#endif + #if 0 // merge box bool indestructible = false; double theta = deg2rad(65.0); @@ -3415,6 +3492,27 @@ void process(struct GameState *gs, double dt) cur_box->scanner_head_rotate += cur_box->scanner_head_rotate_speed * dt; cur_box->scanner_head_rotate = fmod(cur_box->scanner_head_rotate, 2.0 * PI); } + if (cur_box->box_type == BoxLandingGear) + { + if (cur_box->landed_constraint == NULL) + { + cpVect along = box_facing_vector(cur_box); + cpVect from = cpvadd(entity_pos(cur_box), cpvmult(along, BOX_SIZE / 2.0 + 0.03)); + cpVect to = cpvadd(from, cpvmult(along, LANDING_GEAR_MAX_DIST)); + + cpSegmentQueryInfo query_result = {0}; + cpShape *found = cpSpaceSegmentQueryFirst(gs->space, from, to, 0.0, FILTER_DEFAULT, &query_result); + if (found != NULL && cpShapeGetBody(found) != box_grid(cur_box)->body) + { + cpVect anchor = cpvadd(entity_pos(cur_box), cpvmult(along, BOX_SIZE / 2.0)); + cpBody *a = box_grid(cur_box)->body; + cpBody *b = cpShapeGetBody(found); + cur_box->landed_constraint = cpPivotJointNew(a, b, anchor); + cpSpaceAddConstraint(gs->space, cur_box->landed_constraint); + on_create_constraint(cur_box, cur_box->landed_constraint); + } + } + } } } } diff --git a/loaded/landing_gear.png b/loaded/landing_gear.png new file mode 100644 index 0000000000000000000000000000000000000000..8a6e462c1972cac7c61a76d573c9d2b5f1540879 GIT binary patch literal 445 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM1|%Pp+x`GjjKx9jP7LeL$-D%z%ROBjLp(Z@ zQx*tntcd+RzYz!~&UOE0&&n~PSHYO(KS7ppXrAk#PYA0L4n>Kw8{-Ns|x{CWL}d}qX6q8oAy zn3+}fY?SQbV{;b}y|V7_{vt>2E{TmR`&j3z=twF_|NHZ?@>qab@Bh`EhI1RwC`A2z z&6cb&o6SwJlXpYH>VkruHG7v`{O=$n+Z+_Lc)IF|lLj+m|8MIT+I(u$?)wk+DV$