More fixes, add agnostic_interface.rs

This commit is contained in:
Stephen Seo 2023-02-13 18:53:04 +09:00
parent b29a35c2d6
commit 1dcfbc2db9
3 changed files with 40 additions and 5 deletions

22
src/agnostic_interface.rs Normal file
View file

@ -0,0 +1,22 @@
use crate::faux_quicksilver::Color;
pub trait ImageInterface {
fn draw(&self, x: f32, y: f32) -> Result<(), String>;
}
pub trait FontInterface {
fn draw(&self, s: &str) -> Result<(), String>;
}
pub trait SoundInterface {
fn play(&self, vol: f32) -> Result<(), String>;
}
pub trait WindowInterface {
fn get_dimensions(&self) -> Result<(f32, f32), String>;
fn get_key_pressed(&self, key: char) -> Result<bool, String>;
fn get_mouse_pressed(&self) -> Result<Option<(f32, f32)>, String>;
fn clear_window(&self, color: Color) -> Result<(), String>;
fn begin_drawing(&self) -> Result<(), String>;
fn end_drawing(&self) -> Result<(), String>;
}

View file

@ -2,6 +2,8 @@ use std::ops::{Mul, Add, AddAssign, Sub};
use serde::{Deserialize, Serialize};
use crate::agnostic_interface::WindowInterface;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Color {
pub r: u8,
@ -230,6 +232,15 @@ pub struct View {
}
pub struct Window {
interface: Box<dyn WindowInterface>,
}
impl Window {
pub fn new() -> Self {
Self {
interface: todo!(),
}
}
}
pub struct Key {

View file

@ -13,6 +13,7 @@
use rand::prelude::*;
use serde::{Deserialize, Serialize};
mod agnostic_interface;
mod faux_quicksilver;
use faux_quicksilver::{Color, Image, Rectangle, Circle, Vector, Transform, Window, Sound, Font, FontStyle, Event, Key, View};
@ -1914,9 +1915,9 @@ impl GameState {
0xFF,
0xFF,
0xFF,
self.player_particles.opacity,
(self.player_particles.opacity * 255.0) as u8,
)),
Transform::translate(-self.player.size.x / 2.0, -self.player.size.y / 2.0)
Transform::translate(-self.player.x / 2.0, -self.player.y / 2.0)
* Transform::rotate(self.player_r as f32),
1,
);
@ -1948,9 +1949,10 @@ impl GameState {
| SaveLoadNotification::Load { text, timer } => {
if let Some(i) = text {
let mut c = Color::WHITE;
c.a = (*timer / SL_NOTIF_TIME) as f32;
let mut image_rect = i.area();
image_rect.pos = self.camera.pos + Vector::new(20.0, 20.0);
c.a = ((*timer / SL_NOTIF_TIME) as f32 * 255.0) as u8;
let mut image_rect = i.area_rect();
image_rect.x = self.camera.x + 20.0;
image_rect.y = self.camera.y + 20.0;
window.draw(&image_rect, Blended(i, c));
}
}