void Fighter::update() {
	if (health >= 100) {
		blood_particles->max_particles = 4;
		blood_particles->spawn_x = pos.x + 80;
		blood_particles->spawn_y = pos.y + 64;
	}

	if (type == PLAYER) {
		left_key = universe->input->left_key[player_id];
		right_key = universe->input->right_key[player_id];
		up_key = universe->input->up_key[player_id];
		down_key = universe->input->down_key[player_id];
		a_key = universe->input->a_key[player_id];
		b_key = universe->input->b_key[player_id];
	}else {
		//TODO implement ai here
	}

	if (invincible) {
		texture->set_colour(r, g, b,  .25f + ((sin(alpha_colour / 4) + 1) / 2.5f));
		++alpha_colour;
	}

	if (!respawning) {
		update_movement();
		update_damage();
		rect.x = pos.x + universe->camera->x + universe->camera->get_offset_x(pos.x) + offset_x;
		rect.y = pos.y + universe->camera->y + universe->camera->get_offset_y(pos.y) + offset_y;
		rect.w = width * universe->camera->scale; rect.h = height * universe->camera->scale;
		origin.x = rect.w / 2; origin.y = rect.h / 2;
		universe->renderer->render_transform(texture, &src_rect, &rect, rotation, &origin, flip);

		int min_bounds_x = universe->camera->min_bounds_x - 250;
		int max_bounds_x = universe->camera->max_bounds_x + 300;
		int min_bounds_y = universe->camera->min_bounds_y - 250;
		int max_bounds_y = universe->camera->max_bounds_y + 250;
		if (pos.x < min_bounds_x || pos.x > max_bounds_x || pos.y < min_bounds_y || pos.y > max_bounds_y) {
			int p_x = pos.x; int p_y = pos.y;
			if (p_x < min_bounds_x) { p_x = min_bounds_x; }else if (p_x > max_bounds_x) { p_x = max_bounds_x; }
			if (p_y < min_bounds_y) { p_y = min_bounds_y; }else if (p_y > max_bounds_y) { p_y = max_bounds_y; }

			universe->particles->create_particle_chunk(new ParticleEmitter(p_x, p_y, 
																		   0, 250, 250, true), CLOUD);
			universe->particles->create_particle_chunk(new ParticleEmitter(p_x, p_y, 
																		   0, 250, 250, true), BLOOD_CLOUD);

			respawning = true;
			health = 0;
			universe->game_ui->update_damage_text(id, health);

			//respawn after 2.5 seconds
			universe->timer->set_timer([this](void) {
				reset();
				respawning = false;
				enable_camera_view = true;
				pos.x = 100 + (rand() % (universe->map->map_width - 400));
				pos.y = -150;
				invincible = true;
				alpha_colour = 0;
				//turn off invincibility after 1 second
				universe->timer->set_timer([this](void) { invincible = false; texture->set_colour(r, g, b, 1);
				}, 2500);
			}, 2500);

			//turn off camera view after 1.5 seconds
			universe->timer->set_timer([this](void) {
				if (respawning) {
					enable_camera_view = false;
				}
			}, 1500);
		}
	}
}
示例#2
0
int run_game_loop(GameState * state) {
	int done = 0;
	int i = 0;

	/* Initialise gui */
	init_gui(SCREEN_WIDTH,SCREEN_HEIGHT);

	/* Load resources */
	init_sprite_cache();
	render_world_to_sprite(&state->world);
	update_hud(
		&state->hud, 
		&state->score,
		&state->mana,
		&state->money, 
		&state->world.castle->castle.health,
		&state->wave.wave_number,
		&state->play);

	/* Initialise game loop */
	init_game_loop(FPS);
	while (!done) {
		
			/* Get events */
			Event ev;
			wait_for_event(&ev);

			/* Event handlers */
			switch (ev.type) {
			case EVENT_TIMER:
				state->redraw = 1;
				if (!state->game_over) {
					if (*state->hud.play) {
						check_spells(state);
						check_enemy_wave(state);
						update_movement(state);
						do_tower_attacks(state);
					}
				}
				break;
			case EVENT_MOUSE_MOVE:
				mouse_move(&ev.mouseMoveEvent, state);
				break;
			case EVENT_MOUSE_DOWN:
				mouse_down(&ev.mouseDownEvent, state);
				break;
			case EVENT_MOUSE_UP:
				mouse_up(&ev.mouseUpEvent, state);
				break;
			case EVENT_DISPLAY_CLOSE:
				done = 1;
				break;
			} 

			/* Render only on timer event AND if all movement and logic was processed */
			if (state->redraw && all_events_processed()) { 
				render_game(state);
			}
	}
	/* Cleanup */
	cleanup_game_loop();
	cleanup_sprite_cache();
	return 0;
}