Skip to content

Instantly share code, notes, and snippets.

@spacechase0
Created July 8, 2021 06:42
Show Gist options
  • Save spacechase0/b396647167578d25bc066fd4a5f76ab1 to your computer and use it in GitHub Desktop.
Save spacechase0/b396647167578d25bc066fd4a5f76ab1 to your computer and use it in GitHub Desktop.
SpaceECS (name subject to change)
extends Node2D
func _physics_process(delta):
var vels = get_tree().get_nodes_in_group( "components/VelocityComponent" )
for node in vels:
var vel = ComponentServer.get_component( node, "VelocityComponent" )
node.move_and_collide( vel.velocity * delta )
var velsCS = get_tree().get_nodes_in_group( "components/VelocityCSComponent" )
for node in velsCS:
var vel = ComponentServer.get_component( node, "VelocityCSComponent" )
node.move_and_collide( vel.Velocity * delta )
extends Node2D
func _ready():
var c := VelocityComponent.new()
var dir := randf() * PI * 2
c.velocity = Vector2( cos( dir ) * 100, sin( dir ) * 100 )
ComponentServer.add_component( self, c )
using Godot;
using System;
public class TestEntityCS : KinematicBody2D
{
public override void _Ready()
{
float dir = GD.Randf() * Mathf.Pi * 2;
ComponentServer.AddComponent( this, new VelocityCSComponent()
{
Velocity = new Vector2( Mathf.Cos( dir ) * 100, Mathf.Sin( dir ) * 100 )
} );
}
}
extends Component
class_name VelocityComponent # Not necessary
export var velocity : Vector2 = Vector2( 0, 0 )
func _get_component_name(): # Is necessary
return "VelocityComponent"
using Godot;
using System;
public class VelocityCSComponent : Component
{
[Export]
public Vector2 Velocity = Vector2.Zero;
public override string _GetComponentName()
{
return "VelocityCSComponent";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment