annotate src/th06/anm0_vm.rs @ 642:9e40bd5cc26d

Move interpolators in another module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 07 Jul 2019 12:19:50 +0200
parents a58103f2f264
children 01849ffd0180
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 //! Animation runner.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use crate::th06::anm0::{
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 Script,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 Anm0,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 Call,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 Instruction,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 };
642
9e40bd5cc26d Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 641
diff changeset
9 use crate::th06::interpolator::{Interpolator1, Interpolator2, Interpolator3, Formula};
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 use std::cell::RefCell;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 use std::rc::Rc;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 /// Base visual element.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 #[derive(Debug, Clone, Default)]
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 pub struct Sprite {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 blendfunc: u32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 frame: u16,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 width_override: f32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 height_override: f32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 angle: f32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 removed: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 changed: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 visible: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 force_rotation: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 automatic_orientation: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 allow_dest_offset: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 mirrored: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 corner_relative_placement: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
32 scale_interpolator: Option<Interpolator2<f32>>,
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
33 fade_interpolator: Option<Interpolator1<f32>>, // XXX: should be u8!
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
34 offset_interpolator: Option<Interpolator3<f32>>,
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
35 rotation_interpolator: Option<Interpolator3<f32>>,
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
36 color_interpolator: Option<Interpolator3<f32>>, // XXX: should be u8!
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 anm: Option<Anm0>,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 dest_offset: [f32; 3],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 texcoords: [f32; 4],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 texoffsets: [f32; 2],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 rescale: [f32; 2],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 scale_speed: [f32; 2],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 rotations_3d: [f32; 3],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 rotations_speed_3d: [f32; 3],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 color: [u8; 4],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 impl Sprite {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 /// Create a new sprite.
640
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
52 pub fn new(width_override: f32, height_override: f32) -> Sprite {
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
53 Sprite {
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
54 width_override,
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
55 height_override,
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
56 changed: true,
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
57 visible: true,
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
58 rescale: [1., 1.],
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
59 color: [255, 255, 255, 255],
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
60 ..Default::default()
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
61 }
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 /// Update sprite values from the interpolators.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 pub fn update(&mut self) {
640
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
66 self.frame += 1;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
67
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
68 let [sax, say, saz] = self.rotations_speed_3d;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
69 if sax != 0. || say != 0. || saz != 0. {
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
70 let [ax, ay, az] = self.rotations_3d;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
71 self.rotations_3d = [ax + sax, ay + say, az + saz];
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
72 self.changed = true;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
73 } else if let Some(ref interpolator) = self.rotation_interpolator {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
74 self.rotations_3d = interpolator.values(self.frame);
640
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
75 self.changed = true;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
76 }
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
77
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
78 let [rsx, rsy] = self.scale_speed;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
79 if rsx != 0. || rsy != 0. {
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
80 let [rx, ry] = self.rescale;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
81 self.rescale = [rx + rsx, ry + rsy];
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
82 self.changed = true;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
83 }
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
84
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
85 if let Some(ref interpolator) = self.fade_interpolator {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
86 self.color[3] = interpolator.values(self.frame)[0] as u8;
640
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
87 self.changed = true;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
88 }
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
89
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
90 if let Some(ref interpolator) = self.scale_interpolator {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
91 self.rescale = interpolator.values(self.frame);
640
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
92 self.changed = true;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
93 }
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
94
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
95 if let Some(ref interpolator) = self.offset_interpolator {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
96 self.dest_offset = interpolator.values(self.frame);
640
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
97 self.changed = true;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
98 }
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
99
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
100 if let Some(ref interpolator) = self.color_interpolator {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
101 let color = interpolator.values(self.frame);
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
102 // TODO: this can probably be made to look nicer.
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
103 self.color[0] = color[0] as u8;
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
104 self.color[1] = color[1] as u8;
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
105 self.color[2] = color[2] as u8;
640
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
106 self.changed = true;
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
107 }
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 /// Interpreter for `Anm0` instructions to update a `Sprite`.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 pub struct AnmRunner {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 anm: Anm0,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 sprite: Rc<RefCell<Sprite>>,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 running: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 sprite_index_offset: u32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 script: Script,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 instruction_pointer: usize,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 frame: u16,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 waiting: bool,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 variables: ([i32; 4], [f32; 4], [i32; 4]),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 timeout: Option<u16>,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 impl AnmRunner {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 /// Create a new `AnmRunner`.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 pub fn new(anm: &Anm0, script_id: u8, sprite: Rc<RefCell<Sprite>>, sprite_index_offset: u32) -> AnmRunner {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 let mut runner = AnmRunner {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 anm: anm.clone(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 sprite: sprite,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 running: true,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 waiting: false,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 script: anm.scripts[&script_id].clone(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 frame: 0,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 timeout: None,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 instruction_pointer: 0,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 variables: ([0, 0, 0, 0 ],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 [0., 0., 0., 0.],
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 [0, 0, 0, 0 ]),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 sprite_index_offset: sprite_index_offset,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 };
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 runner.run_frame();
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 runner.sprite_index_offset = 0;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 runner
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 /// Trigger an interrupt.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 pub fn interrupt(&mut self, interrupt: i32) -> bool {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 let mut new_ip = self.script.interrupts.get(&interrupt);
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 if new_ip.is_none() {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 new_ip = self.script.interrupts.get(&-1);
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 let new_ip = if let Some(new_ip) = new_ip {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 *new_ip as usize
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 } else {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 return false;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 };
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 self.instruction_pointer = new_ip;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 let Call { time: frame, instr: _ } = &self.script.instructions[self.instruction_pointer];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 self.frame = *frame;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 self.waiting = false;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 self.sprite.borrow_mut().visible = true;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 true
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 /// Advance the Anm of a single frame.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 pub fn run_frame(&mut self) -> bool {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 if !self.running {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 return false;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 while self.running && !self.waiting {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 let Call { time: frame, instr } = self.script.instructions[self.instruction_pointer];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 let frame = frame.clone();
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 if frame > self.frame {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 break;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 } else {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 self.instruction_pointer += 1;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 if frame == self.frame {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 self.run_instruction(instr);
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 self.sprite.borrow_mut().changed = true;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190 if !self.waiting {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 self.frame += 1;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 } else if let Some(timeout) = self.timeout {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 if timeout == self.sprite.borrow().frame { // TODO: check if it’s happening at the correct frame.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194 self.waiting = false;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 self.sprite.borrow_mut().update();
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200 self.running
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203 fn run_instruction(&mut self, instruction: Instruction) {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
204 let mut sprite = self.sprite.borrow_mut();
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205 match instruction {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206 Instruction::Delete() => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
207 sprite.removed = true;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
208 self.running = false;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210 Instruction::LoadSprite(sprite_index) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211 sprite.anm = Some(self.anm.clone());
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 let texcoords = &self.anm.sprites[(sprite_index + self.sprite_index_offset) as usize];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213 sprite.texcoords = [texcoords.x, texcoords.y, texcoords.width, texcoords.height];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
215 Instruction::SetScale(sx, sy) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
216 sprite.rescale = [sx, sy];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
217 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
218 Instruction::SetAlpha(alpha) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219 // TODO: check this modulo.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
220 sprite.color[3] = (alpha % 256) as u8;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
221 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
222 Instruction::SetColor(b, g, r) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
223 if sprite.fade_interpolator.is_none() {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
224 sprite.color[0] = r;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
225 sprite.color[1] = g;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
226 sprite.color[2] = b;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
227 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
228 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
229 Instruction::Jump(pointer) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
230 // TODO: is that really how it works?
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
231 self.instruction_pointer = pointer as usize;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
232 self.frame = self.script.instructions[pointer as usize].time;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
233 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
234 Instruction::ToggleMirrored() => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
235 sprite.mirrored = !sprite.mirrored;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
236 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
237 Instruction::SetRotations3d(rx, ry, rz) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
238 sprite.rotations_3d = [rx, ry, rz];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
239 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
240 Instruction::SetRotationsSpeed3d(srx, sry, srz) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
241 sprite.rotations_speed_3d = [srx, sry, srz];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
242 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
243 Instruction::SetScaleSpeed(ssx, ssy) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
244 sprite.scale_speed = [ssx, ssy];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
245 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
246 Instruction::Fade(new_alpha, duration) => {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
247 sprite.fade_interpolator = Some(Interpolator1::new([sprite.color[3] as f32], sprite.frame, [new_alpha as f32], sprite.frame + duration as u16, Formula::Linear));
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
248 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
249 Instruction::SetBlendmodeAlphablend() => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
250 sprite.blendfunc = 1;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
251 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
252 Instruction::SetBlendmodeAdd() => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
253 sprite.blendfunc = 0;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
254 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
255 Instruction::KeepStill() => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
256 self.running = false;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
257 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
258 Instruction::LoadRandomSprite(min_index, amplitude) => {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
259 let sprite_index = min_index; // XXX: + randrange(amplitude);
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
260
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
261 // TODO: refactor that with Instruction::LoadSprite.
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
262 sprite.anm = Some(self.anm.clone());
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
263 let texcoords = &self.anm.sprites[(sprite_index + self.sprite_index_offset) as usize];
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
264 sprite.texcoords = [texcoords.x, texcoords.y, texcoords.width, texcoords.height];
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
265 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
266 Instruction::Move(x, y, z) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
267 sprite.dest_offset = [x, y, z];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
268 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
269 Instruction::MoveToLinear(x, y, z, duration) => {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
270 sprite.offset_interpolator = Some(Interpolator3::new(sprite.dest_offset, sprite.frame, [x, y, z], sprite.frame + duration as u16, Formula::Linear));
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
271 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
272 Instruction::MoveToDecel(x, y, z, duration) => {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
273 sprite.offset_interpolator = Some(Interpolator3::new(sprite.dest_offset, sprite.frame, [x, y, z], sprite.frame + duration as u16, Formula::InvertPower2));
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
274 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
275 Instruction::MoveToAccel(x, y, z, duration) => {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
276 sprite.offset_interpolator = Some(Interpolator3::new(sprite.dest_offset, sprite.frame, [x, y, z], sprite.frame + duration as u16, Formula::Power2));
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
277 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
278 Instruction::Wait() => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
279 self.waiting = true;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
280 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
281 // There is nothing to do here.
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
282 Instruction::InterruptLabel(_label) => (),
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
283 Instruction::SetCornerRelativePlacement() => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
284 sprite.corner_relative_placement = true;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
285 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
286 Instruction::WaitEx() => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
287 sprite.visible = false;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
288 self.waiting = true;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
289 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
290 Instruction::SetAllowOffset(value) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
291 sprite.allow_dest_offset = value == 1
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
292 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
293 Instruction::SetAutomaticOrientation(value) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
294 sprite.automatic_orientation = value == 1
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
295 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
296 Instruction::ShiftTextureX(dx) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
297 let [tox, toy] = sprite.texoffsets;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
298 sprite.texoffsets = [tox + dx, toy];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
299 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
300 Instruction::ShiftTextureY(dy) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
301 let [tox, toy] = sprite.texoffsets;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
302 sprite.texoffsets = [tox, toy + dy];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
303 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
304 Instruction::SetVisible(visible) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
305 sprite.visible = (visible & 1) != 0;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
306 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
307 Instruction::ScaleIn(sx, sy, duration) => {
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
308 sprite.scale_interpolator = Some(Interpolator2::new(sprite.rescale, sprite.frame, [sx, sy], sprite.frame + duration as u16, Formula::Linear));
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
309 }
641
a58103f2f264 Implement and use interpolators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 640
diff changeset
310 Instruction::Todo(_todo) => {
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
311 // TODO.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
312 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
313 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
314 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
315 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
316
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
317 #[cfg(test)]
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
318 mod tests {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
319 use super::*;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
320 use std::io::{self, Read};
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
321 use std::fs::File;
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
322
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
323 #[test]
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
324 fn anm_runner() {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
325 let file = File::open("/home/linkmauve/games/pc/東方/TH06 ~ The Embodiment of Scarlet Devil/CM/player01.anm").unwrap();
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
326 let mut file = io::BufReader::new(file);
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
327 let mut buf = vec![];
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
328 file.read_to_end(&mut buf).unwrap();
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
329 let anm0 = Anm0::from_slice(&buf).unwrap();
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
330 assert_eq!(anm0.size, (256, 256));
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
331 assert_eq!(anm0.format, 5);
640
37d151fe000b Implement better defaults for Sprite, and a beginning of
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
332 let sprite = Rc::new(RefCell::new(Sprite::new(0., 0.)));
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
333 let mut anm_runner = AnmRunner::new(&anm0, 1, sprite.clone(), 0);
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
334 for _ in 0..50 {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
335 anm_runner.run_frame();
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
336 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
337 }
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
338 }