2014-11-06 00:20:42 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Member variables
|
2014-11-06 00:20:42 +00:00
|
|
|
const BULLET_COUNT = 500
|
|
|
|
const SPEED_MIN = 20
|
|
|
|
const SPEED_MAX = 50
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
var bullets = []
|
2014-11-06 00:20:42 +00:00
|
|
|
var shape
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
# Inner classes
|
2014-11-06 00:20:42 +00:00
|
|
|
class Bullet:
|
|
|
|
var pos = Vector2()
|
|
|
|
var speed = 1.0
|
|
|
|
var body = RID()
|
|
|
|
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
func _draw():
|
2014-11-06 00:20:42 +00:00
|
|
|
var t = preload("res://bullet.png")
|
|
|
|
var tofs = -t.get_size()*0.5
|
|
|
|
for b in bullets:
|
2015-11-21 15:13:43 +00:00
|
|
|
draw_texture(t, b.pos + tofs)
|
|
|
|
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
func _process(delta):
|
|
|
|
var width = get_viewport_rect().size.x*2.0
|
|
|
|
var mat = Matrix32()
|
|
|
|
for b in bullets:
|
2015-11-21 15:13:43 +00:00
|
|
|
b.pos.x -= b.speed*delta
|
2014-11-06 00:20:42 +00:00
|
|
|
if (b.pos.x < -30):
|
2015-11-21 15:13:43 +00:00
|
|
|
b.pos.x += width
|
|
|
|
mat.o = b.pos
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
Physics2DServer.body_set_state(b.body, Physics2DServer.BODY_STATE_TRANSFORM, mat)
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
update()
|
|
|
|
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
func _ready():
|
|
|
|
shape = Physics2DServer.shape_create(Physics2DServer.SHAPE_CIRCLE)
|
|
|
|
Physics2DServer.shape_set_data(shape, 8) # Radius
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
for i in range(BULLET_COUNT):
|
|
|
|
var b = Bullet.new()
|
2015-11-21 15:13:43 +00:00
|
|
|
b.speed = rand_range(SPEED_MIN, SPEED_MAX)
|
2014-11-06 00:20:42 +00:00
|
|
|
b.body = Physics2DServer.body_create(Physics2DServer.BODY_MODE_KINEMATIC)
|
2015-11-21 15:13:43 +00:00
|
|
|
Physics2DServer.body_set_space(b.body, get_world_2d().get_space())
|
|
|
|
Physics2DServer.body_add_shape(b.body, shape)
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
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
|
2014-11-06 00:20:42 +00:00
|
|
|
var mat = Matrix32()
|
2015-11-21 15:13:43 +00:00
|
|
|
mat.o = b.pos
|
|
|
|
Physics2DServer.body_set_state(b.body, Physics2DServer.BODY_STATE_TRANSFORM, mat)
|
2014-11-06 00:20:42 +00:00
|
|
|
|
|
|
|
bullets.append(b)
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
set_process(true)
|
|
|
|
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
func _exit_tree():
|
|
|
|
for b in bullets:
|
2014-11-12 14:23:23 +00:00
|
|
|
Physics2DServer.free_rid(b.body)
|
2014-11-06 00:20:42 +00:00
|
|
|
|
2014-11-12 14:23:23 +00:00
|
|
|
Physics2DServer.free_rid(shape)
|
2014-11-06 00:20:42 +00:00
|
|
|
bullets.clear()
|