高效地生成和移动大量物体,如子弹(用于子弹地狱),粒子等。
Efficiently spawn and move high amounts of objects like bullets for bullet hells, particles and more.
This is a GDNative plugin, compatible with Godot 3.4.x and up.
Not compatible with Godot 4.x.
The versions of the pre-built binaries are as follows:
Notice: to make sure you have the latest binary version you can build from source.
See Compiling and extending the plugin.
For best performance, toggle use_bvh off in Project Settings -> Physics -> 2d.
The first thing to do is create a BulletKit resource and choose how bullets will appear and behave tweaking its properties.
Create a new empty resource and assign one of the scripts you can find in the addons/native_bullets/kits folder to it.
In this example, we'll assign the basic_bullet_kit.gdns script.
New properties will appear in the resource inspector.
To get started, fill the texture property with any texture you have.
As the material, you can use a new material resource using the animated_shader.gdshader you can find in the utils folder.
This shader takes care of animating your bullets if you specify more than 1 frame in its parameters.
For now, turn off collisions_enabled, turn use_viewport_as_active_rect on, turn rotate off and set unique_modulate_component to None.
See the Reference section to learn more.
This BulletKit resource is now ready to be used!
In order to spawn bullets in the scene, a BulletsEnvironment node is needed. This is the node that will define which kinds of bullets will be available during its lifetime. Only one BulletsEnvironment node can be active at the same time and if you add another one to the scene tree its configuration will replace the previous one.
The BulletsEnvironment node has to be configured to choose which kinds of bullets will be able to spawn, their maximum amount and their z indices.
Create a new BulletsEnvironment node.
Choose how many types of bullets this node will be able to spawn. Increase bullet_types_amount to 1 for now.
A new section of the inspector will appear below. Unfold the Bullet Type 0 entry.
Here, drag & drop the BulletKit resource you created earlier to let the node know that you'll want to spawn the bullet described in it!
Choose the maximum amount of bullets setting the pool_size property and the their z_index.
3000 and 1 will be ok.
Leave the parent_hint property empty, it will make the bullets spawn in the nearest Viewport or CanvasLayer up in the scene tree.
Nice! Now the bullets are ready.
The last step is to spawn a lot of those bullets.
You can use the utilities provided by the plugin to spawn bullets in a variety of ways, but let's do it manually for now.
Create a script.
…Here you go! Now attach this script to one of your 2D nodes, start the game, press space to shoot and right/left to change the bullets direction!
The repository contains an examples folder containing a few pre-configured scenes, ready to be played in the editor.
Below are reported some of the most common things you can do.
Spawn bullets.
…You can manipulate and set properties of bullets even after you spawned them.
export(float) var bullets_speed = 50.0
func shoot():
var bullet_kit = preload("res://path/to/basic_bullet_kit.tres")
# Use `obtain_bullet` to receive an opaque BulletID object.
var bullet_id = Bullets.obtain_bullet(bullet_kit)
# You can then use `bullet_id' to set and get properties.
Bullets.set_bullet_property(bullet_id, "transform", Transform2D(global_rotation, global_position))
Bullets.set_bullet_property(bullet_id, "velocity", Vector2(cos(global_rotation), sin(global_rotation)) * bullets_speed)When a bullet collides with a body or an area, the best way to get the data of the colliding bullet is to connect to the area_shape_entered signal.
…The plugin has a few main components, explained below:
This is the resource used to configure each type of bullet.
Every BulletKit spawns its own bullet instances that support different kinds of properties.
The plugin comes with 4 base variants that can be customized via the editor:
BasicBulletKit
This is the most basic BulletKit, makes bullets move, generally at constant speed.
It's configurable with:
texture: controls what texture is sent to the bullet material.material: the material used to render each bullet.collisions_enabled: enables or disables collision detection, turning it off increases performance.collision_layer: the collision layer to use during collision detection. Visible only if collisions_enabled is on.collision_mask: the collision mask to use during collision detection. Visible only if collisions_enabled is on.collision_shape: the CollisionShape to use during collision detection. Visible only if collisions_enabled is on.use_viewport_as_active_rect: if enabled, uses the current viewport to detect whether a bullet should be deleted.active_rect: the rect outside of which the bullets get deleted. Visible only if use_viewport_as_active_rect if off.rotate: controls whether the bullets automatically rotate based on their direction of travel.unique_modulate_component: controls which modulate component in the material will be used as a unique value for each bullet instance. This can be used to offset bullets animation frames by unique amounts inside shaders and it's needed due to Godot not supporting material instance properties in 3.x.data: custom data you can assign to the BulletKit.Bullets spawned by a BasicBulletKit have those properties:
item_rid: the bullet canvas item RID, used internally. Read-only.cycle: the number of times the bullet was recycled, used internally. Read-only.shape_index: the index of the collision shape used by this bullet, used internally. Read-only.transform: the Transform2D used to position and rotate the bullet.velocity: the Vector2 that will be used to update the bullet position.lifetime: how much time the bullet has been alive.data: custom data you can assign to any bullet.FollowingBulletKit
This BulletKit lets you define a target node for each bullet and a turning speed used to chase it around.
It's configurable with:
texture: controls what texture is sent to the bullet material.bullets_turning_speed: the turning speed with which the bullets will rotate towards the target node.material: the material used to render each bullet.collisions_enabled: enables or disables collision detection, turning it off increases performance.collision_layer: the collision layer to use during collision detection. Visible only if collisions_enabled is on.collision_mask: the collision mask to use during collision detection. Visible only if collisions_enabled is on.collision_shape: the CollisionShape to use during collision detection. Visible only if collisions_enabled is on.use_viewport_as_active_rect: if enabled, uses the current viewport to detect whether a bullet should be deleted.active_rect: the rect outside of which the bullets get deleted. Visible only if use_viewport_as_active_rect if off.rotate: controls whether the bullets automatically rotate based on their direction of travel.unique_modulate_component: controls which modulate component in the material will be used as a unique value for each bullet instance. This can be used to offset bullets animation frames by unique amounts inside shaders and it's needed due to Godot not supporting material instance properties in 3.x.data: custom data you can assign to the BulletKit.Bullets spawned by a FollowingBulletKit have those properties:
target_node: the Node2D the bullet will chase.item_rid: the bullet canvas item RID, used internally. Read-only.cycle: the number of times the bullet was recycled, used internally. Read-only.shape_index: the index of the collision shape used by this bullet, used internally. Read-only.transform: the Transform2D used to position and rotate the bullet.velocity: the Vector2 that will be used to update the bullet position.lifetime: how much time the bullet has been alive.data: custom data you can assign to any bullet.DynamicBulletKit
Lets you define bullets that change their behaviour based on their lifetime.
It's configurable with:
texture: controls what texture is sent to the bullet material.lifetime_curves_span: the reference lifespan used by the curves to determine their current value.lifetime_curves_loop: controls whether the curves will loop around when lifetime goes past lifetime_curves_span.speed_multiplier_over_lifetime: controls the bullet speed multiplying its velocity magnitude by the value defined in this curve, based on the bullet lifetime.rotation_offset_over_lifetime: controls the bullet rotation offsetting its initial rotation by the value in radians defined in this curve, based on the bullet lifetime.material: the material used to render each bullet.collisions_enabled: enables or disables collision detection, turning it off increases performance.collision_layer: the collision layer to use during collision detection. Visible only if collisions_enabled is on.collision_mask: the collision mask to use during collision detection. Visible only if collisions_enabled is on.collision_shape: the CollisionShape to use during collision detection. Visible only if collisions_enabled is on.use_viewport_as_active_rect: if enabled, uses the current viewport to detect whether a bullet should be deleted.active_rect: the rect outside of which the bullets get deleted. Visible only if use_viewport_as_active_rect if off.rotate: controls whether the bullets automatically rotate based on their direction of travel.unique_modulate_component: controls which modulate component in the material will be used as a unique value for each bullet instance. This can be used to offset bullets animation frames by unique amounts inside shaders and it's needed due to Godot not supporting material instance properties in 3.x.data: custom data you can assign to the BulletKit.Bullets spawned by a DynamicBulletKit have those properties:
transform: the Transform2D used to position and rotate the bullet.starting_transform: the baseline Transform2D, curves will apply their changes based on this transform. This is automatically set when you manually edit transform.velocity: the Vector2 that will be used to update the bullet position.starting_speed: the baseline speed, curves will apply their changes based on this speed value. This is automatically set when you manually edit velocity.item_rid: the bullet canvas item RID, used internally. Read-only.cycle: the number of times the bullet was re暂无开放 Issues,或尚未同步最近议题。