├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── assets ├── Bullet.png ├── E-100_Base.png ├── E-100_Base_destroyed.png ├── E-100_Turret.png └── E-100_Turret_destroyed.png └── src ├── main.rs └── object.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | rundbg.sh 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "piston-tutorial" 3 | version = "0.5.0" 4 | dependencies = [ 5 | "find_folder 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "gfx 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "gfx_device_gl 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 8 | "nalgebra 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 9 | "ncollide 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", 10 | "piston2d-gfx_graphics 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", 11 | "piston_window 0.25.0 (registry+https://github.com/rust-lang/crates.io-index)", 12 | ] 13 | 14 | [[package]] 15 | name = "advapi32-sys" 16 | version = "0.1.2" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | dependencies = [ 19 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 20 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 21 | ] 22 | 23 | [[package]] 24 | name = "android_glue" 25 | version = "0.1.1" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | 28 | [[package]] 29 | name = "bitflags" 30 | version = "0.1.1" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | 33 | [[package]] 34 | name = "bitflags" 35 | version = "0.3.2" 36 | source = "registry+https://github.com/rust-lang/crates.io-index" 37 | 38 | [[package]] 39 | name = "byteorder" 40 | version = "0.3.13" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | 43 | [[package]] 44 | name = "cgl" 45 | version = "0.1.0" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | dependencies = [ 48 | "gleam 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", 49 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 50 | ] 51 | 52 | [[package]] 53 | name = "clock_ticks" 54 | version = "0.0.6" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | dependencies = [ 57 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 58 | ] 59 | 60 | [[package]] 61 | name = "cocoa" 62 | version = "0.1.5" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | dependencies = [ 65 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 66 | "core-graphics 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 67 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 68 | "objc 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", 69 | ] 70 | 71 | [[package]] 72 | name = "color_quant" 73 | version = "1.0.0" 74 | source = "registry+https://github.com/rust-lang/crates.io-index" 75 | 76 | [[package]] 77 | name = "core-foundation" 78 | version = "0.1.0" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | dependencies = [ 81 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 82 | ] 83 | 84 | [[package]] 85 | name = "core-graphics" 86 | version = "0.1.1" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | dependencies = [ 89 | "core-foundation 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 90 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 91 | ] 92 | 93 | [[package]] 94 | name = "dlib" 95 | version = "0.1.1" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | dependencies = [ 98 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 99 | ] 100 | 101 | [[package]] 102 | name = "draw_state" 103 | version = "0.2.0" 104 | source = "registry+https://github.com/rust-lang/crates.io-index" 105 | dependencies = [ 106 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 107 | ] 108 | 109 | [[package]] 110 | name = "dwmapi-sys" 111 | version = "0.1.0" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | dependencies = [ 114 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 115 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 116 | ] 117 | 118 | [[package]] 119 | name = "dylib" 120 | version = "0.0.1" 121 | source = "registry+https://github.com/rust-lang/crates.io-index" 122 | dependencies = [ 123 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 124 | ] 125 | 126 | [[package]] 127 | name = "enum_primitive" 128 | version = "0.0.1" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | dependencies = [ 131 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 132 | ] 133 | 134 | [[package]] 135 | name = "find_folder" 136 | version = "0.3.0" 137 | source = "registry+https://github.com/rust-lang/crates.io-index" 138 | 139 | [[package]] 140 | name = "flate2" 141 | version = "0.2.9" 142 | source = "registry+https://github.com/rust-lang/crates.io-index" 143 | dependencies = [ 144 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 145 | "miniz-sys 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 146 | ] 147 | 148 | [[package]] 149 | name = "freetype-rs" 150 | version = "0.3.1" 151 | source = "registry+https://github.com/rust-lang/crates.io-index" 152 | dependencies = [ 153 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 154 | "freetype-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 155 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 156 | ] 157 | 158 | [[package]] 159 | name = "freetype-sys" 160 | version = "0.1.2" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | dependencies = [ 163 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 164 | "libz-sys 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 165 | "pkg-config 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 166 | ] 167 | 168 | [[package]] 169 | name = "gcc" 170 | version = "0.3.19" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | dependencies = [ 173 | "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 174 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 175 | ] 176 | 177 | [[package]] 178 | name = "gdi32-sys" 179 | version = "0.1.1" 180 | source = "registry+https://github.com/rust-lang/crates.io-index" 181 | dependencies = [ 182 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 183 | ] 184 | 185 | [[package]] 186 | name = "gfx" 187 | version = "0.7.2" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | dependencies = [ 190 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 191 | "draw_state 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 192 | "log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 193 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 194 | ] 195 | 196 | [[package]] 197 | name = "gfx_device_gl" 198 | version = "0.5.0" 199 | source = "registry+https://github.com/rust-lang/crates.io-index" 200 | dependencies = [ 201 | "gfx 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", 202 | "gfx_gl 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 203 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 204 | "log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 205 | ] 206 | 207 | [[package]] 208 | name = "gfx_gl" 209 | version = "0.1.4" 210 | source = "registry+https://github.com/rust-lang/crates.io-index" 211 | dependencies = [ 212 | "gl_common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 213 | "gl_generator 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 214 | "khronos_api 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 215 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 216 | ] 217 | 218 | [[package]] 219 | name = "gif" 220 | version = "0.5.1" 221 | source = "registry+https://github.com/rust-lang/crates.io-index" 222 | dependencies = [ 223 | "color_quant 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 224 | "lzw 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 225 | ] 226 | 227 | [[package]] 228 | name = "gl" 229 | version = "0.1.0" 230 | source = "registry+https://github.com/rust-lang/crates.io-index" 231 | dependencies = [ 232 | "gl_common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 233 | "gl_generator 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 234 | "khronos_api 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 235 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 236 | ] 237 | 238 | [[package]] 239 | name = "gl_common" 240 | version = "0.1.0" 241 | source = "registry+https://github.com/rust-lang/crates.io-index" 242 | dependencies = [ 243 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 244 | ] 245 | 246 | [[package]] 247 | name = "gl_generator" 248 | version = "0.1.0" 249 | source = "registry+https://github.com/rust-lang/crates.io-index" 250 | dependencies = [ 251 | "khronos_api 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 252 | "log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 253 | "xml-rs 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)", 254 | ] 255 | 256 | [[package]] 257 | name = "gleam" 258 | version = "0.1.13" 259 | source = "registry+https://github.com/rust-lang/crates.io-index" 260 | dependencies = [ 261 | "gl_common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 262 | "gl_generator 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 263 | "khronos_api 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 264 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 265 | ] 266 | 267 | [[package]] 268 | name = "glob" 269 | version = "0.2.10" 270 | source = "registry+https://github.com/rust-lang/crates.io-index" 271 | 272 | [[package]] 273 | name = "glutin" 274 | version = "0.4.0" 275 | source = "registry+https://github.com/rust-lang/crates.io-index" 276 | dependencies = [ 277 | "android_glue 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 278 | "cgl 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 279 | "cocoa 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", 280 | "core-foundation 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 281 | "core-graphics 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 282 | "dwmapi-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 283 | "gdi32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 284 | "gl_common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 285 | "gl_generator 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 286 | "kernel32-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 287 | "khronos_api 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 288 | "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", 289 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 290 | "objc 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", 291 | "osmesa-sys 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", 292 | "shared_library 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 293 | "shell32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 294 | "user32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 295 | "wayland-client 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 296 | "wayland-kbd 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 297 | "wayland-window 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 298 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 299 | "x11-dl 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 300 | ] 301 | 302 | [[package]] 303 | name = "image" 304 | version = "0.4.1" 305 | source = "registry+https://github.com/rust-lang/crates.io-index" 306 | dependencies = [ 307 | "byteorder 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", 308 | "enum_primitive 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 309 | "gif 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", 310 | "glob 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", 311 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 312 | "png 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 313 | ] 314 | 315 | [[package]] 316 | name = "interpolation" 317 | version = "0.1.0" 318 | source = "registry+https://github.com/rust-lang/crates.io-index" 319 | 320 | [[package]] 321 | name = "kernel32-sys" 322 | version = "0.1.4" 323 | source = "registry+https://github.com/rust-lang/crates.io-index" 324 | dependencies = [ 325 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 326 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 327 | ] 328 | 329 | [[package]] 330 | name = "khronos_api" 331 | version = "0.0.8" 332 | source = "registry+https://github.com/rust-lang/crates.io-index" 333 | 334 | [[package]] 335 | name = "lazy_static" 336 | version = "0.1.15" 337 | source = "registry+https://github.com/rust-lang/crates.io-index" 338 | 339 | [[package]] 340 | name = "libc" 341 | version = "0.1.10" 342 | source = "registry+https://github.com/rust-lang/crates.io-index" 343 | 344 | [[package]] 345 | name = "libz-sys" 346 | version = "0.1.9" 347 | source = "registry+https://github.com/rust-lang/crates.io-index" 348 | dependencies = [ 349 | "gcc 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", 350 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 351 | "pkg-config 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 352 | ] 353 | 354 | [[package]] 355 | name = "log" 356 | version = "0.3.2" 357 | source = "registry+https://github.com/rust-lang/crates.io-index" 358 | dependencies = [ 359 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 360 | ] 361 | 362 | [[package]] 363 | name = "lzw" 364 | version = "0.8.0" 365 | source = "registry+https://github.com/rust-lang/crates.io-index" 366 | 367 | [[package]] 368 | name = "malloc_buf" 369 | version = "0.0.5" 370 | source = "registry+https://github.com/rust-lang/crates.io-index" 371 | dependencies = [ 372 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 373 | ] 374 | 375 | [[package]] 376 | name = "miniz-sys" 377 | version = "0.1.6" 378 | source = "registry+https://github.com/rust-lang/crates.io-index" 379 | dependencies = [ 380 | "gcc 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", 381 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 382 | ] 383 | 384 | [[package]] 385 | name = "mmap" 386 | version = "0.1.1" 387 | source = "registry+https://github.com/rust-lang/crates.io-index" 388 | dependencies = [ 389 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 390 | "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 391 | ] 392 | 393 | [[package]] 394 | name = "nalgebra" 395 | version = "0.4.0" 396 | source = "registry+https://github.com/rust-lang/crates.io-index" 397 | dependencies = [ 398 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 399 | "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", 400 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 401 | ] 402 | 403 | [[package]] 404 | name = "ncollide" 405 | version = "0.6.0" 406 | source = "registry+https://github.com/rust-lang/crates.io-index" 407 | dependencies = [ 408 | "ncollide_entities 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 409 | "ncollide_math 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 410 | "ncollide_pipeline 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 411 | "ncollide_procedural 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 412 | "ncollide_queries 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 413 | "ncollide_transformation 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 414 | "ncollide_utils 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 415 | ] 416 | 417 | [[package]] 418 | name = "ncollide_entities" 419 | version = "0.4.0" 420 | source = "registry+https://github.com/rust-lang/crates.io-index" 421 | dependencies = [ 422 | "nalgebra 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 423 | "ncollide_math 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 424 | "ncollide_utils 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 425 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 426 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 427 | ] 428 | 429 | [[package]] 430 | name = "ncollide_math" 431 | version = "0.2.1" 432 | source = "registry+https://github.com/rust-lang/crates.io-index" 433 | dependencies = [ 434 | "nalgebra 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 435 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 436 | "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", 437 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 438 | ] 439 | 440 | [[package]] 441 | name = "ncollide_pipeline" 442 | version = "0.5.0" 443 | source = "registry+https://github.com/rust-lang/crates.io-index" 444 | dependencies = [ 445 | "nalgebra 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 446 | "ncollide_entities 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 447 | "ncollide_math 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 448 | "ncollide_queries 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 449 | "ncollide_utils 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 450 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 451 | ] 452 | 453 | [[package]] 454 | name = "ncollide_procedural" 455 | version = "0.2.4" 456 | source = "registry+https://github.com/rust-lang/crates.io-index" 457 | dependencies = [ 458 | "nalgebra 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 459 | "ncollide_math 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 460 | "ncollide_utils 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 461 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 462 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 463 | ] 464 | 465 | [[package]] 466 | name = "ncollide_queries" 467 | version = "0.3.1" 468 | source = "registry+https://github.com/rust-lang/crates.io-index" 469 | dependencies = [ 470 | "nalgebra 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 471 | "ncollide_entities 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 472 | "ncollide_math 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 473 | "ncollide_utils 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 474 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 475 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 476 | ] 477 | 478 | [[package]] 479 | name = "ncollide_transformation" 480 | version = "0.2.2" 481 | source = "registry+https://github.com/rust-lang/crates.io-index" 482 | dependencies = [ 483 | "nalgebra 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 484 | "ncollide_entities 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 485 | "ncollide_math 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 486 | "ncollide_procedural 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 487 | "ncollide_queries 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 488 | "ncollide_utils 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 489 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 490 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 491 | ] 492 | 493 | [[package]] 494 | name = "ncollide_utils" 495 | version = "0.2.4" 496 | source = "registry+https://github.com/rust-lang/crates.io-index" 497 | dependencies = [ 498 | "nalgebra 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 499 | "ncollide_math 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 500 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 501 | "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", 502 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 503 | ] 504 | 505 | [[package]] 506 | name = "num" 507 | version = "0.1.27" 508 | source = "registry+https://github.com/rust-lang/crates.io-index" 509 | dependencies = [ 510 | "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", 511 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 512 | ] 513 | 514 | [[package]] 515 | name = "objc" 516 | version = "0.1.7" 517 | source = "registry+https://github.com/rust-lang/crates.io-index" 518 | dependencies = [ 519 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 520 | "malloc_buf 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", 521 | ] 522 | 523 | [[package]] 524 | name = "osmesa-sys" 525 | version = "0.0.5" 526 | source = "registry+https://github.com/rust-lang/crates.io-index" 527 | dependencies = [ 528 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 529 | "shared_library 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 530 | ] 531 | 532 | [[package]] 533 | name = "piston" 534 | version = "0.13.0" 535 | source = "registry+https://github.com/rust-lang/crates.io-index" 536 | dependencies = [ 537 | "pistoncore-event_loop 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 538 | "pistoncore-input 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 539 | "pistoncore-window 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", 540 | ] 541 | 542 | [[package]] 543 | name = "piston-float" 544 | version = "0.2.0" 545 | source = "registry+https://github.com/rust-lang/crates.io-index" 546 | 547 | [[package]] 548 | name = "piston-gfx_texture" 549 | version = "0.4.0" 550 | source = "registry+https://github.com/rust-lang/crates.io-index" 551 | dependencies = [ 552 | "gfx 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", 553 | "image 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 554 | "piston-texture 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 555 | ] 556 | 557 | [[package]] 558 | name = "piston-shaders_graphics2d" 559 | version = "0.1.0" 560 | source = "registry+https://github.com/rust-lang/crates.io-index" 561 | 562 | [[package]] 563 | name = "piston-texture" 564 | version = "0.2.3" 565 | source = "registry+https://github.com/rust-lang/crates.io-index" 566 | 567 | [[package]] 568 | name = "piston-viewport" 569 | version = "0.2.0" 570 | source = "registry+https://github.com/rust-lang/crates.io-index" 571 | dependencies = [ 572 | "piston-float 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 573 | ] 574 | 575 | [[package]] 576 | name = "piston2d-gfx_graphics" 577 | version = "0.11.0" 578 | source = "registry+https://github.com/rust-lang/crates.io-index" 579 | dependencies = [ 580 | "freetype-rs 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 581 | "gfx 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", 582 | "piston-gfx_texture 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 583 | "piston-shaders_graphics2d 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 584 | "piston2d-graphics 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", 585 | ] 586 | 587 | [[package]] 588 | name = "piston2d-graphics" 589 | version = "0.10.0" 590 | source = "registry+https://github.com/rust-lang/crates.io-index" 591 | dependencies = [ 592 | "draw_state 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 593 | "interpolation 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 594 | "piston-texture 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 595 | "piston-viewport 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 596 | "read_color 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 597 | "vecmath 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 598 | ] 599 | 600 | [[package]] 601 | name = "piston_window" 602 | version = "0.25.0" 603 | source = "registry+https://github.com/rust-lang/crates.io-index" 604 | dependencies = [ 605 | "gfx 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", 606 | "gfx_device_gl 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 607 | "piston 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", 608 | "piston2d-gfx_graphics 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", 609 | "piston2d-graphics 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", 610 | "pistoncore-glutin_window 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", 611 | "shader_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 612 | ] 613 | 614 | [[package]] 615 | name = "pistoncore-event_loop" 616 | version = "0.13.0" 617 | source = "registry+https://github.com/rust-lang/crates.io-index" 618 | dependencies = [ 619 | "clock_ticks 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)", 620 | "piston-viewport 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 621 | "pistoncore-input 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 622 | "pistoncore-window 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", 623 | ] 624 | 625 | [[package]] 626 | name = "pistoncore-glutin_window" 627 | version = "0.16.0" 628 | source = "registry+https://github.com/rust-lang/crates.io-index" 629 | dependencies = [ 630 | "gl 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 631 | "glutin 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 632 | "pistoncore-input 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 633 | "pistoncore-window 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", 634 | "shader_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 635 | ] 636 | 637 | [[package]] 638 | name = "pistoncore-input" 639 | version = "0.8.0" 640 | source = "registry+https://github.com/rust-lang/crates.io-index" 641 | dependencies = [ 642 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 643 | "piston-viewport 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 644 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", 645 | ] 646 | 647 | [[package]] 648 | name = "pistoncore-window" 649 | version = "0.12.0" 650 | source = "registry+https://github.com/rust-lang/crates.io-index" 651 | dependencies = [ 652 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 653 | "pistoncore-input 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 654 | "shader_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 655 | ] 656 | 657 | [[package]] 658 | name = "pkg-config" 659 | version = "0.3.6" 660 | source = "registry+https://github.com/rust-lang/crates.io-index" 661 | 662 | [[package]] 663 | name = "png" 664 | version = "0.3.1" 665 | source = "registry+https://github.com/rust-lang/crates.io-index" 666 | dependencies = [ 667 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 668 | "flate2 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", 669 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 670 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", 671 | ] 672 | 673 | [[package]] 674 | name = "rand" 675 | version = "0.3.11" 676 | source = "registry+https://github.com/rust-lang/crates.io-index" 677 | dependencies = [ 678 | "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 679 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 680 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 681 | ] 682 | 683 | [[package]] 684 | name = "read_color" 685 | version = "0.1.0" 686 | source = "registry+https://github.com/rust-lang/crates.io-index" 687 | 688 | [[package]] 689 | name = "rustc-serialize" 690 | version = "0.3.16" 691 | source = "registry+https://github.com/rust-lang/crates.io-index" 692 | 693 | [[package]] 694 | name = "shader_version" 695 | version = "0.2.1" 696 | source = "registry+https://github.com/rust-lang/crates.io-index" 697 | 698 | [[package]] 699 | name = "shared_library" 700 | version = "0.1.0" 701 | source = "registry+https://github.com/rust-lang/crates.io-index" 702 | dependencies = [ 703 | "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", 704 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 705 | ] 706 | 707 | [[package]] 708 | name = "shell32-sys" 709 | version = "0.1.1" 710 | source = "registry+https://github.com/rust-lang/crates.io-index" 711 | dependencies = [ 712 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 713 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 714 | ] 715 | 716 | [[package]] 717 | name = "tempdir" 718 | version = "0.3.4" 719 | source = "registry+https://github.com/rust-lang/crates.io-index" 720 | dependencies = [ 721 | "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", 722 | ] 723 | 724 | [[package]] 725 | name = "tempfile" 726 | version = "1.1.1" 727 | source = "registry+https://github.com/rust-lang/crates.io-index" 728 | dependencies = [ 729 | "kernel32-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 730 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 731 | "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", 732 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 733 | ] 734 | 735 | [[package]] 736 | name = "user32-sys" 737 | version = "0.1.2" 738 | source = "registry+https://github.com/rust-lang/crates.io-index" 739 | dependencies = [ 740 | "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", 741 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 742 | ] 743 | 744 | [[package]] 745 | name = "vecmath" 746 | version = "0.2.0" 747 | source = "registry+https://github.com/rust-lang/crates.io-index" 748 | dependencies = [ 749 | "piston-float 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 750 | ] 751 | 752 | [[package]] 753 | name = "wayland-client" 754 | version = "0.2.1" 755 | source = "registry+https://github.com/rust-lang/crates.io-index" 756 | dependencies = [ 757 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 758 | "dlib 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 759 | "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", 760 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 761 | ] 762 | 763 | [[package]] 764 | name = "wayland-kbd" 765 | version = "0.2.0" 766 | source = "registry+https://github.com/rust-lang/crates.io-index" 767 | dependencies = [ 768 | "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 769 | "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", 770 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 771 | "mmap 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 772 | "wayland-client 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 773 | ] 774 | 775 | [[package]] 776 | name = "wayland-window" 777 | version = "0.1.0" 778 | source = "registry+https://github.com/rust-lang/crates.io-index" 779 | dependencies = [ 780 | "byteorder 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", 781 | "tempfile 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 782 | "wayland-client 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 783 | ] 784 | 785 | [[package]] 786 | name = "winapi" 787 | version = "0.2.4" 788 | source = "registry+https://github.com/rust-lang/crates.io-index" 789 | 790 | [[package]] 791 | name = "winapi-build" 792 | version = "0.1.1" 793 | source = "registry+https://github.com/rust-lang/crates.io-index" 794 | 795 | [[package]] 796 | name = "x11-dl" 797 | version = "2.0.1" 798 | source = "registry+https://github.com/rust-lang/crates.io-index" 799 | dependencies = [ 800 | "dylib 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 801 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 802 | ] 803 | 804 | [[package]] 805 | name = "xml-rs" 806 | version = "0.1.26" 807 | source = "registry+https://github.com/rust-lang/crates.io-index" 808 | dependencies = [ 809 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 810 | ] 811 | 812 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "piston-tutorial" 3 | version = "0.5.0" 4 | authors = ["StelarCF "] 5 | 6 | [dependencies] 7 | piston_window = "0.25.0" 8 | find_folder = "*" 9 | gfx_device_gl = "0.5.0" 10 | gfx = "0.7.0" 11 | piston2d-gfx_graphics = "0.11.0" 12 | nalgebra = "*" 13 | ncollide = "*" 14 | -------------------------------------------------------------------------------- /assets/Bullet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StelarCF/piston-tutorial/d43765a75800493cd6901c0b8ca196b87b5287e4/assets/Bullet.png -------------------------------------------------------------------------------- /assets/E-100_Base.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StelarCF/piston-tutorial/d43765a75800493cd6901c0b8ca196b87b5287e4/assets/E-100_Base.png -------------------------------------------------------------------------------- /assets/E-100_Base_destroyed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StelarCF/piston-tutorial/d43765a75800493cd6901c0b8ca196b87b5287e4/assets/E-100_Base_destroyed.png -------------------------------------------------------------------------------- /assets/E-100_Turret.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StelarCF/piston-tutorial/d43765a75800493cd6901c0b8ca196b87b5287e4/assets/E-100_Turret.png -------------------------------------------------------------------------------- /assets/E-100_Turret_destroyed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StelarCF/piston-tutorial/d43765a75800493cd6901c0b8ca196b87b5287e4/assets/E-100_Turret_destroyed.png -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate piston_window; 2 | extern crate gfx_device_gl; 3 | extern crate find_folder; 4 | extern crate gfx_graphics; 5 | extern crate gfx; 6 | extern crate nalgebra; 7 | extern crate ncollide; 8 | 9 | use piston_window::*; 10 | use gfx_device_gl::{Resources}; 11 | 12 | use nalgebra::Vec2 as Vector2; 13 | type Vec2 = Vector2; 14 | 15 | mod object; 16 | use object::Object; 17 | use object::Tank; 18 | use object::Bullet; 19 | 20 | struct Game { 21 | player1: Tank, 22 | player2: Tank, 23 | bullets: Vec, 24 | hull_destroyed: Option>, 25 | turret_destroyed: Option>, 26 | bullet: Option>, 27 | up_d: bool, down_d: bool, left_d: bool, right_d: bool, 28 | scx: f64, scy: f64 29 | } 30 | 31 | impl Game { 32 | fn new() -> Game { 33 | Game { player1 : Tank::new(), player2: Tank::new(), bullets: Vec::::new(), up_d: false, down_d: false, left_d: false, right_d: false, scx: 300.0, scy: 300.0, 34 | hull_destroyed: None, turret_destroyed: None, bullet: None } 35 | } 36 | fn on_load(&mut self, w: &PistonWindow) { 37 | let assets = find_folder::Search::ParentsThenKids(3, 3).for_folder("assets").unwrap(); 38 | let tank_sprite = assets.join("E-100_Base.png"); 39 | let tank_sprite = Texture::from_path( 40 | &mut *w.factory.borrow_mut(), 41 | &tank_sprite, 42 | Flip::None, 43 | &TextureSettings::new()) 44 | .unwrap(); 45 | let tank_turret = assets.join("E-100_Turret.png"); 46 | let tank_turret = Texture::from_path( 47 | &mut *w.factory.borrow_mut(), 48 | &tank_turret, 49 | Flip::None, 50 | &TextureSettings::new()) 51 | .unwrap(); 52 | let tank_dest_sprite = assets.join("E-100_Base_destroyed.png"); 53 | let tank_dest_sprite = Texture::from_path( 54 | &mut *w.factory.borrow_mut(), 55 | &tank_dest_sprite, 56 | Flip::None, 57 | &TextureSettings::new()) 58 | .unwrap(); 59 | let tank_dest_turret = assets.join("E-100_Turret_destroyed.png"); 60 | let tank_dest_turret = Texture::from_path( 61 | &mut *w.factory.borrow_mut(), 62 | &tank_dest_turret, 63 | Flip::None, 64 | &TextureSettings::new()) 65 | .unwrap(); 66 | let bulsprite = assets.join("Bullet.png"); 67 | let bulsprite = Texture::from_path( 68 | &mut *w.factory.borrow_mut(), 69 | &bulsprite, 70 | Flip::None, 71 | &TextureSettings::new()) 72 | .unwrap(); 73 | self.hull_destroyed = Some(tank_dest_sprite); 74 | self.turret_destroyed = Some(tank_dest_turret); 75 | self.bullet = Some(bulsprite); 76 | 77 | self.player1.hull.set_sprite(tank_sprite.clone()); 78 | self.player1.turret.set_sprite(tank_turret.clone()); 79 | self.player2.hull.set_sprite(tank_sprite.clone()); 80 | self.player2.turret.set_sprite(tank_turret.clone()); 81 | self.player2.mov_to(Vec2::new(300.0, 0.0)); 82 | } 83 | fn on_update(&mut self, upd: UpdateArgs) { 84 | for bul in &mut self.bullets { 85 | if self.player1.collides(&bul) { 86 | self.player1.is_destroyed = true; 87 | self.player1.hull.set_sprite(self.hull_destroyed.clone().unwrap()); 88 | self.player1.turret.set_sprite(self.turret_destroyed.clone().unwrap()); 89 | bul.to_be_removed = true; 90 | } 91 | if self.player2.collides(&bul) { 92 | self.player2.is_destroyed = true; 93 | self.player2.hull.set_sprite(self.hull_destroyed.clone().unwrap()); 94 | self.player2.turret.set_sprite(self.turret_destroyed.clone().unwrap()); 95 | bul.to_be_removed = true; 96 | } 97 | bul.update(upd.dt); 98 | } 99 | self.bullets.retain(|ref bul| bul.to_be_removed == false); 100 | if self.up_d && !self.player1.is_destroyed { 101 | self.player1.fwd(150.0 * upd.dt); 102 | } 103 | if self.down_d && !self.player1.is_destroyed { 104 | self.player1.fwd(-150.0 * upd.dt); 105 | } 106 | if self.left_d && !self.player1.is_destroyed { 107 | self.player1.rot(-1.0 * upd.dt); 108 | } 109 | if self.right_d && !self.player1.is_destroyed { 110 | self.player1.rot(1.0 * upd.dt); 111 | } 112 | self.player1.update(upd.dt); 113 | self.player2.update(upd.dt); 114 | } 115 | fn on_draw(&mut self, ren: RenderArgs, e: PistonWindow) { 116 | self.scx = (ren.width / 2) as f64; 117 | self.scy = (ren.height / 2) as f64; 118 | e.draw_2d(|c, g| { 119 | clear([0.8, 0.8, 0.8, 1.0], g); 120 | let center = c.transform.trans(self.scx, self.scy); 121 | self.player1.render(g, center); 122 | self.player2.render(g, center); 123 | for bul in &self.bullets { 124 | bul.render(g, center); 125 | } 126 | }); 127 | } 128 | fn on_input(&mut self, inp: Input) { 129 | match inp { 130 | Input::Press(but) => { 131 | match but { 132 | Button::Keyboard(Key::Up) => { 133 | self.up_d = true; 134 | } 135 | Button::Keyboard(Key::Down) => { 136 | self.down_d = true; 137 | } 138 | Button::Keyboard(Key::Left) => { 139 | self.left_d = true; 140 | } 141 | Button::Keyboard(Key::Right) => { 142 | self.right_d = true; 143 | } 144 | _ => {} 145 | } 146 | } 147 | Input::Release(but) => { 148 | match but { 149 | Button::Keyboard(Key::Up) => { 150 | self.up_d = false; 151 | } 152 | Button::Keyboard(Key::Down) => { 153 | self.down_d = false; 154 | } 155 | Button::Keyboard(Key::Left) => { 156 | self.left_d = false; 157 | } 158 | Button::Keyboard(Key::Right) => { 159 | self.right_d = false; 160 | } 161 | Button::Mouse(MouseButton::Left) => { 162 | self.bullets.push(self.player1.fire(self.bullet.clone().unwrap())); 163 | } 164 | _ => {} 165 | } 166 | } 167 | Input::Move(mot) => { 168 | match mot { 169 | Motion::MouseCursor(x, y) => { 170 | self.player1.point_tur_to(x - self.scx, y - self.scy); 171 | } 172 | _ => {} 173 | } 174 | } 175 | _ => {} 176 | } 177 | } 178 | } 179 | 180 | fn main() { 181 | let window: PistonWindow = WindowSettings::new( 182 | "piston-tutorial", 183 | [600, 600] 184 | ) 185 | .exit_on_esc(true) 186 | .build() 187 | .unwrap(); 188 | let mut game = Game::new(); 189 | game.on_load(&window); 190 | for e in window { 191 | match e.event { 192 | Some(Event::Update(upd)) => { 193 | game.on_update(upd); 194 | } 195 | Some(Event::Render(ren)) => { 196 | game.on_draw(ren, e); 197 | } 198 | Some(Event::Input(inp)) => { 199 | game.on_input(inp); 200 | } 201 | _ => { 202 | 203 | } 204 | } 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /src/object.rs: -------------------------------------------------------------------------------- 1 | use piston_window::*; 2 | use gfx_device_gl::{Resources, Output, CommandBuffer}; 3 | use gfx_graphics::GfxGraphics; 4 | use std::f64::consts::PI; 5 | use nalgebra::Vec1 as Vector1; 6 | use nalgebra::Vec2 as Vector2; 7 | use nalgebra::Rot2 as Rotate2; 8 | use nalgebra::Pnt2 as Point2; 9 | 10 | use ncollide::point::PointQuery; 11 | use ncollide::shape::Cuboid2; 12 | 13 | pub type Vec1 = Vector1; 14 | pub type Vec2 = Vector2; 15 | pub type Rot2 = Rotate2; 16 | pub type Pnt2 = Point2; 17 | pub type Cuboid2f = Cuboid2; 18 | 19 | #[derive(Copy, Clone)] 20 | pub struct Transform { 21 | pos: Vec2, 22 | scale: Vec2, 23 | rot: f64 24 | } 25 | 26 | #[allow(dead_code)] 27 | impl Transform { 28 | fn new() -> Transform { 29 | Transform { pos: Vec2::new(0.0, 0.0), scale: Vec2::new(1.0, 1.0), rot: 0.0 } 30 | } 31 | pub fn mov(&mut self, v: Vec2) { 32 | self.pos = self.pos + v; 33 | } 34 | pub fn mov_to(&mut self, v: Vec2) { 35 | self.pos = v; 36 | } 37 | pub fn rot(&mut self, r: f64) { 38 | self.rot += r; 39 | } 40 | pub fn rot_to(&mut self, r: f64) { 41 | self.rot = r; 42 | } 43 | pub fn fwd(&mut self, d: f64) { 44 | self.pos.x += d * (-self.rot.sin()); 45 | self.pos.y += d * self.rot.cos(); 46 | } 47 | } 48 | 49 | pub struct Component { 50 | trans: Transform, 51 | sprite: Option> 52 | } 53 | 54 | impl Component { 55 | fn new() -> Component { 56 | Component { trans: Transform::new(), sprite: Option::None } 57 | } 58 | fn render(self: &Component, g: &mut GfxGraphics, Output>, view: math::Matrix2d) { 59 | let t: Transform = self.trans; 60 | match self.sprite { 61 | Some(ref sprite) => { 62 | let (spritex, spritey) = sprite.get_size(); 63 | let (ocx, ocy) = (spritex / 2, spritey / 2); 64 | image(sprite, view.trans(t.pos.x, t.pos.y).scale(t.scale.x, t.scale.y).rot_rad(t.rot).trans(-(ocx as f64), -(ocy as f64)), g); 65 | } 66 | _ => {} 67 | } 68 | } 69 | pub fn set_sprite(&mut self, sprite: Texture) { 70 | self.sprite = Some(sprite); 71 | } 72 | } 73 | 74 | pub trait Object { 75 | fn mov(&mut self, pos: Vec2); 76 | fn mov_to(&mut self, pos: Vec2); 77 | fn rot(&mut self, r: f64); 78 | fn rot_to(&mut self, r: f64); 79 | fn fwd(&mut self, d: f64); 80 | fn update(&mut self, dt: f64); 81 | fn render(&self, g: &mut GfxGraphics, Output>, view: math::Matrix2d); 82 | } 83 | 84 | pub struct Tank { 85 | pub hull: Component, 86 | pub turret: Component, 87 | collider: Cuboid2f, 88 | pub is_destroyed: bool, 89 | point_to: Vec2 90 | } 91 | 92 | pub struct Bullet { 93 | bullet: Component, 94 | pub to_be_removed: bool 95 | } 96 | 97 | #[allow(dead_code)] 98 | impl Object for Bullet { 99 | fn mov(&mut self, pos: Vec2) { 100 | self.bullet.trans.mov(pos); 101 | } 102 | fn mov_to(&mut self, pos: Vec2) { 103 | self.bullet.trans.mov_to(pos); 104 | } 105 | fn rot(&mut self, r: f64) { 106 | self.bullet.trans.rot(r); 107 | } 108 | fn rot_to(&mut self, r: f64) { 109 | self.bullet.trans.rot_to(r); 110 | } 111 | fn fwd(&mut self, d: f64) { 112 | self.bullet.trans.fwd(d); 113 | } 114 | fn update(&mut self, dt: f64) { 115 | let bullet_speed = 200.0; 116 | self.fwd(bullet_speed * dt); 117 | } 118 | fn render(&self, g: &mut GfxGraphics, Output>, view: math::Matrix2d) { 119 | self.bullet.render(g, view); 120 | } 121 | } 122 | 123 | impl Tank { 124 | pub fn new() -> Tank { 125 | Tank { hull: Component::new(), turret: Component::new(), collider: Cuboid2f::new(Vec2::new(38.0, 65.0)), point_to: Vec2::new(0.0, 0.0), is_destroyed: false } 126 | } 127 | pub fn point_tur_to(&mut self, x: f64, y: f64) { 128 | self.point_to = Vec2::new(x, y); 129 | } 130 | fn calc_tur_pos(&mut self, dt: f64) { 131 | let mut new_rot = (-(self.point_to.x - self.hull.trans.pos.x)).atan2(self.point_to.y - self.hull.trans.pos.y); 132 | if new_rot == self.turret.trans.rot { 133 | return; 134 | } 135 | if new_rot < self.turret.trans.rot && self.turret.trans.rot - new_rot > new_rot + 2.0 * PI - self.turret.trans.rot { 136 | new_rot += 2.0 * PI; 137 | } 138 | if new_rot > self.turret.trans.rot && new_rot - self.turret.trans.rot > self.turret.trans.rot + 2.0 * PI - new_rot { 139 | new_rot -= 2.0 * PI; 140 | } 141 | let rot_speed = 2.0; 142 | if new_rot > self.turret.trans.rot { 143 | if new_rot - self.turret.trans.rot > rot_speed * dt { 144 | self.turret.trans.rot += rot_speed * dt; 145 | } else { 146 | self.turret.trans.rot = new_rot; 147 | } 148 | } else { 149 | if self.turret.trans.rot - new_rot > rot_speed * dt { 150 | self.turret.trans.rot -= rot_speed * dt; 151 | } else { 152 | self.turret.trans.rot = new_rot; 153 | } 154 | } 155 | if self.turret.trans.rot > 2.0 * PI { 156 | self.turret.trans.rot -= 2.0 * PI; 157 | } 158 | if self.turret.trans.rot < 0.0 { 159 | self.turret.trans.rot += 2.0 * PI; 160 | } 161 | } 162 | pub fn collides(&mut self, b: &Bullet) -> bool { 163 | let bpnt = Pnt2::new(b.bullet.trans.pos.x, b.bullet.trans.pos.y); 164 | self.collider.contains_point(&self.hull.trans.pos, &bpnt) 165 | } 166 | pub fn fire(&self, sprite: Texture) -> Bullet { 167 | let mut bul = Bullet { bullet: Component::new(), to_be_removed: false }; 168 | bul.mov_to(self.turret.trans.pos); 169 | bul.rot_to(self.turret.trans.rot); 170 | bul.fwd(125.0); 171 | bul.bullet.set_sprite(sprite); 172 | bul 173 | } 174 | } 175 | 176 | #[allow(dead_code)] 177 | impl Object for Tank { 178 | fn mov(&mut self, pos: Vec2) { 179 | self.hull.trans.mov(pos); 180 | self.turret.trans.mov(pos); 181 | } 182 | fn mov_to(&mut self, pos: Vec2) { 183 | self.hull.trans.mov_to(pos); 184 | self.turret.trans.mov_to(pos); 185 | } 186 | fn rot(&mut self, r: f64) { 187 | self.hull.trans.rot(r); 188 | self.turret.trans.rot(r); 189 | } 190 | fn rot_to(&mut self, r: f64) { 191 | self.turret.trans.rot(r - self.hull.trans.rot); 192 | self.hull.trans.rot_to(r); 193 | } 194 | fn fwd(&mut self, d: f64) { 195 | self.hull.trans.fwd(d); 196 | self.turret.trans.pos = self.hull.trans.pos; 197 | } 198 | fn update(&mut self, dt: f64) { 199 | self.turret.trans.pos = self.hull.trans.pos; 200 | self.calc_tur_pos(dt); 201 | } 202 | fn render(&self, g: &mut GfxGraphics, Output>, view: math::Matrix2d) { 203 | self.hull.render(g, view); 204 | self.turret.render(g, view); 205 | } 206 | } 207 | --------------------------------------------------------------------------------