1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use serde_derive::{Deserialize, Serialize};
pub mod input;
pub mod output;
pub mod runtime;
pub mod serial;
use input::*;
use output::*;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Info {
name: String,
step_interval: u32,
players: Vec<Player>,
}
impl Info {
pub fn new(
name: &str,
steps_per_second: i32,
number_of_players: i32,
input: InputDeviceType,
) -> Self {
let player = Player { input };
let mut players = Vec::with_capacity(number_of_players as usize);
for _ in 0..number_of_players {
players.push(player.clone());
}
Self {
name: name.to_string(),
step_interval: Self::steps_per_second_to_interval(steps_per_second),
players,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn step_interval(&self) -> u32 {
self.step_interval
}
pub fn steps_per_second(&self) -> u32 {
1_000_000_000 / self.step_interval
}
pub fn steps_per_second_to_interval(steps: i32) -> u32 {
1_000_000_000 / steps as u32
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Player {
input: InputDeviceType,
}
pub trait Game {
fn step(&mut self, arguments: &StepArguments);
fn draw(&self, arguments: &DrawArguments) -> Image;
fn render_audio(&self, arguments: &RenderAudioArguments) -> Sound;
}
#[derive(Serialize, Deserialize, Default)]
pub struct StepArguments {
input: InputArguments,
}
impl StepArguments {
pub fn new(input: InputArguments) -> Self {
Self { input }
}
pub fn input(&self) -> &InputArguments {
&self.input
}
}
#[derive(Serialize, Deserialize, Default)]
pub struct InputArguments {
players: Vec<Option<PlayerInputArguments>>,
}
impl InputArguments {
pub fn new(players: Vec<Option<PlayerInputArguments>>) -> Self {
Self { players }
}
pub fn player(&self, player: i32) -> Option<&PlayerInputArguments> {
let player = self.players.get(player as usize);
if let Some(player) = player {
return player.as_ref();
}
None
}
}
#[derive(Serialize, Deserialize)]
pub struct PlayerInputArguments {
input: InputDevice,
}
impl PlayerInputArguments {
pub fn nes(&self) -> Option<&Nes> {
if let InputDevice::Nes(ref nes) = self.input {
return Some(nes);
}
None
}
pub fn controller(&self) -> Option<&Controller> {
if let InputDevice::Controller(ref nes) = self.input {
return Some(nes);
}
None
}
pub fn keyboard(&self) -> Option<&Keyboard> {
if let InputDevice::Keyboard(ref nes) = self.input {
return Some(nes);
}
None
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DrawArguments {
width: i32,
height: i32,
step_offset: f32,
}
impl DrawArguments {
pub fn new(width: i32, height: i32, step_offset: f32) -> Self {
Self {
width,
height,
step_offset,
}
}
pub fn width(&self) -> i32 {
self.width
}
pub fn height(&self) -> i32 {
self.height
}
pub fn step_offset(&self) -> f32 {
self.step_offset
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct RenderAudioArguments {}