|
@@ -7,6 +7,7 @@ use ggez::input::keyboard::KeyCode;
|
7
|
7
|
use ggez::timer::check_update_time;
|
8
|
8
|
use ggez::{event, graphics, input, Context, GameResult};
|
9
|
9
|
|
|
10
|
+use crate::behavior::order::Order;
|
10
|
11
|
use crate::behavior::ItemBehavior;
|
11
|
12
|
use crate::config::{
|
12
|
13
|
ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
|
|
@@ -21,6 +22,7 @@ use crate::scene::item::{ItemState, SceneItem, SceneItemType};
|
21
|
22
|
use crate::ui::scene_item_menu::SceneItemMenuItem;
|
22
|
23
|
use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
|
23
|
24
|
use crate::{Offset, ScenePoint, WindowPoint};
|
|
25
|
+use std::f32::consts::FRAC_PI_2;
|
24
|
26
|
|
25
|
27
|
pub struct MainState {
|
26
|
28
|
// time
|
|
@@ -61,16 +63,16 @@ impl MainState {
|
61
|
63
|
let mut scene_items = vec![];
|
62
|
64
|
for x in 0..1 {
|
63
|
65
|
for y in 0..4 {
|
64
|
|
- let current_behavior = if y % 2 == 0 {
|
65
|
|
- ItemBehavior::Walking(util::vec_from_angle(90.0))
|
66
|
|
- } else {
|
67
|
|
- ItemBehavior::Crawling
|
68
|
|
- };
|
|
66
|
+ // let current_behavior = if y % 2 == 0 {
|
|
67
|
+ // ItemBehavior::WalkingTo(util::vec_from_angle(90.0))
|
|
68
|
+ // } else {
|
|
69
|
+ // ItemBehavior::CrawlingTo()
|
|
70
|
+ // };
|
69
|
71
|
|
70
|
72
|
scene_items.push(SceneItem::new(
|
71
|
73
|
SceneItemType::Soldier,
|
72
|
74
|
ScenePoint::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
|
73
|
|
- ItemState::new(current_behavior),
|
|
75
|
+ ItemState::new(ItemBehavior::Standing(0)),
|
74
|
76
|
));
|
75
|
77
|
}
|
76
|
78
|
}
|
|
@@ -105,6 +107,18 @@ impl MainState {
|
105
|
107
|
Ok(main_state)
|
106
|
108
|
}
|
107
|
109
|
|
|
110
|
+ fn get_scene_item(&self, index: usize) -> &SceneItem {
|
|
111
|
+ self.scene_items
|
|
112
|
+ .get(index)
|
|
113
|
+ .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ fn get_scene_item_mut(&mut self, index: usize) -> &mut SceneItem {
|
|
117
|
+ self.scene_items
|
|
118
|
+ .get_mut(index)
|
|
119
|
+ .expect(SCENE_ITEMS_CHANGE_ERR_MSG)
|
|
120
|
+ }
|
|
121
|
+
|
108
|
122
|
fn inputs(&mut self, ctx: &Context) {
|
109
|
123
|
let display_offset_by =
|
110
|
124
|
if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
|
|
@@ -149,6 +163,13 @@ impl MainState {
|
149
|
163
|
|
150
|
164
|
if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
|
151
|
165
|
// TODO: Add order to scene_item
|
|
166
|
+ match scene_item_prepare_order {
|
|
167
|
+ SceneItemPrepareOrder::Move(scene_item_usize) => {
|
|
168
|
+ let mut scene_item = self.get_scene_item_mut(*scene_item_usize);
|
|
169
|
+ scene_item.next_order = Some(Order::MoveTo(scene_position));
|
|
170
|
+ }
|
|
171
|
+ }
|
|
172
|
+
|
152
|
173
|
self.scene_item_prepare_order = None;
|
153
|
174
|
}
|
154
|
175
|
|
|
@@ -157,10 +178,7 @@ impl MainState {
|
157
|
178
|
let window_menu_point =
|
158
|
179
|
window_point_from_scene_point(&scene_menu_point, &self.display_offset);
|
159
|
180
|
let menu_sprite_info = UiSpriteInfo::from_type(UiItem::SceneItemMenu);
|
160
|
|
- let scene_item = self
|
161
|
|
- .scene_items
|
162
|
|
- .get(scene_item_usize)
|
163
|
|
- .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
|
|
181
|
+ let scene_item = self.get_scene_item(scene_item_usize);
|
164
|
182
|
if window_click_point.x >= window_menu_point.x
|
165
|
183
|
&& window_click_point.x <= window_menu_point.x + menu_sprite_info.width
|
166
|
184
|
&& window_click_point.y >= window_menu_point.y
|
|
@@ -197,10 +215,7 @@ impl MainState {
|
197
|
215
|
self.get_first_scene_item_for_scene_point(&scene_right_click_point)
|
198
|
216
|
{
|
199
|
217
|
if self.selected_scene_items.contains(&scene_item_usize) {
|
200
|
|
- let scene_item = self
|
201
|
|
- .scene_items
|
202
|
|
- .get(scene_item_usize)
|
203
|
|
- .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
|
|
218
|
+ let scene_item = self.get_scene_item(scene_item_usize);
|
204
|
219
|
self.scene_item_menu = Some((scene_item_usize, scene_item.position))
|
205
|
220
|
}
|
206
|
221
|
}
|
|
@@ -219,9 +234,12 @@ impl MainState {
|
219
|
234
|
// Scene items movements
|
220
|
235
|
for scene_item in self.scene_items.iter_mut() {
|
221
|
236
|
match scene_item.state.current_behavior {
|
222
|
|
- ItemBehavior::Walking(vector) => {
|
|
237
|
+ ItemBehavior::WalkingTo(scene_point) => {
|
|
238
|
+ // FIXME BS NOW: velocity
|
|
239
|
+ let move_vector = (scene_point - scene_item.position).normalize() * 1.0;
|
223
|
240
|
// TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
|
224
|
|
- scene_item.position.x += 1.0;
|
|
241
|
+ scene_item.position.x += move_vector.x;
|
|
242
|
+ scene_item.position.y += move_vector.y;
|
225
|
243
|
scene_item.grid_position =
|
226
|
244
|
util::grid_position_from_scene_point(&scene_item.position);
|
227
|
245
|
}
|
|
@@ -251,31 +269,83 @@ impl MainState {
|
251
|
269
|
// TODO: ici il faut reflechir a comment organiser les comportements
|
252
|
270
|
|
253
|
271
|
for scene_item in self.scene_items.iter_mut() {
|
254
|
|
- for meta_event in &scene_item.meta_events {
|
255
|
|
- match meta_event {
|
256
|
|
- MetaEvent::FeelExplosion => {
|
257
|
|
- scene_item.state = ItemState::new(ItemBehavior::Standing(self.frame_i));
|
|
272
|
+ // for meta_event in &scene_item.meta_events {
|
|
273
|
+ // match meta_event {
|
|
274
|
+ // MetaEvent::FeelExplosion => {
|
|
275
|
+ // scene_item.state = ItemState::new(ItemBehavior::Standing(self.frame_i));
|
|
276
|
+ // }
|
|
277
|
+ // }
|
|
278
|
+ // }
|
|
279
|
+
|
|
280
|
+ // match scene_item.state.current_behavior {
|
|
281
|
+ // ItemBehavior::Crawling => {
|
|
282
|
+ // scene_item.state =
|
|
283
|
+ // ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
|
|
284
|
+ // }
|
|
285
|
+ // ItemBehavior::Walking(_) => {
|
|
286
|
+ // scene_item.state = ItemState::new(ItemBehavior::Crawling);
|
|
287
|
+ // }
|
|
288
|
+ // ItemBehavior::Standing(since) => {
|
|
289
|
+ // if self.frame_i - since >= 120 {
|
|
290
|
+ // scene_item.state =
|
|
291
|
+ // ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
|
|
292
|
+ // }
|
|
293
|
+ // }
|
|
294
|
+ // }
|
|
295
|
+
|
|
296
|
+ scene_item.meta_events.drain(..);
|
|
297
|
+
|
|
298
|
+ if let Some(next_order) = &scene_item.next_order {
|
|
299
|
+ // TODO: Compute here if it possible (fear, compatible with current order, etc)
|
|
300
|
+ match next_order {
|
|
301
|
+ Order::MoveTo(move_to_scene_point) => {
|
|
302
|
+ scene_item.current_order = Some(Order::MoveTo(*move_to_scene_point));
|
258
|
303
|
}
|
259
|
304
|
}
|
|
305
|
+ scene_item.next_order = None;
|
260
|
306
|
}
|
261
|
307
|
|
262
|
|
- match scene_item.state.current_behavior {
|
263
|
|
- ItemBehavior::Crawling => {
|
264
|
|
- scene_item.state =
|
265
|
|
- ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
|
266
|
|
- }
|
267
|
|
- ItemBehavior::Walking(_) => {
|
268
|
|
- scene_item.state = ItemState::new(ItemBehavior::Crawling);
|
269
|
|
- }
|
270
|
|
- ItemBehavior::Standing(since) => {
|
271
|
|
- if self.frame_i - since >= 120 {
|
272
|
|
- scene_item.state =
|
273
|
|
- ItemState::new(ItemBehavior::Walking(util::vec_from_angle(90.0)));
|
|
308
|
+ // FIXME BS NOW: stop move when move is accomplished; warn: recompute move_vector here
|
|
309
|
+ if let Some(current_order) = &scene_item.current_order {
|
|
310
|
+ match current_order {
|
|
311
|
+ Order::MoveTo(move_to_scene_point) => {
|
|
312
|
+ let change_to_walk = match scene_item.state.current_behavior {
|
|
313
|
+ ItemBehavior::Standing(_) => true,
|
|
314
|
+ ItemBehavior::CrawlingTo(_) => true,
|
|
315
|
+ ItemBehavior::WalkingTo(_) => false,
|
|
316
|
+ };
|
|
317
|
+
|
|
318
|
+ if change_to_walk {
|
|
319
|
+ scene_item.state =
|
|
320
|
+ ItemState::new(ItemBehavior::WalkingTo(*move_to_scene_point));
|
|
321
|
+ }
|
274
|
322
|
}
|
275
|
323
|
}
|
276
|
324
|
}
|
277
|
325
|
|
278
|
|
- scene_item.meta_events.drain(..);
|
|
326
|
+ match scene_item.state.current_behavior {
|
|
327
|
+ ItemBehavior::Standing(_) => {}
|
|
328
|
+ ItemBehavior::CrawlingTo(scene_point) => {
|
|
329
|
+ let angle = f32::atan2(
|
|
330
|
+ scene_point.y - scene_item.position.y,
|
|
331
|
+ scene_point.x - scene_item.position.x,
|
|
332
|
+ ) + FRAC_PI_2;
|
|
333
|
+ scene_item.display_angle = angle;
|
|
334
|
+
|
|
335
|
+ let move_vector = (scene_point - scene_item.position).normalize() * 1.0;
|
|
336
|
+ println!("{:?}", move_vector);
|
|
337
|
+ }
|
|
338
|
+ ItemBehavior::WalkingTo(scene_point) => {
|
|
339
|
+ let angle = f32::atan2(
|
|
340
|
+ scene_point.y - scene_item.position.y,
|
|
341
|
+ scene_point.x - scene_item.position.x,
|
|
342
|
+ ) + FRAC_PI_2;
|
|
343
|
+ scene_item.display_angle = angle;
|
|
344
|
+
|
|
345
|
+ let move_vector = (scene_point - scene_item.position).normalize() * 1.0;
|
|
346
|
+ println!("{:?}", move_vector);
|
|
347
|
+ }
|
|
348
|
+ }
|
279
|
349
|
}
|
280
|
350
|
}
|
281
|
351
|
|
|
@@ -400,7 +470,7 @@ impl MainState {
|
400
|
470
|
mut mesh_builder: MeshBuilder,
|
401
|
471
|
) -> GameResult<MeshBuilder> {
|
402
|
472
|
for i in &self.selected_scene_items {
|
403
|
|
- let selected_scene_item = self.scene_items.get(*i).expect(SCENE_ITEMS_CHANGE_ERR_MSG);
|
|
473
|
+ let selected_scene_item = self.get_scene_item(*i);
|
404
|
474
|
mesh_builder.rectangle(
|
405
|
475
|
DrawMode::Stroke(StrokeOptions::default()),
|
406
|
476
|
graphics::Rect::new(
|
|
@@ -449,10 +519,7 @@ impl MainState {
|
449
|
519
|
if let Some(scene_item_prepare_order) = &self.scene_item_prepare_order {
|
450
|
520
|
match scene_item_prepare_order {
|
451
|
521
|
SceneItemPrepareOrder::Move(scene_item_usize) => {
|
452
|
|
- let scene_item = self
|
453
|
|
- .scene_items
|
454
|
|
- .get(*scene_item_usize)
|
455
|
|
- .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
|
|
522
|
+ let scene_item = self.get_scene_item(*scene_item_usize);
|
456
|
523
|
mesh_builder.line(
|
457
|
524
|
&vec![
|
458
|
525
|
scene_item.position.clone(),
|
|
@@ -521,9 +588,9 @@ impl event::EventHandler for MainState {
|
521
|
588
|
graphics::clear(ctx, graphics::BLACK);
|
522
|
589
|
let mut scene_mesh_builder = MeshBuilder::new();
|
523
|
590
|
|
524
|
|
- self.generate_scene_item_sprites();
|
525
|
|
- self.generate_scene_item_menu_sprites();
|
526
|
|
- self.generate_map_sprites();
|
|
591
|
+ self.generate_scene_item_sprites()?;
|
|
592
|
+ self.generate_scene_item_menu_sprites()?;
|
|
593
|
+ self.generate_map_sprites()?;
|
527
|
594
|
|
528
|
595
|
scene_mesh_builder = self.update_mesh_builder_with_debug(scene_mesh_builder)?;
|
529
|
596
|
scene_mesh_builder = self.update_mesh_builder_with_selected_items(scene_mesh_builder)?;
|
|
@@ -547,7 +614,7 @@ impl event::EventHandler for MainState {
|
547
|
614
|
|
548
|
615
|
graphics::present(ctx)?;
|
549
|
616
|
|
550
|
|
- println!("FPS: {}", ggez::timer::fps(ctx));
|
|
617
|
+ // println!("FPS: {}", ggez::timer::fps(ctx));
|
551
|
618
|
Ok(())
|
552
|
619
|
}
|
553
|
620
|
|