void Explosion::explode() { if (state != STATE_WAITING) return; state = STATE_EXPLODING; set_action(hurt ? "default" : "pop", 1); sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action sprite->set_angle(graphicsRandom.randf(0, 360)); // a random rotation on the sprite to make explosions appear more random SoundManager::current()->play(hurt ? "sounds/explosion.wav" : "sounds/firecracker.ogg", get_pos()); // spawn some particles int pnumber = push ? 8 : 100; Vector accel = Vector(0, Sector::get().get_gravity()*100); Sector::get().add<Particles>( bbox.get_middle(), -360, 360, 450, 900, accel , pnumber, Color(.4f, .4f, .4f), 3, .8f, LAYER_OBJECTS-1); if (push) { Vector center = bbox.get_middle (); auto near_objects = Sector::get().get_nearby_objects (center, 10.0 * 32.0); for(auto& obj: near_objects) { Vector obj_vector = obj->get_bbox ().get_middle (); Vector direction = obj_vector - center; float distance = direction.norm (); /* If the distance is very small, for example because "obj" is the badguy * causing the explosion, skip this object. */ if (distance <= 1.0) continue; /* The force decreases with the distance squared. In the distance of one * tile (32 pixels) you will have a speed increase of 150 pixels/s. */ float force = 150.0f * 32.0f * 32.0f / (distance * distance); if (force > 200.0) force = 200.0; Vector add_speed = direction.unit () * force; auto player = dynamic_cast<Player *> (obj); if (player) { player->add_velocity (add_speed); } auto badguy = dynamic_cast<WalkingBadguy *> (obj); if (badguy && badguy->is_active()) { badguy->add_velocity (add_speed); } } /* for (i = 0 ... near_objects) */ } /* if (push) */ }
struct Velocities *AddVelocity2( int velocityvelocity, const Place *placement, struct Notes *note ) { int ret; return add_velocity(velocityvelocity, placement, note, &ret); }
int AddVelocity( int velocityvelocity, const Place *placement, struct Notes *note ){ int ret; add_velocity(velocityvelocity, placement, note, &ret); return ret; }
int main(int argc, char* argv[]) { parse_args(argc, argv); // Done differently depending on application nbody_init(num_bodies); gui_init("nbody1"); /* // Make some clumped bodies with initial velocity for a more interesting simulation for(i = 0; i < num_bodies/4; ++i) { body[i].pos.x = -50 + rand() % 20; body[i].pos.y = -50 + rand() % 20; body[i].pos.z = -50 + rand() % 20; body[i].vel.x = 0.05; } for(i = num_bodies/4; i < num_bodies/2; ++i) { body[i].pos.x = 30 + rand() % 20; body[i].pos.y = 30 + rand() % 20; body[i].pos.z = 30 + rand() % 20; body[i].vel.x = -0.05; }*/ float delta = 0.f; clock_t start = times(NULL); client_start(); int iterations = 0; while(gui_update()) { // simulate stuff delta += turbo; while(delta >= 1.f) { // random mass flip //if(allow_negative) // body[rand() % num_bodies].mass *= -1.f; calc_forces(); add_velocity(); delta -= 1.f; ++iterations; } // if there's a limit, count down and break if reached if(num_steps > 0 && (--num_steps == 0)) break; } clock_t stop = times(NULL); fputs(argv[0], stdout); int i; for(i = 1; i < argc; ++i) { fputc(' ', stdout); fputs(argv[i], stdout); } long ticks_per_sec = sysconf(_SC_CLK_TCK); printf("\n%d iterations.\n", iterations); clock_t time = stop - start; printf("elapsed: %f seconds.\navg: %f seconds per iteration.\n", ((float)(time))/ticks_per_sec, ((float)(time))/ticks_per_sec/iterations); client_exit(); gui_quit(); free(body); return 0; }