annotate src/th06/enemy.rs @ 662:107bb5ca5cc8

Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 11 Aug 2019 18:14:01 +0200
parents 598f3125cbac
children f08e8e3c6196
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 //! Module providing an Enemy struct, to be changed by EclRunner.
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use crate::th06::anm0::Anm0;
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use crate::th06::anm0_vm::{Sprite, AnmRunner};
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use crate::th06::interpolator::{Interpolator1, Interpolator2};
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use crate::util::prng::Prng;
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use std::cell::RefCell;
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 use std::collections::HashMap;
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 use std::rc::{Rc, Weak};
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
11 /// The 2D position of an object in the game.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
12 #[derive(Debug, Clone, Copy, Default)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
13 pub struct Position {
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
14 pub(crate) x: f32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
15 pub(crate) y: f32,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
18 /// An offset which can be added to a Position.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
19 #[derive(Debug, Clone, Copy, Default)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
20 pub struct Offset {
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 dx: f32,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 dy: f32,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 impl Position {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
26 /// Create said position.
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 pub fn new(x: f32, y: f32) -> Position {
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 Position { x, y }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 impl Offset {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
33 /// Create said offset.
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 pub fn new(dx: f32, dy: f32) -> Offset {
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 Offset { dx, dy }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 impl std::ops::Add<Offset> for Position {
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 type Output = Position;
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 fn add(self, offset: Offset) -> Position {
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 Position {
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 x: self.x + offset.dx,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 y: self.y + offset.dy,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 #[derive(Debug, Clone)]
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 struct Callback;
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 #[derive(Debug, Clone)]
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 struct Laser;
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
55 #[derive(Debug, Clone, Default)]
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 struct Process;
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
58 /// God struct of our game.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
59 pub struct Game {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
60 enemies: Vec<Rc<RefCell<Enemy>>>,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
61 anmrunners: Vec<Rc<RefCell<AnmRunner>>>,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 prng: Rc<RefCell<Prng>>,
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
63 rank: i32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
64 difficulty: i32,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
67 impl Game {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
68 /// Create said god struct.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
69 pub fn new(prng: Rc<RefCell<Prng>>) -> Game {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
70 Game {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
71 enemies: Vec::new(),
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
72 anmrunners: Vec::new(),
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
73 prng,
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
74 rank: 0,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
75 difficulty: 0,
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
76 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
77 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
78
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
79 /// Run the simulation for a single frame.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
80 pub fn run_frame(&mut self) {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
81 /*
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
82 for eclrunner in self.eclrunners {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
83 eclrunner.run_frame();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
84 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
85 */
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
86
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
87 for anmrunner in self.anmrunners.iter() {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
88 let mut anmrunner = anmrunner.borrow_mut();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
89 anmrunner.run_frame();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
90 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
91 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
92
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
93 /// Returns a list of all sprites currently being displayed on screen.
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
94 pub fn get_sprites(&self) -> Vec<(f32, f32, f32, Rc<RefCell<Sprite>>)> {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
95 let mut sprites = vec![];
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
96 for enemy in self.enemies.iter() {
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
97 let enemy = enemy.borrow();
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
98 let anmrunner = enemy.anmrunner.upgrade().unwrap();
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
99 let anmrunner = anmrunner.borrow();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
100 let sprite = anmrunner.get_sprite();
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
101 sprites.push((enemy.pos.x, enemy.pos.y, enemy.z, sprite));
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
102 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
103 sprites
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
104 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
105 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
106
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 /// Common to all elements in game.
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 struct Element {
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 pos: Position,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 removed: bool,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 anmrunner: AnmRunner,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 /// The enemy struct, containing everything pertaining to an enemy.
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
115 #[derive(Default)]
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 pub struct Enemy {
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 // Common to all elements in game.
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
118 pub(crate) pos: Position,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
119 pub(crate) removed: bool,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
120 pub(crate) anmrunner: Weak<RefCell<AnmRunner>>,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 // Specific to enemy.
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 // Floats.
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
124 pub(crate) z: f32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
125 pub(crate) angle: f32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
126 pub(crate) speed: f32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
127 pub(crate) rotation_speed: f32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
128 pub(crate) acceleration: f32,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 // Ints.
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
131 pub(crate) type_: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
132 pub(crate) bonus_dropped: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
133 pub(crate) die_score: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
134 /// XXX
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
135 pub frame: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
136 pub(crate) life: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
137 pub(crate) death_flags: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
138 pub(crate) current_laser_id: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
139 pub(crate) low_life_trigger: Option<u32>,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
140 pub(crate) timeout: Option<u32>,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
141 pub(crate) remaining_lives: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
142 pub(crate) bullet_launch_interval: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
143 pub(crate) bullet_launch_timer: u32,
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
144 pub(crate) death_anim: i32,
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
145 pub(crate) direction: u32,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
146 pub(crate) update_mode: u32,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 // Bools.
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
149 pub(crate) visible: bool,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
150 pub(crate) was_visible: bool,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
151 pub(crate) touchable: bool,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
152 pub(crate) collidable: bool,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
153 pub(crate) damageable: bool,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
154 pub(crate) boss: bool,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
155 pub(crate) automatic_orientation: bool,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
156 pub(crate) delay_attack: bool,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 // Tuples.
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
159 pub(crate) difficulty_coeffs: (f32, f32, u32, u32, u32, u32),
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
160 pub(crate) extended_bullet_attributes: Option<(u32, u32, u32, u32, f32, f32, f32, f32)>,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
161 pub(crate) bullet_attributes: Option<(i16, i16, u32, u32, u32, f32, f32, f32, f32, u32)>,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
162 pub(crate) bullet_launch_offset: Offset,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
163 pub(crate) movement_dependant_sprites: Option<(f32, f32, f32, f32)>,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
164 pub(crate) screen_box: Option<(f32, f32, f32, f32)>,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 // Callbacks.
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 death_callback: Option<Callback>,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 boss_callback: Option<Callback>,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 low_life_callback: Option<Callback>,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 timeout_callback: Option<Callback>,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 // Laser.
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 laser_by_id: HashMap<u32, Laser>,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 // Options.
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
176 // TODO: actually a 8 element array.
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 options: Vec<Element>,
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 // Interpolators.
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
180 pub(crate) interpolator: Option<Interpolator2<f32>>,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
181 pub(crate) speed_interpolator: Option<Interpolator1<f32>>,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 // Misc stuff, do we need them?
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
184 pub(crate) anm0: Weak<RefCell<Anm0>>,
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 process: Rc<RefCell<Process>>,
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
186 pub(crate) game: Weak<RefCell<Game>>,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
187 pub(crate) prng: Weak<RefCell<Prng>>,
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
188 pub(crate) hitbox_half_size: [f32; 2],
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 }
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 impl Enemy {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
192 /// Create a new enemy.
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
193 pub fn new(pos: Position, life: i32, bonus_dropped: u32, die_score: u32, anm0: Weak<RefCell<Anm0>>, game: Weak<RefCell<Game>>) -> Rc<RefCell<Enemy>> {
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
194 let game_rc = game.upgrade().unwrap();
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
195 let enemy = Enemy {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
196 pos,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
197 anm0,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
198 game,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
199 visible: true,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
200 bonus_dropped,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
201 die_score,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
202 life: if life < 0 { 1 } else { life as u32 },
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
203 touchable: true,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
204 collidable: true,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
205 damageable: true,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
206 difficulty_coeffs: (-0.5, 0.5, 0, 0, 0, 0),
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
207 ..Default::default()
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
208 };
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
209 let enemy = Rc::new(RefCell::new(enemy));
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
210 game_rc.borrow_mut().enemies.push(enemy.clone());
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
211 enemy
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
212 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
213
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 /// Sets the animation to the one indexed by index in the current anm0.
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
215 pub fn set_anim(&mut self, index: u8) {
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
216 let anm0 = self.anm0.upgrade().unwrap();
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
217 let game = self.game.upgrade().unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
218 let sprite = Rc::new(RefCell::new(Sprite::new()));
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
219 let anmrunner = AnmRunner::new(&*anm0.borrow(), index, sprite, self.prng.clone(), 0);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
220 let anmrunner = Rc::new(RefCell::new(anmrunner));
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
221 self.anmrunner = Rc::downgrade(&anmrunner);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
222 (*game.borrow_mut()).anmrunners.push(anmrunner);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
223 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
224
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
225 /// Sets the current position of the enemy.
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
226 pub fn set_pos(&mut self, x: f32, y: f32, z: f32) {
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
227 self.pos.x = x;
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
228 self.pos.y = y;
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
229 self.z = z;
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
230 }
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
231
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
232 /// Sets the hitbox around the enemy.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
233 pub fn set_hitbox(&mut self, width: f32, height: f32) {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
234 self.hitbox_half_size = [width, height];
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
235 }
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
236
662
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
237 /// Run all interpolators and such, and update internal variables once per
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
238 /// frame.
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
239 pub fn update(&mut self) {
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
240 let Position { mut x, mut y } = self.pos;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
241
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
242 let speed = if self.update_mode == 1 {
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
243 0.
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
244 } else {
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
245 let speed = self.speed;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
246 self.speed += self.acceleration;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
247 self.angle += self.rotation_speed;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
248 speed
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
249 };
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
250
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
251 let dx = self.angle.cos() * speed;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
252 let dy = self.angle.sin() * speed;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
253 if self.type_ & 2 != 0 {
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
254 x -= dx;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
255 } else {
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
256 x += dx;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
257 }
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
258 y += dy;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
259
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
260 self.pos = Position { x, y };
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
261
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
262 self.frame += 1;
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
263 }
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
264
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
265 pub(crate) fn get_rank(&self) -> i32 {
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
266 let game = self.game.upgrade().unwrap();
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
267 let game = game.borrow();
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
268 game.rank
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
269 }
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
270
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
271 pub(crate) fn get_difficulty(&self) -> i32 {
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
272 let game = self.game.upgrade().unwrap();
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
273 let game = game.borrow();
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
274 game.difficulty
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
275 }
657
ff7b6355cdf1 Port the Enemy struct from Python, for now without its methods.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
276 }
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
277
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
278 trait Renderable {
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
279 fn get_sprites(&self) -> Vec<Rc<RefCell<Sprite>>>;
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
280 }
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
281
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
282 impl Renderable for Enemy {
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
283 fn get_sprites(&self) -> Vec<Rc<RefCell<Sprite>>> {
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
284 let anmrunner = self.anmrunner.upgrade().unwrap();
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
285 let anmrunner = anmrunner.borrow();
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
286 vec![anmrunner.get_sprite()]
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
287 }
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
288 }
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
289
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
290 #[cfg(test)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
291 mod tests {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
292 use super::*;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
293 use std::io::{self, Read};
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
294 use std::fs::File;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
295
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
296 #[test]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
297 fn enemy() {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
298 let file = File::open("EoSD/ST/stg1enm.anm").unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
299 let mut file = io::BufReader::new(file);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
300 let mut buf = vec![];
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
301 file.read_to_end(&mut buf).unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
302 let anm0 = Anm0::from_slice(&buf).unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
303 let anm0 = Rc::new(RefCell::new(anm0));
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
304 let prng = Rc::new(RefCell::new(Prng::new(0)));
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
305 let game = Game::new(prng);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
306 let game = Rc::new(RefCell::new(game));
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
307 let mut enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, Rc::downgrade(&anm0), Rc::downgrade(&game));
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
308 assert!(enemy.anmrunner.upgrade().is_none());
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
309 enemy.set_anim(0);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
310 assert!(enemy.anmrunner.upgrade().is_some());
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
311 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 657
diff changeset
312 }