summaryrefslogtreecommitdiff
path: root/demos/2d/shower_of_bullets/bullets.gd
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2015-11-21 16:13:43 +0100
committerRémi Verschelde <rverschelde@gmail.com>2015-12-09 08:39:12 +0100
commit7589b2bf605b58fbb0e34dec8ae833708e9b54ea (patch)
tree6c6b6515be9d121127f738a586fd95299ca7018a /demos/2d/shower_of_bullets/bullets.gd
parent323dde7f3164477b3d51fda8352d8b37a19f7f9d (diff)
Improve code formatting
The scripts were streamlined using more or less the following conventions: - space after a comma in lists of arguments - spaces around weak operators (+, -), no spaces around strong operators (*, /) - spaces around comparison operators and compound assignment operators - space after a comment start (#) - removed trailing spaces or tabs, apart from those that delimit the function indentation level (those could be removed too but since they are added automatically by the editor when typing code, keeping them for now) - function blocks separate by two newlines - comment sentences start with an upper-case letter
Diffstat (limited to 'demos/2d/shower_of_bullets/bullets.gd')
-rw-r--r--demos/2d/shower_of_bullets/bullets.gd60
1 files changed, 28 insertions, 32 deletions
diff --git a/demos/2d/shower_of_bullets/bullets.gd b/demos/2d/shower_of_bullets/bullets.gd
index 79f4faaae6..b62c796f32 100644
--- a/demos/2d/shower_of_bullets/bullets.gd
+++ b/demos/2d/shower_of_bullets/bullets.gd
@@ -4,73 +4,69 @@ extends Node2D
# This demo is an example of controling a high number of 2D objects with logic and collision without using scene nodes.
# This technique is a lot more efficient than using instancing and nodes, but requires more programming and is less visual
+# Member variables
const BULLET_COUNT = 500
const SPEED_MIN = 20
const SPEED_MAX = 50
-var bullets=[]
+var bullets = []
var shape
+
+# Inner classes
class Bullet:
var pos = Vector2()
var speed = 1.0
var body = RID()
-
-func _draw():
+func _draw():
var t = preload("res://bullet.png")
var tofs = -t.get_size()*0.5
for b in bullets:
- draw_texture(t,b.pos+tofs)
-
-
+ draw_texture(t, b.pos + tofs)
+
+
func _process(delta):
var width = get_viewport_rect().size.x*2.0
var mat = Matrix32()
for b in bullets:
- b.pos.x-=b.speed*delta
+ b.pos.x -= b.speed*delta
if (b.pos.x < -30):
- b.pos.x+=width
- mat.o=b.pos
-
- Physics2DServer.body_set_state(b.body,Physics2DServer.BODY_STATE_TRANSFORM,mat)
+ b.pos.x += width
+ mat.o = b.pos
+ Physics2DServer.body_set_state(b.body, Physics2DServer.BODY_STATE_TRANSFORM, mat)
+
update()
-
-
-func _ready():
- shape = Physics2DServer.shape_create(Physics2DServer.SHAPE_CIRCLE)
- Physics2DServer.shape_set_data(shape,8) #radius
+func _ready():
+ # Initialization here
+ shape = Physics2DServer.shape_create(Physics2DServer.SHAPE_CIRCLE)
+ Physics2DServer.shape_set_data(shape, 8) # Radius
+
for i in range(BULLET_COUNT):
var b = Bullet.new()
- b.speed=rand_range(SPEED_MIN,SPEED_MAX)
+ b.speed = rand_range(SPEED_MIN, SPEED_MAX)
b.body = Physics2DServer.body_create(Physics2DServer.BODY_MODE_KINEMATIC)
- Physics2DServer.body_set_space(b.body,get_world_2d().get_space())
- Physics2DServer.body_add_shape(b.body,shape)
+ Physics2DServer.body_set_space(b.body, get_world_2d().get_space())
+ Physics2DServer.body_add_shape(b.body, shape)
- b.pos = Vector2( get_viewport_rect().size * Vector2(randf()*2.0,randf()) ) #twice as long
- b.pos.x += get_viewport_rect().size.x # start outside
+ b.pos = Vector2(get_viewport_rect().size * Vector2(randf()*2.0, randf())) # Twice as long
+ b.pos.x += get_viewport_rect().size.x # Start outside
var mat = Matrix32()
- mat.o=b.pos
- Physics2DServer.body_set_state(b.body,Physics2DServer.BODY_STATE_TRANSFORM,mat)
+ mat.o = b.pos
+ Physics2DServer.body_set_state(b.body, Physics2DServer.BODY_STATE_TRANSFORM, mat)
bullets.append(b)
-
-
- set_process(true)
-
+ set_process(true)
+
+
func _exit_tree():
for b in bullets:
Physics2DServer.free_rid(b.body)
Physics2DServer.free_rid(shape)
- # Initalization here
bullets.clear()
-
- pass
-
-