Advertisement
Guest User

term3d high-level code

a guest
Feb 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.94 KB | None | 0 0
  1. // https://github.com/asmoaesl/term3d/blob/master/examples/cube/src/main.rs
  2.  
  3. use term3d::*;
  4.  
  5. const COLORS: &[Color] = &[
  6.     Color::Red,   // face 1
  7.     Color::Green, // face 2
  8.     Color::Blue,  // face 3, etc.
  9.     Color::Yellow,
  10.     Color::White,
  11.     Color::Magenta,
  12. ];
  13.  
  14. struct App;
  15.  
  16. impl Game for App {
  17.     fn start(&mut self, term: &mut Term3D) {
  18.         // Initialize camera
  19.         term.cam.transform = Transform {
  20.             pos: (6., -2., -10.),
  21.             rot: (0.15, -0.5),
  22.         };
  23.  
  24.         // Create a cube Mesh with vertex and face data
  25.         let mut cube = Mesh::cube();
  26.         // Color the faces different colors
  27.         for i in 0..cube.faces.len() {
  28.             cube.faces[i].1 = Some(COLORS[i]);
  29.         }
  30.         // Create an object with the cube mesh
  31.         let obj = Object::new(cube);
  32.         // Add the object to the scene
  33.         term.objects.push(obj);
  34.     }
  35.  
  36.     fn update(&mut self, term: &mut Term3D, delta: f32, key: Option<Input>) {
  37.         if let Some(input) = key {
  38.             let s = delta * 10.;
  39.             let cam = &mut term.cam;
  40.  
  41.             match input {
  42.                 Input::Character('q') => cam.transform.pos.1 += s, // Go down
  43.                 Input::Character('e') => cam.transform.pos.1 -= s, // Go up
  44.  
  45.                 Input::Character('w')
  46.                 | Input::Character('a')
  47.                 | Input::Character('s')
  48.                 | Input::Character('d') => {
  49.                     let (x, y) = (s * cam.transform.rot.1.sin(), s * cam.transform.rot.1.cos());
  50.                     match input {
  51.                         Input::Character('w') => { // Forward
  52.                             cam.transform.pos.0 += x;
  53.                             cam.transform.pos.2 += y;
  54.                         }
  55.                         Input::Character('s') => { // Backward
  56.                             cam.transform.pos.0 -= x;
  57.                             cam.transform.pos.2 -= y;
  58.                         }
  59.                         Input::Character('a') => { // Left
  60.                             cam.transform.pos.0 -= y;
  61.                             cam.transform.pos.2 += x;
  62.                         }
  63.                         Input::Character('d') => { // Right
  64.                             cam.transform.pos.0 += y;
  65.                             cam.transform.pos.2 -= x;
  66.                         }
  67.                         _ => unreachable!(),
  68.                     }
  69.                 }
  70.  
  71.                 // The following inputs are for the arrow keys,
  72.                 // which in this example control looking around.
  73.                 Input::KeyUp => cam.transform.rot.0 -= s,
  74.                 Input::KeyDown => cam.transform.rot.0 += s,
  75.                 Input::KeyLeft => cam.transform.rot.1 -= s,
  76.                 Input::KeyRight => cam.transform.rot.1 += s,
  77.  
  78.                 _ => {}
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. fn main() {
  85.     let mut term3d = Term3D::new();
  86.     term3d.run(&mut App);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement