Skip to content

Instantly share code, notes, and snippets.

@andreivasiliu
Last active June 14, 2020 11:17
Show Gist options
  • Save andreivasiliu/3ce6e2a97900ac627401194211da4583 to your computer and use it in GitHub Desktop.
Save andreivasiliu/3ce6e2a97900ac627401194211da4583 to your computer and use it in GitHub Desktop.
Tile RTS

Things I need to solve:

  • Coordinate system
  • Collision detection, if any
  • Collision shapes
  • Physics, if any
  • Nudging, if any
  • Path-finding
  • Map structure

Coordinate system

  • Most likely 2D tiles, hex would be prettier but be a real pain to manage.
  • Units must be able to occupy multiple tiles.
    • Although is that really necessary?
    • Not really, but it would look too ugly.
    • What if unit tiles were much bigger than map tiles though?
    • That is actually acceptable. Hmm.

Map structure

  • Static tiles. Possibly with multiple vertical layers, but that's optional.
  • A tile on the map has the same size as a tile on a unit, although they may not be part of the same coordinate system.

Path-finding

  • What's the MVP?
[package]
name = "tilerts"
version = "0.1.0"
authors = ["Andrei Vasiliu <whyte.vuhuni@gmail.com>"]
edition = "2018"
[dependencies]
ggez = "0.5.0-rc.1"
use ggez::event::{self, EventHandler, MouseButton};
use ggez::graphics;
use ggez::{Context, ContextBuilder, GameResult};
#[derive(Default)]
struct MyGame {
focus_color: f32,
focused: bool,
selecting: Option<(f32, f32, f32, f32)>,
}
impl EventHandler for MyGame {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
if self.focused && self.focus_color < 0.3 {
self.focus_color += 0.01;
} else if !self.focused && self.focus_color > 0.2 {
self.focus_color -= 0.01;
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
graphics::clear(ctx, (0.2, 0.2, self.focus_color).into());
if let Some((x1, y1, x2, y2)) = self.selecting {
let rect =
graphics::Rect::new(x1.min(x2), y1.min(y2), (x2 - x1).abs(), (y2 - y1).abs());
for (color, fill) in &[
((0.0, 0.0, 0.0, 0.2).into(), graphics::DrawMode::fill()),
(graphics::BLACK, graphics::DrawMode::stroke(1.0)),
] {
let mesh = graphics::Mesh::new_rectangle(ctx, *fill, rect, graphics::BLACK)?;
graphics::draw(ctx, &mesh, graphics::DrawParam::default().color(*color))?;
}
}
graphics::present(ctx)?;
Ok(())
}
fn mouse_button_down_event(&mut self, _ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
if button == MouseButton::Left && x > 0.0 && y > 0.0 {
self.selecting = Some((x, y, x, y));
}
}
fn mouse_button_up_event(&mut self, _ctx: &mut Context, button: MouseButton, _x: f32, _y: f32) {
if button == MouseButton::Left {
self.selecting = None;
}
}
fn mouse_motion_event(&mut self, _ctx: &mut Context, x: f32, y: f32, _dx: f32, _dy: f32) {
if let Some((x1, y1, _, _)) = self.selecting {
self.selecting = Some((x1, y1, x, y));
}
}
fn focus_event(&mut self, _ctx: &mut Context, gained: bool) {
self.focused = gained;
self.selecting = None;
}
}
fn main() -> GameResult {
let (mut ctx, mut event_loop) = ContextBuilder::new("MyId", "MyAuthor")
.window_setup(ggez::conf::WindowSetup {
title: "Tile RTS".into(),
..Default::default()
})
.window_mode(ggez::conf::WindowMode {
width: 1024.0,
height: 768.0,
resizable: true,
..Default::default()
})
.build()?;
let mut my_game = MyGame::default();
event::run(&mut ctx, &mut event_loop, &mut my_game)?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment