Guest User

Untitled

a guest
Sep 24th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. extern crate piston_window;
  2. extern crate rand;
  3. extern crate array_init;
  4. extern crate hsl;
  5.  
  6. use piston_window::*;
  7. use rand::Rng;
  8. use hsl::*;
  9.  
  10. const CUBE_SIZE: i32 = 100;
  11. const NUM_CUBES: usize = 8;
  12.  
  13. const PIXEL_SIZE: i32 = 10; //CUBE_SIZE / PIXEL_SIZE should be a whole number. This is used to split the rendering of the cubes into pixels, as to have diffrent colors
  14.  
  15. fn main() {
  16. let mut window: PistonWindow = WindowSettings::new("Window", [800, 800]).exit_on_esc(true).build().unwrap();
  17. let mut cube_array: [Cube; NUM_CUBES] = array_init::array_init(|_i| Cube::new(&window));
  18.  
  19. let max = CUBE_SIZE / PIXEL_SIZE;
  20.  
  21. let mut time = 0;
  22. let mul = 2;
  23.  
  24. while let Some(event) = window.next() {
  25. let mut local_pos_array: [Position; NUM_CUBES] = array_init::array_init(|i| cube_array[i].get_local(&window));
  26. let size = window.size();
  27.  
  28. window.draw_2d(&event, |context, graphics| {
  29. clear([0.1; 4], graphics);
  30. for i in 0..NUM_CUBES {
  31. let pos = &local_pos_array[i];
  32.  
  33.  
  34. for x in 0..max {
  35. for y in 0..max {
  36. let color = HSL {
  37. h: ((pos.x + x * PIXEL_SIZE) as f64 / (size.width as i32 - CUBE_SIZE) as f64) * 360.0,
  38. s: 0.5,
  39. l: {
  40. let mut l = (pos.y + y * PIXEL_SIZE) as f64 / (size.height as i32 - CUBE_SIZE) as f64;
  41. if l < 0.0 {
  42. l = 0.0;
  43. }
  44. if l > 1.0 {
  45. l = 1.0;
  46. }
  47. l = l * 0.7 + 0.15; //Get a range between 0.15 and 0.85
  48. 1.0 - l
  49. }
  50. }.to_rgb();
  51. rectangle([color.0 as f32 / 255.0, color.1 as f32 / 255.0, color.2 as f32 / 255.0, 1.0],
  52. [(pos.x + x * PIXEL_SIZE) as f64, (pos.y + y * PIXEL_SIZE) as f64, PIXEL_SIZE as f64, PIXEL_SIZE as f64],
  53. context.transform,
  54. graphics);
  55. }
  56.  
  57. }
  58.  
  59. }
  60. });
  61.  
  62. time += 1;
  63. if time % mul != 0 {
  64. continue;
  65. }
  66. let position_array: [Position; NUM_CUBES] = array_init::array_init(|i| cube_array[i].get_position());
  67. for i in 0..NUM_CUBES {
  68. let cube = &mut cube_array[i];
  69. cube.tick(&window, &position_array, i as i32)
  70. }
  71. }
  72. }
  73.  
  74.  
  75. struct Cube {
  76. pub r: f32,
  77. pub g: f32,
  78. pub b: f32,
  79.  
  80. x_pos: i32,
  81. y_pos: i32,
  82.  
  83. x_dir: i32,
  84. y_dir: i32
  85. }
  86.  
  87. impl Cube {
  88.  
  89. pub fn new(window: &PistonWindow) -> Cube {
  90. let position: Position = window.get_position().unwrap();
  91. let size: Size = window.size();
  92. let mut rng = rand::thread_rng();
  93.  
  94. let mut x = 1;
  95. if rng.gen_bool(0.5) {
  96. x = -1;
  97. }
  98.  
  99. let mut y = 1;
  100. if rng.gen_bool(0.5) {
  101. y = -1;
  102. }
  103.  
  104. Cube {
  105. r: rng.gen(),
  106. g: rng.gen(),
  107. b: rng.gen(),
  108.  
  109. x_pos: position.x + rng.gen_range(0, size.height as i32 - CUBE_SIZE),
  110. y_pos: position.y + rng.gen_range(0, size.height as i32 - CUBE_SIZE),
  111.  
  112. x_dir: x,
  113. y_dir: y
  114. }
  115. }
  116.  
  117. pub fn get_local(&self, window: &PistonWindow) -> Position {
  118. let position: Position = window.get_position().unwrap();
  119. Position {
  120. x: self.x_pos - position.x,
  121. y: self.y_pos - position.y
  122. }
  123. }
  124.  
  125. pub fn tick(&mut self, window: &PistonWindow, positions: &[Position; NUM_CUBES], index: i32) {
  126. let pos: Position = self.get_local(window);
  127. let size: Size = window.size();
  128. let window_pos = window.get_position().unwrap();
  129.  
  130. //Detect wall collision
  131. if pos.x < 0 || pos.x + CUBE_SIZE > size.width as i32 {
  132. self.x_dir *= -1;
  133. if pos.x < 0 { //If outside of the window, go into the window
  134. self.x_pos = window_pos.x;
  135. } else {
  136. self.x_pos = window_pos.x + size.width as i32 - CUBE_SIZE;
  137. }
  138. }
  139.  
  140. if pos.y < 0 || pos.y + CUBE_SIZE > size.height as i32 {
  141. self.y_dir *= -1;
  142. if pos.y < 0 { //If outside of the window, go into the window
  143. self.y_pos = window_pos.y;
  144. } else {
  145. self.y_pos = window_pos.y + size.height as i32 - CUBE_SIZE;
  146. }
  147. }
  148.  
  149. //Detect cube collision
  150. for i in 0..NUM_CUBES {
  151. if i as i32 == index { //Don't collide with self
  152. continue;
  153. }
  154. let pos = positions[i];
  155.  
  156. let mut dest_x = pos.x + CUBE_SIZE + 1;
  157. let mut dest_y = pos.y + CUBE_SIZE + 1;
  158.  
  159. let mut intersect_x = (pos.x + CUBE_SIZE) - self.x_pos;
  160. let mut intersect_y = (pos.y + CUBE_SIZE) - self.y_pos;
  161.  
  162. //Im getting the intersect from the bottom of the cube, meaning that the top of the cube may still be intersected
  163. if intersect_x > CUBE_SIZE && intersect_x < 2 * CUBE_SIZE {
  164. intersect_x = CUBE_SIZE - (intersect_x - CUBE_SIZE);
  165. dest_x = pos.x - CUBE_SIZE - 1;
  166. }
  167. if intersect_y > CUBE_SIZE && intersect_y < 2 * CUBE_SIZE {
  168. intersect_y = CUBE_SIZE - (intersect_y - CUBE_SIZE);
  169. dest_y = pos.y - CUBE_SIZE - 1;
  170. }
  171.  
  172. if intersect_x > 0 && intersect_x < CUBE_SIZE && intersect_y > 0 && intersect_y < CUBE_SIZE {
  173. if intersect_x == intersect_y {//When intersections are the same, meaning that the cubes are colliding at 45 degrees to each other.
  174. self.x_dir *= -1;
  175. self.y_dir *= -1;
  176. self.x_pos = dest_x;
  177. self.y_pos = dest_y;
  178. } else if intersect_x > intersect_y {
  179. self.y_dir *= -1;
  180. self.y_pos = dest_y;
  181. } else {
  182. self.x_dir *= -1;
  183. self.x_pos = dest_x;
  184. }
  185.  
  186. }
  187.  
  188. }
  189.  
  190. self.x_pos += self.x_dir;
  191. self.y_pos += self.y_dir;
  192.  
  193. }
  194.  
  195. pub fn get_position(&self) -> Position {
  196. Position {
  197. x: self.x_pos,
  198. y: self.y_pos
  199. }
  200. }
  201. }
Add Comment
Please, Sign In to add comment