Skip to content

Commit bbf4cff

Browse files
authored
Merge pull request #588 from Calinou/readd-bullet-shower-demo
Port the Bullet Shower demo from Godot 2.1
2 parents c2e7a30 + ca0f74e commit bbf4cff

File tree

16 files changed

+347
-0
lines changed

16 files changed

+347
-0
lines changed

2d/bullet_shower/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Bullet Shower
2+
3+
This demonstrates how to manage large amounts of objects efficiently using
4+
low-level Servers.
5+
6+
See
7+
[Optimization using Servers](https://docs.godotengine.org/en/latest/tutorials/performance/using_servers.html)
8+
in the documentation for more information.
9+
10+
Language: GDScript
11+
12+
Renderer: GLES 2
13+
14+
## Screenshots
15+
16+
![No collision](screenshots/no_collision.png)
17+
18+
![Collision](screenshots/collision.png)

2d/bullet_shower/bullet.png

312 Bytes
Loading

2d/bullet_shower/bullet.png.import

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/bullet.png-ff1424653e10246c11e3724e402c519e.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://bullet.png"
13+
dest_files=[ "res://.import/bullet.png-ff1424653e10246c11e3724e402c519e.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0

2d/bullet_shower/bullets.gd

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
extends Node2D
2+
# This demo is an example of controling a high number of 2D objects with logic
3+
# and collision without using nodes in the scene. This technique is a lot more
4+
# efficient than using instancing and nodes, but requires more programming and
5+
# is less visual. Bullets are managed together in the `bullets.gd` script.
6+
7+
const BULLET_COUNT = 500
8+
const SPEED_MIN = 20
9+
const SPEED_MAX = 80
10+
11+
const bullet_image = preload("res://bullet.png")
12+
13+
var bullets = []
14+
var shape
15+
16+
17+
class Bullet:
18+
var position = Vector2()
19+
var speed = 1.0
20+
# The body is stored as a RID, which is an "opaque" way to access resources.
21+
# With large amounts of objects (thousands or more), it can be significantly
22+
# faster to use RIDs compared to a high-level approach.
23+
var body = RID()
24+
25+
26+
func _ready():
27+
randomize()
28+
29+
shape = Physics2DServer.circle_shape_create()
30+
# Set the collision shape's radius for each bullet in pixels.
31+
Physics2DServer.shape_set_data(shape, 8)
32+
33+
for _i in BULLET_COUNT:
34+
var bullet = Bullet.new()
35+
# Give each bullet its own speed.
36+
bullet.speed = rand_range(SPEED_MIN, SPEED_MAX)
37+
bullet.body = Physics2DServer.body_create()
38+
39+
Physics2DServer.body_set_space(bullet.body, get_world_2d().get_space())
40+
Physics2DServer.body_add_shape(bullet.body, shape)
41+
42+
# Place bullets randomly on the viewport and move bullets outside the
43+
# play area so that they fade in nicely.
44+
bullet.position = Vector2(
45+
rand_range(0, get_viewport_rect().size.x) + get_viewport_rect().size.x,
46+
rand_range(0, get_viewport_rect().size.y)
47+
)
48+
var transform2d = Transform2D()
49+
transform2d.origin = bullet.position
50+
Physics2DServer.body_set_state(bullet.body, Physics2DServer.BODY_STATE_TRANSFORM, transform2d)
51+
52+
bullets.push_back(bullet)
53+
54+
55+
func _process(delta):
56+
var transform2d = Transform2D()
57+
for bullet in bullets:
58+
bullet.position.x -= bullet.speed * delta
59+
60+
if bullet.position.x < -16:
61+
# The bullet has left the screen; move it back to the right.
62+
bullet.position.x = get_viewport_rect().size.x + 16
63+
64+
transform2d.origin = bullet.position
65+
66+
Physics2DServer.body_set_state(bullet.body, Physics2DServer.BODY_STATE_TRANSFORM, transform2d)
67+
68+
# Order the CanvasItem to update since bullets are moving every frame.
69+
update()
70+
71+
72+
# Instead of drawing each bullet individually in a script attached to each bullet,
73+
# we are drawing *all* the bullets at once here.
74+
func _draw():
75+
var offset = -bullet_image.get_size() * 0.5
76+
for bullet in bullets:
77+
draw_texture(bullet_image, bullet.position + offset)
78+
79+
80+
# Perform cleanup operations (required to exit without error messages in the console).
81+
func _exit_tree():
82+
for bullet in bullets:
83+
Physics2DServer.free_rid(bullet.body)
84+
85+
Physics2DServer.free_rid(shape)
86+
bullets.clear()

2d/bullet_shower/face_happy.png

907 Bytes
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/face_happy.png-38d387d31ec13459f749c93ce3d75d80.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://face_happy.png"
13+
dest_files=[ "res://.import/face_happy.png-38d387d31ec13459f749c93ce3d75d80.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0

2d/bullet_shower/face_sad.png

846 Bytes
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/face_sad.png-0ac7165eab24f595aba17a746a66c550.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://face_sad.png"
13+
dest_files=[ "res://.import/face_sad.png-0ac7165eab24f595aba17a746a66c550.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0

2d/bullet_shower/icon.png

1.9 KB
Loading

2d/bullet_shower/icon.png.import

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://icon.png"
13+
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0

0 commit comments

Comments
 (0)