void player_update(struct obj *self) { struct player_extra extra_ro = *(struct player_extra*)self->extra; struct player_extra *extra_rw = (struct player_extra*)self->extra; #if INPUT == KEYBOARD char *keys = extra_ro.keystate; #define RT 0.000000009 if (keys[SDL_SCANCODE_X]) { if (self->gun.fire) { Mix_PlayChannel(-1, extra_ro.gunSound, 0); self->gun.fire(&(self->gun)); } } if (keys[SDL_SCANCODE_Z]) { cpBodyApplyImpulseAtLocalPoint(self->body, cpv(-RT, 0.0), cpv(0, -32)); cpBodyApplyImpulseAtLocalPoint(self->body, cpv(RT, 0.0), cpv(0, 32)); } if (keys[SDL_SCANCODE_C]) { // cpBodySetVelocity(self->body, cpv(0, 0)); // cpBodySetAngularVelocity(self->body, 0.0); } if (keys[SDL_SCANCODE_V]) { cpBodyApplyImpulseAtLocalPoint(self->body, cpv(RT, 0.0), cpv(0, -32)); cpBodyApplyImpulseAtLocalPoint(self->body, cpv(-RT, 0.0), cpv(0, 32)); } /*#elif INPUT == CONTROLLER // SDL_GameControllerUpdate(); Sint16 cx = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX); if (abs(cx) < 6000) cx = 0; double dx = (abs(cx) / (double)32767.0) * (cx > 0 ? 1 : -1); Sint16 cy = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY); if (abs(cy) < 6000) cy = 0; double dy = (abs(cy) / (double)32767.0) * (cy > 0 ? 1 : -1); // printf("%d %f %d %f\n", cx, dx, cy, dy); self->position.y += (speed * dy); self->position.x += (speed * dx); }*/ #endif }
void RigidBody2D::AddImpulse(const Vector2f& impulse, const Vector2f& point, CoordSys coordSys) { switch (coordSys) { case CoordSys_Global: cpBodyApplyImpulseAtWorldPoint(m_handle, cpv(impulse.x, impulse.y), cpv(point.x, point.y)); break; case CoordSys_Local: cpBodyApplyImpulseAtLocalPoint(m_handle, cpv(impulse.x, impulse.y), cpv(point.x, point.y)); break; } }
int applykeys(Ship* s, bool* keylist) { if (keylist[0]) cpBodyApplyForceAtLocalPoint(s->body, cpv(0, 1e7), cpv(0.5, 0)); if (keylist[1]) cpBodyApplyForceAtLocalPoint(s->body, cpv(0, -1e6), cpv(0.5, 3)); if (keylist[2]) { cpBodyApplyForceAtLocalPoint(s->body, cpv(1e6, 0), cpv(0, 0)); cpBodyApplyForceAtLocalPoint(s->body, cpv(-1e6, 0), cpv(1, 3)); } if (keylist[3]) { cpBodyApplyForceAtLocalPoint(s->body, cpv(1e6, 0), cpv(0, 3)); cpBodyApplyForceAtLocalPoint(s->body, cpv(-1e6, 0), cpv(1, 0)); } if (keylist[4]) { cpBodyApplyForceAtLocalPoint(s->body, cpv(-1e6, 0), cpv(1, 3)); cpBodyApplyForceAtLocalPoint(s->body, cpv(-1e6, 0), cpv(1, 0)); } if (keylist[5]) { cpBodyApplyForceAtLocalPoint(s->body, cpv(1e6, 0), cpv(0, 3)); cpBodyApplyForceAtLocalPoint(s->body, cpv(1e6, 0), cpv(0, 0)); } if (keylist[6] && last_time_updated - s->last_fired > 1) { cpVect tip = cpBodyLocalToWorld(s->body, nosev + cpv(0, 0.1)); cpVect base = cpBodyLocalToWorld(s->body, cpv(0.5, 0)); cpVect newvel = cpvnormalize(tip - base) * shell_muzzle_vel + cpBodyGetVelocityAtLocalPoint(s->body, nosev); Shell* newshell = addshell(tip, newvel, cpBodyGetAngle(s->body)); cpBodyApplyImpulseAtLocalPoint(s->body, cpv(0, -1)*cpBodyGetMass(newshell->body)*shell_muzzle_vel, cpv(0.5, 3)); s->last_fired = last_time_updated; } return 0; }
void PhysicsBody::applyImpulse(const Vec2& impulse, const Vec2& offset) { cpBodyApplyImpulseAtLocalPoint(_cpBody, PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset)); }
struct gun instantiate_gun(struct obj *owner, const char *gunType, const char *bulletType) { // fprintf(stderr, "\n\n\n%s\n\n\n", bulletType); struct gun ret = {.bulletType = strdup(bulletType), .owner = owner, .cooldown = owner->room->ticks}; ret.fire = guntbl_find(&fires, gunType); return ret; } void machinegun_fire(struct gun *self) { if (self->owner->room->ticks - self->cooldown > 1) { struct obj *bullet = instantiate_object(self->bulletType, self->owner->objectVect, self->owner->room); cpBodySetPosition(bullet->body, cpBodyLocalToWorld(self->owner->body, cpv(0, -self->owner->radius - bullet->radius - 2))); cpBodySetMoment(bullet->body, cpMomentForCircle(10000.0, 0.0, bullet->radius*2, cpvzero)); cpBodySetAngle(bullet->body, cpBodyGetAngle(self->owner->body) + M_PI); ((struct bullet_extra*)bullet->extra)->speed = 20.0; ((struct bullet_extra*)bullet->extra)->owner = self->owner; cpBodyApplyImpulseAtLocalPoint(self->owner->body, cpv(0, 50.0), cpv(0, -(self->owner->radius * 2))); self->cooldown = self->owner->room->ticks; } } struct gunpair { const char *name; const gunmethod method; }; void register_guns(void) { struct gunpair pairs[] = {{"machine gun", machinegun_fire}}; for (size_t i = 0; i < ARRAY_LENGTH(pairs); i++) { char *currentName = pairs[i].name; gunmethod currentMethod = pairs[i].method; if ((!currentName && currentMethod) || (currentName && !currentMethod)) { CROAK("Unevenly sized methodpair array"); } guntbl_insert(&fires, currentName, currentMethod); } } void guntbl_insert(struct guntbl *table, const char *key, gunmethod method) { int offset = hash(key) % 32; if (table->keys[offset]) { if (strcmp(table->keys[offset], key) == 0 ) { table->methods[offset] = method; } else { if (!table->next) { table->next = malloc(sizeof(struct guntbl)); *table->next = (struct guntbl){.next = NULL}; } guntbl_insert(table->next, key, method); } } else {