Refactored client event if-else cascade to switch statement

This commit is contained in:
Ben Deutsch 2017-03-17 20:56:04 +01:00 committed by paramat
parent d3131aeae7
commit e7246d3f0d

@ -3046,7 +3046,10 @@ void Game::processClientEvents(CameraOrientation *cam)
for ( ; event.type != CE_NONE; event = client->getClientEvent()) { for ( ; event.type != CE_NONE; event = client->getClientEvent()) {
if (event.type == CE_PLAYER_DAMAGE && client->getHP() != 0) { switch (event.type) {
case CE_PLAYER_DAMAGE:
if (client->getHP() == 0)
break;
if (client->moddingEnabled()) { if (client->moddingEnabled()) {
client->getScript()->on_damage_taken(event.player_damage.amount); client->getScript()->on_damage_taken(event.player_damage.amount);
} }
@ -3058,12 +3061,15 @@ void Game::processClientEvents(CameraOrientation *cam)
player->hurt_tilt_strength = player->hurt_tilt_strength =
rangelim(event.player_damage.amount / 4, 1.0, 4.0); rangelim(event.player_damage.amount / 4, 1.0, 4.0);
MtEvent *e = new SimpleTriggerEvent("PlayerDamage"); client->event()->put(new SimpleTriggerEvent("PlayerDamage"));
client->event()->put(e); break;
} else if (event.type == CE_PLAYER_FORCE_MOVE) {
case CE_PLAYER_FORCE_MOVE:
cam->camera_yaw = event.player_force_move.yaw; cam->camera_yaw = event.player_force_move.yaw;
cam->camera_pitch = event.player_force_move.pitch; cam->camera_pitch = event.player_force_move.pitch;
} else if (event.type == CE_DEATHSCREEN) { break;
case CE_DEATHSCREEN:
// This should be enabled for death formspec in builtin // This should be enabled for death formspec in builtin
client->getScript()->on_death(); client->getScript()->on_death();
@ -3071,8 +3077,9 @@ void Game::processClientEvents(CameraOrientation *cam)
runData.damage_flash = 0; runData.damage_flash = 0;
player->hurt_tilt_timer = 0; player->hurt_tilt_timer = 0;
player->hurt_tilt_strength = 0; player->hurt_tilt_strength = 0;
break;
} else if (event.type == CE_SHOW_FORMSPEC) { case CE_SHOW_FORMSPEC:
if (*(event.show_formspec.formspec) == "") { if (*(event.show_formspec.formspec) == "") {
if (current_formspec && ( *(event.show_formspec.formname) == "" || *(event.show_formspec.formname) == cur_formname) ){ if (current_formspec && ( *(event.show_formspec.formname) == "" || *(event.show_formspec.formname) == cur_formname) ){
current_formspec->quitMenu(); current_formspec->quitMenu();
@ -3088,22 +3095,32 @@ void Game::processClientEvents(CameraOrientation *cam)
cur_formname = *(event.show_formspec.formname); cur_formname = *(event.show_formspec.formname);
} }
delete(event.show_formspec.formspec); delete event.show_formspec.formspec;
delete(event.show_formspec.formname); delete event.show_formspec.formname;
} else if (event.type == CE_SHOW_LOCAL_FORMSPEC) { break;
case CE_SHOW_LOCAL_FORMSPEC:
{
FormspecFormSource *fs_src = new FormspecFormSource(*event.show_formspec.formspec); FormspecFormSource *fs_src = new FormspecFormSource(*event.show_formspec.formspec);
LocalFormspecHandler *txt_dst = new LocalFormspecHandler(*event.show_formspec.formname, client); LocalFormspecHandler *txt_dst = new LocalFormspecHandler(*event.show_formspec.formname, client);
create_formspec_menu(&current_formspec, client, device, &input->joystick, create_formspec_menu(&current_formspec, client, device, &input->joystick,
fs_src, txt_dst); fs_src, txt_dst);
}
delete event.show_formspec.formspec; delete event.show_formspec.formspec;
delete event.show_formspec.formname; delete event.show_formspec.formname;
} else if ((event.type == CE_SPAWN_PARTICLE) || break;
(event.type == CE_ADD_PARTICLESPAWNER) ||
(event.type == CE_DELETE_PARTICLESPAWNER)) { case CE_SPAWN_PARTICLE:
case CE_ADD_PARTICLESPAWNER:
case CE_DELETE_PARTICLESPAWNER:
client->getParticleManager()->handleParticleEvent(&event, client, client->getParticleManager()->handleParticleEvent(&event, client,
smgr, player); smgr, player);
} else if (event.type == CE_HUDADD) { break;
case CE_HUDADD:
{
u32 id = event.hudadd.id; u32 id = event.hudadd.id;
HudElement *e = player->getHud(id); HudElement *e = player->getHud(id);
if (e != NULL) { if (e != NULL) {
@ -3135,6 +3152,7 @@ void Game::processClientEvents(CameraOrientation *cam)
u32 new_id = player->addHud(e); u32 new_id = player->addHud(e);
//if this isn't true our huds aren't consistent //if this isn't true our huds aren't consistent
sanity_check(new_id == id); sanity_check(new_id == id);
}
delete event.hudadd.pos; delete event.hudadd.pos;
delete event.hudadd.name; delete event.hudadd.name;
@ -3144,12 +3162,19 @@ void Game::processClientEvents(CameraOrientation *cam)
delete event.hudadd.offset; delete event.hudadd.offset;
delete event.hudadd.world_pos; delete event.hudadd.world_pos;
delete event.hudadd.size; delete event.hudadd.size;
} else if (event.type == CE_HUDRM) { break;
case CE_HUDRM:
{
HudElement *e = player->removeHud(event.hudrm.id); HudElement *e = player->removeHud(event.hudrm.id);
if (e != NULL) if (e != NULL)
delete(e); delete e;
} else if (event.type == CE_HUDCHANGE) { }
break;
case CE_HUDCHANGE:
{
u32 id = event.hudchange.id; u32 id = event.hudchange.id;
HudElement *e = player->getHud(id); HudElement *e = player->getHud(id);
@ -3206,12 +3231,15 @@ void Game::processClientEvents(CameraOrientation *cam)
e->size = *event.hudchange.v2s32data; e->size = *event.hudchange.v2s32data;
break; break;
} }
}
delete event.hudchange.v3fdata; delete event.hudchange.v3fdata;
delete event.hudchange.v2fdata; delete event.hudchange.v2fdata;
delete event.hudchange.sdata; delete event.hudchange.sdata;
delete event.hudchange.v2s32data; delete event.hudchange.v2s32data;
} else if (event.type == CE_SET_SKY) { break;
case CE_SET_SKY:
sky->setVisible(false); sky->setVisible(false);
if (skybox) { if (skybox) {
@ -3245,10 +3273,18 @@ void Game::processClientEvents(CameraOrientation *cam)
delete event.set_sky.bgcolor; delete event.set_sky.bgcolor;
delete event.set_sky.type; delete event.set_sky.type;
delete event.set_sky.params; delete event.set_sky.params;
} else if (event.type == CE_OVERRIDE_DAY_NIGHT_RATIO) { break;
bool enable = event.override_day_night_ratio.do_override;
u32 value = event.override_day_night_ratio.ratio_f * 1000; case CE_OVERRIDE_DAY_NIGHT_RATIO:
client->getEnv().setDayNightRatioOverride(enable, value); client->getEnv().setDayNightRatioOverride(
event.override_day_night_ratio.do_override,
event.override_day_night_ratio.ratio_f * 1000);
break;
default:
// unknown or unhandled type
break;
} }
} }
} }