From 40ccab732f061bd78db7f4ecc75d6f10a6f54b3f Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 28 Dec 2011 02:16:43 -0500 Subject: [PATCH] Fun with explosions! Also reorganized code a bit. --- bullet.nls | 7 ++++++- explosion.nls | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lobo.nlogo | 55 ++++++++++++++++++++++++++++++++++--------------- player.nls | 30 +++++++++++++++++++++++---- tank.nls | 28 ++++++++++++++++++------- 5 files changed, 157 insertions(+), 29 deletions(-) create mode 100644 explosion.nls diff --git a/bullet.nls b/bullet.nls index 400b809..01f4b62 100644 --- a/bullet.nls +++ b/bullet.nls @@ -10,6 +10,10 @@ bullets-own [ travel-distance ] +;; ========== +;; Procedures +;; ========== + to fire-bullet hatch-bullets 1 [ set max-travel-distance 8 @@ -31,13 +35,14 @@ to do-bullet-logic fd speed set travel-distance travel-distance + speed if travel-distance > max-travel-distance [ + explode "decay" die ] let mxcor xcor let mycor ycor let is-close-enough false - let target min-one-of (turtles-here with [breed = tanks]) [distancexy mxcor mycor] + let target min-one-of tanks-here [distancexy mxcor mycor] if target != nobody [ ask target [ if distancexy mxcor mycor < 0.65 [ diff --git a/explosion.nls b/explosion.nls new file mode 100644 index 0000000..dba8b5c --- /dev/null +++ b/explosion.nls @@ -0,0 +1,66 @@ +;; lobo: Logo Bolo +;; (c) Ben Kurtovic, 2011 + +globals [ + explodes +] + +breed [explosions explosion] + +explosions-own [ + midlife + size-increment + time-to-live +] + +;; ========== +;; Procedures +;; ========== + +to make-explosions-table + ; Fun with particle effects! + set explodes table:make + table:put explodes "decay" [["explosion-decay" 0.65 10 0.5]] + table:put explodes "shot" [["explosion-shot" 1.25 10 0.5]] + table:put explodes "kill" [["explosion-kill" 3 20 0.5] ["explosion-shot" 1.25 15 1] + ["explosion-shot" 1.25 15 1 ] ["explosion-shot" 1.25 10 1] + ["explosion-shot" 1.25 10 1 ] ["explosion-decay" 0.65 10 2] + ["explosion-decay" 0.65 10 2 ] ["explosion-decay" 0.65 7.5 2] + ["explosion-decay" 0.65 7.5 2 ] ["explosion-decay" 0.65 5 3] + ["explosion-decay" 0.65 5 3 ]] +end + +to explode [name] + debug "EXPLODE" name + let data table:get explodes name + foreach data [create-explosion ?] +end + +to create-explosion [info] + let sprite (item 0 info) + let maxsize (item 1 info) + let lifespan (item 2 info) + let deviation (item 3 info) + hatch-explosions 1 [ + set shape sprite + set size maxsize / 2 + set midlife lifespan / 2 + set size-increment (maxsize / lifespan) * 2 + set time-to-live lifespan + set heading random 360 + fd (random 100) / (100 / deviation) + ] +end + +to keep-exploding + if time-to-live = 0 [ + die + ] + set time-to-live time-to-live - 1 + set midlife midlife - 1 + ifelse midlife > 0 [ + set size size + size-increment + ] [ + set size size - size-increment + ] +end diff --git a/lobo.nlogo b/lobo.nlogo index 58aec85..ac334b5 100644 --- a/lobo.nlogo +++ b/lobo.nlogo @@ -6,6 +6,7 @@ __includes [ "bullet.nls" + "explosion.nls" "player.nls" "tank.nls" ] @@ -19,12 +20,6 @@ globals [ last-tick-time max-fps mouse-was-down? - player - player-deaths - player-has-target? - player-kills - player-target-xcor - player-target-ycor sounds ] @@ -36,9 +31,9 @@ to setup clear-all no-display setup-defaults - ask patches [ - set pcolor (random 3) - 5 + green - ] + make-sounds-table + make-explosions-table + load-map spawn-player spawn-tank 0 ask tank 1 [ setxy -6 0 ] spawn-tank 1 ask tank 2 [ setxy 6 0 ] @@ -56,6 +51,9 @@ to go ask bullets [ do-bullet-logic ] + ask explosions [ + keep-exploding + ] show-crosshair render keep-time @@ -93,18 +91,20 @@ to setup-defaults set player-kills 0 set player-target-xcor 0 set player-target-ycor 0 - make-sounds-table end to make-sounds-table set sounds table:make - table:put sounds "fire" "Hand Clap" - table:put sounds "shot player" "Acoustic Snare" - table:put sounds "shot ally" "Acoustic Snare" - table:put sounds "shot enemy" "Acoustic Snare" - table:put sounds "kill player" "Electric Snare" - table:put sounds "kill ally" "Electric Snare" - table:put sounds "kill enemy" "Electric Snare" + table:put sounds "fire" "Hand Clap" + table:put sounds "noammo" "Cowbell" + table:put sounds "shot" "Acoustic Snare" + table:put sounds "kill" "Electric Snare" +end + +to load-map + ask patches [ + set pcolor (random 3) - 5 + green + ] end to show-crosshair @@ -412,6 +412,27 @@ Rectangle -7500403 true false 165 210 195 268 Rectangle -7500403 true false 105 210 135 268 Polygon -7500403 true false 105 210 135 150 135 210 105 210 +explosion-decay +false +0 +Circle -2674135 true false 2 2 295 +Circle -955883 true false 75 75 148 + +explosion-kill +false +0 +Circle -6459832 true false 2 2 297 +Circle -2674135 true false 30 30 240 +Circle -955883 true false 60 60 180 +Circle -1184463 true false 90 90 120 + +explosion-shot +false +0 +Circle -2674135 true false 0 0 300 +Circle -955883 true false 45 45 210 +Circle -1184463 true false 90 90 120 + tank true 1 diff --git a/player.nls b/player.nls index 4912b37..803d3a5 100644 --- a/player.nls +++ b/player.nls @@ -1,6 +1,19 @@ ;; lobo: Logo Bolo ;; (c) Ben Kurtovic, 2011 +globals [ + player + player-deaths + player-has-target? + player-kills + player-target-xcor + player-target-ycor +] + +;; ========== +;; Procedures +;; ========== + to spawn-player create-tanks 1 [ set player tank who @@ -11,10 +24,16 @@ end to do-player-logic if mouse-inside? [ if mouse-down? and not mouse-was-down? [ - set is-accelerating? true - set player-has-target? true - set player-target-xcor (round mouse-xcor) - set player-target-ycor (round mouse-ycor) + let txcor round mouse-xcor + let tycor round mouse-ycor + ifelse player-has-target? and player-target-xcor = txcor and player-target-ycor = tycor [ + cancel-target + ] [ + set is-accelerating? true + set player-has-target? true + set player-target-xcor txcor + set player-target-ycor tycor + ] ] set mouse-was-down? mouse-down? ] @@ -23,6 +42,8 @@ to do-player-logic tank-facexy player-target-xcor player-target-ycor let dist distancexy player-target-xcor player-target-ycor if dist < 2 and speed > 0.075 [ + ; Turn on "brakes" if we're getting + ; close and we're moving fast: decelerate friction * 5 ] if dist < 0.1 [ @@ -42,4 +63,5 @@ end to cancel-target set is-accelerating? false set player-has-target? false + set speed speed / 2 end diff --git a/tank.nls b/tank.nls index 5c05b45..5a21334 100644 --- a/tank.nls +++ b/tank.nls @@ -18,6 +18,10 @@ tanks-own [ team ] +;; ========== +;; Procedures +;; ========== + to spawn-tank [tank-team] create-tanks 1 [ set-tank-vars tank-team false @@ -83,12 +87,16 @@ to tank-facexy [txcor tycor] end to fire - if ammunition > 0 and fire-cool-down = 0 [ - debug "FIRE" (word who " (" ammunition " left)") - set ammunition ammunition - 1 + if fire-cool-down = 0 [ + ifelse ammunition > 0 [ + debug "FIRE" (word who " (" ammunition " left)") + set ammunition ammunition - 1 + fire-bullet + play-sound "fire" + ] [ + play-sound "noammo" + ] set fire-cool-down max-fire-rate - fire-bullet - play-sound "fire" ] end @@ -96,15 +104,17 @@ to shot-at debug "SHOT" (word who " by " ([shooter] of myself)) set armor armor - 1 ifelse armor = 0 [ + explode "kill" + play-sound "kill" kill-tank ] [ - play-sound (word "shot " get-tank-affiliation) + explode "shot" + play-sound "shot" ] end to kill-tank debug "KILL" (word who " by " ([shooter] of myself)) - play-sound (word "kill " get-tank-affiliation) if is-player? [ set player-deaths player-deaths + 1 ] @@ -114,6 +124,10 @@ to kill-tank die end +;; ========= +;; Reporters +;; ========= + to-report get-tank-affiliation if is-player? [ report "player" ] if team = 0 [ report "ally" ]