From 06a1b0ecccf61a0f582dbb48e985f186d21cd5ed Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 28 Dec 2011 00:25:33 -0500 Subject: [PATCH] Player has a "target" instead of simply moving towards the mouse. --- bullet.nls | 3 ++- lobo.nlogo | 65 +++++++++++++++++++++++++++++++++++++++++--------------------- player.nls | 46 +++++++++++++++----------------------------- tank.nls | 13 +++++++++++++ 4 files changed, 73 insertions(+), 54 deletions(-) diff --git a/bullet.nls b/bullet.nls index ea87d77..400b809 100644 --- a/bullet.nls +++ b/bullet.nls @@ -21,7 +21,8 @@ to fire-bullet set shape "bullet" set size 0.5 - lt random 10 ; Bullets shouldn't travel perfectly straight + ; Bullets shouldn't travel perfectly straight: + lt random 10 rt random 10 ] end diff --git a/lobo.nlogo b/lobo.nlogo index 80bbc67..f885226 100644 --- a/lobo.nlogo +++ b/lobo.nlogo @@ -20,9 +20,11 @@ globals [ max-fps mouse-was-down? player - player-accelerate-for player-deaths + player-has-target? player-kills + player-target-xcor + player-target-ycor sounds ] @@ -38,8 +40,8 @@ to setup set pcolor (random 3) - 5 + green ] spawn-player - spawn-tank 0 ask tank 1 [ setxy -10 0 ] - spawn-tank 1 ask tank 2 [ setxy 10 0 ] + spawn-tank 0 ask tank 1 [ setxy -6 0 ] + spawn-tank 1 ask tank 2 [ setxy 6 0 ] render set last-tick-time timer end @@ -74,9 +76,11 @@ to setup-defaults resize-world -8 8 -8 8 set max-fps 30 set mouse-was-down? false - set player-accelerate-for 0 set player-deaths 0 + set player-has-target? false set player-kills 0 + set player-target-xcor 0 + set player-target-ycor 0 make-sounds-table end @@ -95,27 +99,33 @@ to show-crosshair clear-drawing if mouse-inside? [ ask patch mouse-xcor mouse-ycor [ - sprout 1 [ - set color white - set heading 0 - make-square - die - ] + draw-border white 1 + ] + ] + if player-has-target? [ + ask patch player-target-xcor player-target-ycor [ + draw-border white 2 ] ] end -to make-square - fd 0.5 - pd - rt 90 - fd 0.5 - repeat 3 [ +to draw-border [b-color b-thickness] + sprout 1 [ + set color b-color + set pen-size b-thickness + set heading 0 + fd 0.5 + pd rt 90 - fd 1 + fd 0.5 + repeat 3 [ + rt 90 + fd 1 + ] + rt 90 + fd 0.5 + die ] - rt 90 - fd 0.5 end to render @@ -165,7 +175,7 @@ GRAPHICS-WINDOW 0 0 1 -ticks +frames BUTTON 43 @@ -231,8 +241,8 @@ MONITOR 350 389 399 -Player Accel Time -player-accelerate-for +Player Has Target? +player-has-target? 5 1 12 @@ -299,6 +309,17 @@ enable-sound? 1 -1000 +MONITOR +272 +411 +390 +460 +Player Target +word \"(\" player-target-xcor \", \" player-target-ycor \")\" +10 +1 +12 + @#$#@#$#@ WHAT IS IT? ----------- diff --git a/player.nls b/player.nls index db89e41..b7bcc9e 100644 --- a/player.nls +++ b/player.nls @@ -17,41 +17,25 @@ end to do-player-logic if mouse-inside? [ if mouse-down? and not mouse-was-down? [ - set player-accelerate-for player-accelerate-for + 10 - if player-accelerate-for > 60 [ - set player-accelerate-for 60 - ] + set is-accelerating? true + set player-has-target? true + set player-target-xcor (round mouse-xcor) + set player-target-ycor (round mouse-ycor) ] set mouse-was-down? mouse-down? - let old-heading heading - - ; Slow down the tank a bit more than usual if we're - ; very close to the mouse: - let mouse-dist distancexy mouse-xcor mouse-ycor - if mouse-dist < 3 [ - decelerate friction - ] - if mouse-dist < 1.5 [ - decelerate friction * 6.5 - ] - if mouse-dist < 0.35 [ - set speed 0 - set player-accelerate-for 0 - ] - - facexy mouse-xcor mouse-ycor - if subtract-headings old-heading heading > max-turn [ - set heading old-heading - max-turn + ] + + ifelse player-has-target? [ + tank-facexy player-target-xcor player-target-ycor + let dist distancexy player-target-xcor player-target-ycor + if dist < 2 and speed > 0.075 [ + decelerate friction * 5 ] - if subtract-headings old-heading heading < 0 - max-turn [ - set heading old-heading + max-turn + if dist < 0.1 [ + set is-accelerating? false + set player-has-target? false ] - ] - - ifelse player-accelerate-for > 0 [ - set player-accelerate-for player-accelerate-for - 1 - set is-accelerating? true ] [ - set is-accelerating? false + tank-facexy mouse-xcor mouse-ycor ] end diff --git a/tank.nls b/tank.nls index 61912cb..5c05b45 100644 --- a/tank.nls +++ b/tank.nls @@ -69,6 +69,19 @@ to decelerate [amount] ] end +to tank-facexy [txcor tycor] + ;; Face a target at (txcor, tycor) like facexy, but don't + ;; turn more than our max turn rate (max-turn¡): + let old-heading heading + facexy txcor tycor + if subtract-headings old-heading heading > max-turn [ + set heading old-heading - max-turn + ] + if subtract-headings old-heading heading < 0 - max-turn [ + set heading old-heading + max-turn + ] +end + to fire if ammunition > 0 and fire-cool-down = 0 [ debug "FIRE" (word who " (" ammunition " left)")