Skip to content

Commit 4700e01

Browse files
Dmytro Lysaipingw33n
authored andcommitted
[game] Skill use via croshair pick
1 parent dd0b9ab commit 4700e01

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

src/asset/frame/id/consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl FrameId {
280280
pub const SCRSWEST: FrameId = FrameId::Generic(Generic(0x6000_114));
281281
pub const SCRWEST: FrameId = FrameId::Generic(Generic(0x6000_115));
282282
pub const WAIT: FrameId = FrameId::Generic(Generic(0x6000_116));
283-
pub const CRSSHAIR: FrameId = FrameId::Generic(Generic(0x6000_117));
283+
pub const CROSSHAIR_ATTACK: FrameId = FrameId::Generic(Generic(0x6000_117));
284284
pub const PLUS: FrameId = FrameId::Generic(Generic(0x6000_118));
285285
pub const DESTROY: FrameId = FrameId::Generic(Generic(0x6000_119));
286286
pub const ACTPICK: FrameId = FrameId::Generic(Generic(0x6000_11a));
@@ -294,7 +294,7 @@ impl FrameId {
294294
pub const MVENUM: FrameId = FrameId::Generic(Generic(0x6000_122));
295295
pub const RELOAD: FrameId = FrameId::Generic(Generic(0x6000_123));
296296
pub const USET: FrameId = FrameId::Generic(Generic(0x6000_124));
297-
pub const CROSSUSE: FrameId = FrameId::Generic(Generic(0x6000_125));
297+
pub const CROSSHAIR_USE: FrameId = FrameId::Generic(Generic(0x6000_125));
298298
pub const USEON: FrameId = FrameId::Generic(Generic(0x6000_126));
299299
pub const WAIT2: FrameId = FrameId::Generic(Generic(0x6000_127));
300300
pub const FALLTEXT: FrameId = FrameId::Generic(Generic(0x6000_128));

src/game/state.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,9 @@ impl AppState for GameState {
12271227
None
12281228
};
12291229
self.handle_action(ui, objh, a);
1230+
},
1231+
ObjectPickKind::Skill(skill) => {
1232+
self.action_use_skill_on(skill, objh);
12301233
}
12311234
}
12321235
}
@@ -1341,7 +1344,7 @@ impl AppState for GameState {
13411344
if let Some(target) = target {
13421345
self.action_use_skill_on(skill, target);
13431346
} else {
1344-
// TODO
1347+
ui.widget_mut::<WorldView>(self.world_view).enter_skill_target_pick_mode(skill);
13451348
}
13461349
}
13471350
}

src/game/ui/world.rs

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use matches::matches;
12
use std::rc::Rc;
23
use std::cell::RefCell;
34
use std::time::{Duration, Instant};
@@ -21,7 +22,13 @@ use super::action_menu::{Action, Placement};
2122
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2223
enum PickMode {
2324
Hex,
24-
Object,
25+
Object(ObjectPickMode),
26+
}
27+
28+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29+
enum ObjectPickMode {
30+
Action,
31+
Skill(crate::asset::Skill),
2532
}
2633

