Struct nze_game_sdl::GameObject
source · pub struct GameObject {
pub rect: Rect,
pub tex_rect: Option<Rect>,
pub parallax: Vec2,
pub colour: Colour,
pub rotate: f64,
pub pivot: Option<Vec2>,
pub flip_horizontal: bool,
pub flip_vertical: bool,
/* private fields */
}
Expand description
used by crate::camera::Camera for drawing texures with texture rects and draw rects and a colour.
Fields§
§rect: Rect
§tex_rect: Option<Rect>
§parallax: Vec2
§colour: Colour
§rotate: f64
§pivot: Option<Vec2>
§flip_horizontal: bool
§flip_vertical: bool
Implementations§
source§impl GameObject
impl GameObject
sourcepub fn new_from_tex(texture: Texture) -> Self
pub fn new_from_tex(texture: Texture) -> Self
Examples found in repository?
examples/simple.rs (lines 20-22)
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
pub fn main() -> Result<(), Error> {
let (mut cam, drawing_area, context) = DrawingArea::new(
"Name of Game", // window name
Rect::new(0.0, 0.0, 400.0, 400.0), // window camera
Vec2::new(400.0, 400.0) // window size
)?;
let mut render = Render::new(drawing_area, &context)?;
let mut controls = Controls::new(&context)?;
let mut obj = GameObject::new_from_tex(
render.texture_manager.load(Path::new("resources/textures/gaia.png"))?
);
obj.rect.x = cam.get_view_size().x / 2.0 - obj.rect.w / 2.0;
obj.rect.y = cam.get_view_size().y / 2.0 - obj.rect.h / 2.0;
const SPEED: f64 = 125.0;
while !controls.should_close {
controls.update(&cam);
if controls.kb.down(Key::D) {
obj.rect.x += SPEED * controls.frame_elapsed;
}
if controls.kb.down(Key::A) {
obj.rect.x -= SPEED * controls.frame_elapsed;
}
if controls.kb.down(Key::W) {
obj.rect.y -= SPEED * controls.frame_elapsed;
}
if controls.kb.down(Key::S) {
obj.rect.y += SPEED * controls.frame_elapsed;
}
if controls.kb.down(Key::Escape) {
controls.should_close = true;
}
render.start_draw();
cam.draw(&obj);
render.end_draw(&mut cam)?;
}
Ok(())
}
More examples
examples/main.rs (lines 38-40)
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
pub fn main() -> Result<(), Error> {
let (mut cam, drawing_area, context) = DrawingArea::new(
"Game Template", //window name
Rect::new(0.0, 0.0, 240.0, 160.0),
Vec2::new(240.0 * 4.0, 160.0 * 4.0)
)?;
let mut render = Render::new(drawing_area, &context)?;
let mut controls = Controls::new(&context)?;
let mono_font = render.font_manager.load_font(
Path::new("resources/textures/fonts/FiraCode-Light.ttf"))?;
let map = Map::new(
Path::new("resources/map/test.tmx"), &mut render.texture_manager,
Path::new("resources/textures/fonts"), &mut render.font_manager)?;
let mut audio = AudioManager::new()?;
let music = audio.music.load(Path::new("resources/audio/test.wav"))?;
audio.music.play(music, -1)?;
let sfx = audio.sfx.load(Path::new("resources/audio/test.mp3"))?;
audio.sfx.set_volume(sfx, 0.4)?;
//checking resource loading/unloading
let mut is_gaia = true;
let mut ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/gaia.png"))?);
let mut text = render
.font_manager
.load_text_obj(
&mono_font,
"The Planet Earth",
Colour::new(100, 200, 70, 255),
Vec2::new(0.0, 0.0), 10.0,
Vec2::new(0.0, 0.0)
)?;
while !controls.should_close {
cam_controls(&mut render, &mut controls, &mut cam)?;
controls.update(&cam);
// load/unload resources
if controls.kb.press(Key::L) ||
controls.c.press(0, controller::Button::A)
{
render.texture_manager.unload_from_gameobject(ephemeral_obj);
render.font_manager.unload_text_obj(text);
if is_gaia {
ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/error.png"))?);
text = render.font_manager.load_text_obj(
&mono_font,
"Error Text",
Colour::new(200, 100, 70, 255),
Vec2::new(100.0, 0.0),
10.0,
Vec2::new(0.0, 0.0))?;
text.parallax = Vec2::new(1.0, 1.0);
} else {
ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/gaia.png"))?);
text = render.font_manager.load_text_obj(
&mono_font,
"The Planet Earth",
Colour::new(100, 200, 70, 255),
Vec2::new(0.0, 0.0),
10.0,
Vec2::new(0.0, 0.0))?;
}
is_gaia = !is_gaia;
}
ephemeral_obj.rotate += controls.frame_elapsed * 30.0;
if controls.kb.press(Key::F) {
ephemeral_obj.flip_horizontal = !ephemeral_obj.flip_horizontal;
}
if controls.kb.press(Key::P) {
audio.sfx.play(sfx)?;
}
if controls.c.press(0, controller::Button::DPadUp) {
controls.c.rumble(0, 10000, 20000, 1000);
}
render.start_draw();
map.draw(&mut cam);
cam.draw(&ephemeral_obj);
cam.draw_text(&text);
cam.draw_disposable_text(
&mono_font,
format!("Wheel: {}", controls.m.wheel()),
40,
controls.m.pos(),
Colour::white(),
Vec2::new(1.0, 1.0));
render.end_draw(&mut cam)?;
}
Ok(())
}
pub fn new( texture: Texture, rect: Rect, tex_rect: Option<Rect>, parallax: Vec2, colour: Colour ) -> Self
Trait Implementations§
source§impl Clone for GameObject
impl Clone for GameObject
source§fn clone(&self) -> GameObject
fn clone(&self) -> GameObject
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read more