Browse Source

Splitting code up into .nls files

master
Ben Kurtovic 13 years ago
parent
commit
68e579bf95
4 changed files with 137 additions and 123 deletions
  1. +27
    -0
      bullet.nls
  2. +10
    -123
      lobo.nlogo
  3. +52
    -0
      player.nls
  4. +48
    -0
      tank.nls

+ 27
- 0
bullet.nls View File

@@ -0,0 +1,27 @@
;; lobo: Logo Bolo
;; (c) Ben Kurtovic, 2011

breed [bullets bullet]

bullets-own [
max-travel-distance
speed
travel-distance
]

to fire
hatch-bullets 1 [
set color white
set max-travel-distance 8
set speed 1
set travel-distance 0
]
end

to do-bullet-logic
fd speed
set travel-distance travel-distance + speed
if travel-distance > max-travel-distance [
die
]
end

+ 10
- 123
lobo.nlogo View File

@@ -8,6 +8,12 @@
;; Declarations
;; ============

__includes [
"bullet.nls"
"player.nls"
"tank.nls"
]

globals [
last-tick-time
max-fps
@@ -16,26 +22,6 @@ globals [
player-accelerate-for
]

breed [bullets bullet]
breed [crosshairs crosshair]
breed [tanks tank]

bullets-own [
max-travel-distance
speed
travel-distance
]

tanks-own [
acceleration
friction
is-accelerating?
max-speed
max-turn
speed
team
]

;; ===========================
;; Button-initiated procedures
;; ===========================
@@ -67,12 +53,6 @@ to go
keep-time
end

to player-fire
ask player [
fire
]
end

;; ================
;; Other procedures
;; ================
@@ -85,80 +65,6 @@ to setup-defaults
set player-accelerate-for 0
end

to spawn-player
create-ordered-tanks 1 [
set player (tank who)
set color (get-tank-color "player")
set acceleration 0.05
set friction 0.01
set is-accelerating? false
set max-speed 0.3
set max-turn 24
set speed 0
set team 0
]
end

to fire
hatch-bullets 1 [
set color white
set size 1
set max-travel-distance 8
set speed 2
set travel-distance 0
]
end

to do-player-logic
if mouse-inside? [
if mouse-down? and not mouse-was-down? [
set player-accelerate-for player-accelerate-for + 5
if player-accelerate-for > 30 [
set player-accelerate-for 30
]
]
set mouse-was-down? mouse-down?
let old-heading heading
facexy mouse-xcor mouse-ycor

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
]
]

ifelse player-accelerate-for > 0 [
set player-accelerate-for player-accelerate-for - 1
set is-accelerating? true
] [
set is-accelerating? false
]
end

to do-tank-logic
if is-accelerating? [
set speed speed + acceleration
if speed > max-speed [
set speed max-speed
]
]
fd speed
set speed speed - friction
if speed < 0 [
set speed 0
]
end

to do-bullet-logic
fd speed
set travel-distance travel-distance + speed
if travel-distance > max-travel-distance [
die
]
end

to render
display
no-display
@@ -171,25 +77,6 @@ to keep-time
tick
set last-tick-time timer
end

;; =================
;; Monitor reporters
;; =================

to-report player-speed
report [speed] of player
end

;; ===============
;; Other reporters
;; ===============

to-report get-tank-color [affiliation]
if affiliation = "player" [ report gray ]
if affiliation = "enemy" [ report red ]
if affiliation = "ally" [ report green ]
report black
end
@#$#@#$#@
GRAPHICS-WINDOW
475
@@ -251,11 +138,11 @@ NIL

MONITOR
297
381
382
410
426
427
Player Speed
player-speed
[speed] of player
8
1
11
@@ -272,7 +159,7 @@ NIL
T
OBSERVER
NIL
NIL
F
NIL
NIL



+ 52
- 0
player.nls View File

@@ -0,0 +1,52 @@
;; lobo: Logo Bolo
;; (c) Ben Kurtovic, 2011

to spawn-player
create-tanks 1 [
set player tank who
set-tank-vars 0 true
]
end

to player-fire
ask player [
fire
]
end

to do-player-logic
if mouse-inside? [
if mouse-down? and not mouse-was-down? [
set player-accelerate-for player-accelerate-for + 5
if player-accelerate-for > 30 [
set player-accelerate-for 30
]
]
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:
if distancexy mouse-xcor mouse-ycor < 2.5 [
set speed speed - friction
if speed < 0 [
set speed 0
]
]

facexy mouse-xcor mouse-ycor
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
]
]

ifelse player-accelerate-for > 0 [
set player-accelerate-for player-accelerate-for - 1
set is-accelerating? true
] [
set is-accelerating? false
]
end

+ 48
- 0
tank.nls View File

@@ -0,0 +1,48 @@
;; lobo: Logo Bolo
;; (c) Ben Kurtovic, 2011

breed [tanks tank]

tanks-own [
acceleration
friction
is-accelerating?
is-player?
max-speed
max-turn
speed
team
]

to set-tank-vars [tank-team player-tank?]
set heading 0
set acceleration 0.03
set friction 0.0075
set is-accelerating? false
set is-player? player-tank?
set max-speed 0.25
set max-turn 24
set speed 0
set team tank-team
set color get-tank-color
end

to do-tank-logic
if is-accelerating? [
set speed speed + acceleration
if speed > max-speed [
set speed max-speed
]
]
fd speed
set speed speed - friction
if speed < 0 [
set speed 0
]
end

to-report get-tank-color
if is-player? [ report gray ]
if team = 0 [ report green ]
report red
end

Loading…
Cancel
Save