2734
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -42,6 +49,7 @@ enum PickState {
4249
pub struct WorldView {
4350
world: Rc<RefCell<World>>,
4451
pick_mode: PickMode,
52+
saved_pick_mode: Option<PickMode>,
4553
hex_cursor: object::Handle,
4654
pub hex_cursor_style: HexCursorStyle,
4755
pub roof_visible: bool,
@@ -59,6 +67,7 @@ impl WorldView {
5967
Self {
6068
world,
6169
pick_mode: PickMode::Hex,
70+
saved_pick_mode: None,
6271
hex_cursor,
6372
hex_cursor_style: HexCursorStyle::Normal,
6473
roof_visible: false,
@@ -85,6 +94,11 @@ impl WorldView {
8594
}
8695
}
8796

97+
pub fn enter_skill_target_pick_mode(&mut self, skill: crate::asset::Skill) {
98+
self.saved_pick_mode = Some(self.pick_mode);
99+
self.pick_mode = PickMode::Object(ObjectPickMode::Skill(skill));
100+
}
101+
88102
fn insert_hex_cursor(world: &mut World) -> object::Handle {
89103
let mut hex_cursor = Object::new(FrameId::MOUSE_HEX_OUTLINE, None,
90104
Some(Default::default()), SubObject::None);
@@ -125,10 +139,6 @@ impl WorldView {
125139
}
126140

127141
impl Widget for WorldView {
128-
fn init(&mut self, ctx: Init) {
129-
ctx.base.set_cursor(Some(Cursor::Hidden));
130-
}
131-
132142
fn handle_event(&mut self, mut ctx: HandleEvent) {
133143
match ctx.event {
134144
Event::MouseMove { pos } => {
@@ -139,15 +149,18 @@ impl Widget for WorldView {
139149
ctx.out(UiCommandData::HexPick { action: false, pos });
140150
}
141151
}
142-
PickMode::Object => {
152+
PickMode::Object(ObjectPickMode::Action) => {
143153
self.pick_state = PickState::Pending { start: ctx.now, pos };
144154
self.default_action_icon = None;
145155
}
156+
PickMode::Object(ObjectPickMode::Skill(_)) => {}
146157
}
147158
self.update_hex_cursor_visibility(None);
148159
}
149160
Event::MouseDown { pos, button } => {
150-
if button == MouseButton::Left && self.pick_mode == PickMode::Object {
161+
if button == MouseButton::Left &&
162+
matches!(self.pick_mode, PickMode::Object(ObjectPickMode::Action))
163+
{
151164
let world = self.world.borrow();
152165
if let Some(obj) = world.pick_object(pos, true) {
153166
self.action_menu_state = Some((ctx.now, obj));
@@ -163,25 +176,34 @@ impl Widget for WorldView {
163176
let (pos, _) = self.update_hex_cursor_pos(pos);
164177
ctx.out(UiCommandData::HexPick { action: true, pos });
165178
}
166-
PickMode::Object => {
167-
let world = self.world.borrow();
168-
if let Some(obj) = world.pick_object(pos, true) {
169-
ctx.out(UiCommandData::ObjectPick {
170-
kind: ObjectPickKind::DefaultAction,
171-
obj,
172-
});
179+
PickMode::Object(mode) => {
180+
let picked_obj = self.world.borrow().pick_object(pos, true);
181+
if let Some(obj) = picked_obj {
182+
let kind = match mode {
183+
ObjectPickMode::Action => ObjectPickKind::DefaultAction,
184+
ObjectPickMode::Skill(skill) => {
185+
self.pick_mode = self.saved_pick_mode.take().unwrap();
186+
ObjectPickKind::Skill(skill)
187+
}
188+
};
189+
ctx.out(UiCommandData::ObjectPick { kind, obj });
190+
if self.pick_mode == PickMode::Hex {
191+
self.update_hex_cursor_visibility(None);
192+
let (pos, changed) = self.update_hex_cursor_pos(pos);
193+
if changed {
194+
ctx.out(UiCommandData::HexPick { action: false, pos });
195+
}
196+
}
173197
}
174198
}
175199
}
176200
}
177201
MouseButton::Right => {
178202
self.pick_mode = match self.pick_mode {
179203
PickMode::Hex => {
180-
ctx.base.set_cursor(Some(Cursor::ActionArrow));
181-
PickMode::Object
204+
PickMode::Object(ObjectPickMode::Action)
182205
}
183-
PickMode::Object => {
184-
ctx.base.set_cursor(Some(Cursor::Hidden));
206+
PickMode::Object(_) => {
185207
let (pos, changed) = self.update_hex_cursor_pos(pos);
186208
if changed {
187209
ctx.out(UiCommandData::HexPick { action: false, pos });
@@ -233,14 +255,16 @@ impl Widget for WorldView {
233255
}
234256

235257
fn sync(&mut self, ctx: Sync) {
236-
if ctx.base.cursor() != Some(Cursor::Hidden) {
237-
ctx.base.set_cursor(Some(
238-
if self.default_action_icon.is_some() {
239-
Placement::new(1, ctx.cursor_pos, ctx.base.rect()).cursor
240-
} else {
241-
Cursor::ActionArrow
242-
}))
243-
}
258+
ctx.base.set_cursor(Some(
259+
if self.default_action_icon.is_some() {
260+
Placement::new(1, ctx.cursor_pos, ctx.base.rect()).cursor
261+
} else {
262+
match self.pick_mode {
263+
PickMode::Hex => Cursor::Hidden,
264+
PickMode::Object(ObjectPickMode::Action) => Cursor::ActionArrow,
265+
PickMode::Object(ObjectPickMode::Skill(_)) => Cursor::CrosshairUse,
266+
}
267+
}));
244268
}
245269

246270
fn render(&mut self, ctx: Render) {
@@ -267,7 +291,7 @@ impl Widget for WorldView {
267291
});
268292
}
269293
}
270-
PickMode::Object => if let Some(action) = self.default_action_icon {
294+
PickMode::Object(ObjectPickMode::Action) => if let Some(action) = self.default_action_icon {
271295
let fid = action.icons().0;
272296
let pos = Placement::new(1, ctx.cursor_pos, ctx.base.unwrap().rect()).rect.top_left();
273297
Sprite {
@@ -280,6 +304,7 @@ impl Widget for WorldView {
280304
effect: None,
281305
}.render(ctx.canvas, ctx.frm_db);
282306
}
307+
PickMode::Object(ObjectPickMode::Skill(_)) => {}
283308
}
284309
}
285310
}

src/ui.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub enum Cursor {
5151
Arrow,
5252
ArrowDown,
5353
ArrowUp,
54+
CrosshairUse,
5455

5556
ScrollNorth,
5657
ScrollNorthEast,
@@ -81,6 +82,7 @@ impl Cursor {
8182
Arrow => FrameId::STDARROW,
8283
ArrowDown => FrameId::SDNARROW,
8384
ArrowUp => FrameId::SUPARROW,
85+
CrosshairUse => FrameId::CROSSHAIR_USE,
8486

8587
ScrollNorth => FrameId::SCRNORTH,
8688
ScrollNorthEast => FrameId::SCRNEAST,

src/ui/command.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub enum ObjectPickKind {
3535
Hover,
3636
DefaultAction,
3737
ActionMenu,
38+
Skill(crate::asset::Skill),
3839
}
3940

4041
#[derive(Clone, Copy, Debug, Eq, PartialEq)]

0 commit comments

Comments
 (0)