Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce popup text overlapping using MessagesMap #635

Merged
merged 4 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/screen/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ fn make_gui(context: &mut Context) -> ZResult<ui::Gui<Message>> {
Ok(gui)
}

#[derive(PartialEq, Copy, Clone)]
enum CommandOrigin {
Player,
Internal,
}

#[derive(Debug)]
pub struct Battle {
font: graphics::Font,
Expand Down Expand Up @@ -452,7 +458,7 @@ impl Battle {
self.deselect()?;
let command = command::EndTurn.into();
let mut actions = Vec::new();
actions.push(self.do_command_inner(context, &command));
actions.push(self.do_command_inner(context, &command, CommandOrigin::Internal));
actions.push(self.do_ai(context));
self.add_actions(actions);
Ok(())
Expand All @@ -463,8 +469,8 @@ impl Battle {
let mut actions = Vec::new();
while let Some(command) = self.ai.command(&self.state) {
trace!("AI: command = {:?}", command);
actions.push(self.do_command_inner(context, &command));
actions.push(action::Sleep::new(time_s(0.3)).boxed());
actions.push(self.do_command_inner(context, &command, CommandOrigin::Internal));
actions.push(action::Sleep::new(time_s(0.2)).boxed());
if let command::Command::EndTurn(_) = command {
break;
}
Expand Down Expand Up @@ -499,23 +505,32 @@ impl Battle {
&mut self,
context: &mut Context,
command: &command::Command,
origin: CommandOrigin,
) -> Box<dyn Action> {
trace!("do_command_inner: {:?}", command);
self.view.messages_map_mut().clear();
let mut actions = Vec::new();
let state = &mut self.state;
let view = &mut self.view;
battle::execute(state, command, &mut |state, event, phase| {
let action = visualize::visualize(state, view, context, event, phase)
.expect("Can't visualize the event");
view.messages_map_mut().update(action.duration());
actions.push(action);
if origin != CommandOrigin::Player {
let actual_sleep_duration = view.messages_map().total_duration().mul_f32(0.3);
actions.push(action::Sleep::new(actual_sleep_duration).boxed());
view.messages_map_mut().update(actual_sleep_duration);
}
})
.expect("Can't execute command");
action::Sequence::new(actions).boxed()
}

fn do_command(&mut self, context: &mut Context, command: &command::Command) {
let action = self.do_command_inner(context, command);
let action = self.do_command_inner(context, command, CommandOrigin::Player);
self.add_action(action);
self.view.messages_map_mut().clear();
}

fn add_actions(&mut self, actions: Vec<Box<dyn Action>>) {
Expand Down Expand Up @@ -664,6 +679,7 @@ impl Battle {
self.try_move_selected_agent(context, pos);
}
}
self.view.messages_map_mut().clear();
Ok(())
}

Expand Down
73 changes: 72 additions & 1 deletion src/screen/battle/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
self, ability::Ability, command, component::ObjType, execute::hit_chance, movement,
state, Id, Jokers, Moves, State, TileType,
},
map::{self, Distance, HexMap, PosHex},
map::{self, Dir, Distance, HexMap, PosHex},
utils::roll_dice,
},
geom::{self, hex_to_point},
Expand Down Expand Up @@ -136,6 +136,67 @@ impl Images {
}
}

#[derive(Debug)]
pub struct MessagesMap {
map: HexMap<Option<Duration>>,
total_duration: Duration,
}

impl MessagesMap {
pub fn new(radius: Distance) -> Self {
Self {
map: HexMap::new(radius),
total_duration: Duration::from_secs(0),
}
}

pub fn delay_at(&self, pos: PosHex) -> Option<Duration> {
self.map.tile(pos)
}

pub fn total_duration(&self) -> Duration {
self.total_duration
}

pub fn update(&mut self, dtime: Duration) {
for pos in self.map.iter() {
let new_duration = match self.map.tile(pos) {
Some(t) if t > dtime => Some(t - dtime),
_ => None,
};
self.map.set_tile(pos, new_duration);
}
}

pub fn clear(&mut self) {
for pos in self.map.iter() {
self.map.set_tile(pos, None);
}
self.total_duration = Duration::from_secs(0);
}

fn mark_tile_as_busy(&mut self, pos: PosHex, duration: Duration) {
if !self.map.is_inboard(pos) {
return;
}
let combined_duration = match self.delay_at(pos) {
Some(current_duration) => current_duration + duration,
None => duration,
};
self.map.set_tile(pos, Some(combined_duration));
self.total_duration = self.total_duration.max(combined_duration);
}

pub fn register_message_at(&mut self, pos: PosHex, duration: Duration) {
assert!(self.map.is_inboard(pos));
let duration = duration.mul_f32(0.5);
self.mark_tile_as_busy(pos, duration);
// Also mark neighbors to the left and to the right as busy.
self.mark_tile_as_busy(Dir::get_neighbor_pos(pos, Dir::SouthEast), duration);
self.mark_tile_as_busy(Dir::get_neighbor_pos(pos, Dir::NorthWest), duration);
}
}

#[derive(Debug)]
pub struct BattleView {
font: Font,
Expand All @@ -145,6 +206,7 @@ pub struct BattleView {
sprites: Sprites,
images: Images,
sprite_info: HashMap<ObjType, SpriteInfo>,
messages_map: MessagesMap,
}

impl BattleView {
Expand Down Expand Up @@ -183,6 +245,7 @@ impl BattleView {
tile_size,
images,
sprite_info,
messages_map: MessagesMap::new(map_radius),
})
}

Expand All @@ -194,6 +257,14 @@ impl BattleView {
&self.images
}

pub fn messages_map(&self) -> &MessagesMap {
&self.messages_map
}

pub fn messages_map_mut(&mut self) -> &mut MessagesMap {
&mut self.messages_map
}

pub fn message(&mut self, context: &mut Context, pos: PosHex, text: &str) -> ZResult {
let action = visualize::message(self, context, pos, text)?;
self.add_action(action);
Expand Down
Loading