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::*;

/// Holds information about the Game
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Info {
    name: String,
    step_interval: u32,
    players: Vec<Player>,
}

impl Info {
    /// Create a new structure of info about the game being played
    /// # Arguments
    /// * `name` - The title of the game
    /// * `steps_per_second` - The number of times Game::Step() should be called per second
    /// * `number_of_players` - How many players this games should have
    /// * `input` - The device type to use for each player
    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,
        }
    }
    
    /// Gets the name of the game
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Gets time between steps of the game, in nanoseconds
    pub fn step_interval(&self) -> u32 {
        self.step_interval
    }

    // Gets the number of steps per second
    pub fn steps_per_second(&self) -> u32 {
        1_000_000_000 / self.step_interval
    }

    /// Converts from steps per second to a time interval in nanoseconds
    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,
}

/// The core trait used by Romy, games need to implement this. Romy will use these methods to run
/// the game.
pub trait Game {
    /// Simulates the game by one step
    /// 
    /// # Arguments
    /// * `arguments` - Info, such as inputs, to be used in this step
    fn step(&mut self, arguments: &StepArguments);

    /// Renders an image for Romy to display, can be called many times per step.
    ///
    /// This function can return any image.
    /// 
    /// # Arguments
    /// * `arguments` - Info, such as the width of the frame Romy is rendering too, to be used in
    /// this step
    fn draw(&self, arguments: &DrawArguments) -> Image;

    /// Renders some audio for Romy to play, called once per step.
    ///
    /// The sound returned currently needs to be at a sample rate of 44100hz, and have enough
    /// samples to cover the amount of time between calls to step.
    fn render_audio(&self, arguments: &RenderAudioArguments) -> Sound;
}

// Input Arguments /////////////////////////////////////////////////////////////////////////////////

/// Arguments passed for each step of the game
#[derive(Serialize, Deserialize, Default)]
pub struct StepArguments {
    input: InputArguments,
}

impl StepArguments {
    pub fn new(input: InputArguments) -> Self {
        Self { input }
    }

    /// Get the input for this step
    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 }
    }

    /// Get the input for a specific player, will be None if there is no available player
    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 {
    /// Get the players NES style controller, will be None if there is no suitable input device, or
    /// one wasn't asked for in the supplied game info.
    pub fn nes(&self) -> Option<&Nes> {
        if let InputDevice::Nes(ref nes) = self.input {
            return Some(nes);
        }
        None
    }

    /// Get the players standard style controller, will be None if there is no suitable input device
    /// or one wasn't asked for in the supplied game info.
    pub fn controller(&self) -> Option<&Controller> {
        if let InputDevice::Controller(ref nes) = self.input {
            return Some(nes);
        }
        None
    }

    /// Get the players keyboard, will be None if there is no suitable input device or one wasn't 
    /// asked for in the supplied game info.
    pub fn keyboard(&self) -> Option<&Keyboard> {
        if let InputDevice::Keyboard(ref nes) = self.input {
            return Some(nes);
        }
        None
    }
}

// Draw arguments //////////////////////////////////////////////////////////////////////////////////

/// Arguments passed for each draw of the game
#[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,
        }
    }

    /// The horizontal width in pixels of the display area being used by Romy
    pub fn width(&self) -> i32 {
        self.width
    }

    /// The vertical height in pixels of the display area being used by Romy
    pub fn height(&self) -> i32 {
        self.height
    }

    /// The fraction of time since the last step call in a range of 0.0 - 1.0. 0.0 no time has
    /// passed, 0.5 = half way to the next step, 0.99 = almost all the way to the next step.
    pub fn step_offset(&self) -> f32 {
        self.step_offset
    }
}

// Render audio arguments //////////////////////////////////////////////////////////////////////////

/// Arguments passed for each audio render of the game
#[derive(Serialize, Deserialize, Debug)]
pub struct RenderAudioArguments {}