class Eagle::Node2D

Overview

A node with a 2D position, rotation and scale. It is the base of every 2D game object.

Children move with their parent: a gun added to a ship turns when the ship turns. Local values (#position, #rotation, #scale) are relative to the parent, and the global_* methods give world-space values.

class Ship < Node2D
  def ready : Nil
    add(Polygon2D.new([v2(12, 0), v2(-8, -8), v2(-8, 8)], Color::WHITE))
    add(Node2D.new("Muzzle", position: v2(14, 0)))
  end

  def process(dt : Float32) : Nil
    rotate(Input.axis("left", "right") * 3 * dt)
    translate(forward * 120 * dt)                    # forward follows rotation
    muzzle = get_node("Muzzle", Node2D).global_position # where bullets spawn
  end
end

#modulate tints the node and all its children, so fading a whole character out is one line. Set top_level to ignore the parent's transform.

Direct Known Subclasses

Defined in:

eagle/nodes/node2d.cr

Constructors

Instance Method Summary

Instance methods inherited from class Eagle::Node

<<(child : Node) : self <<, [](path : String) : Node [], []?(path : String) : Node | Nil []?, add(child : Node) : Node
add(*children : Node) : Nil
add
, add_child(child : Node) : Node add_child, add_to_group(group : String) : self add_to_group, ancestor_of?(node : Node) : Bool ancestor_of?, can_process? : Bool can_process?, child?(name : String) : Node | Nil child?, child_added : Eagle::Emitter(Node) child_added, child_count : Int32 child_count, child_removed : Eagle::Emitter(Node) child_removed, children : Array(Eagle::Node) children, children_of(type : T.class) : Array(T) forall T children_of, clear_children : Nil clear_children, draw(g : Graphics) : Nil draw, dump(io : IO = STDOUT, indent = 0) : Nil dump, each_ancestor(& : Node -> ) : Nil each_ancestor, each_child(& : Node -> ) : Nil each_child, each_descendant(&block : Node -> ) : Nil each_descendant, emit_child_added(*args) : Nil emit_child_added, emit_child_removed(*args) : Nil emit_child_removed, emit_tree_entered(*args) : Nil emit_tree_entered, emit_tree_exited(*args) : Nil emit_tree_exited, enter_tree : Nil enter_tree, exit_tree : Nil exit_tree, find(name : String) : Node | Nil find, find_all(type : T.class) : Array(T) forall T find_all, first_child : Node | Nil first_child, free : Nil free, get_node(path : String, type : T.class) : T forall T
get_node(path : String) : Node
get_node
, get_node?(path : String) : Node | Nil get_node?, groups : Set(String) groups, in_group?(group : String) : Bool in_group?, in_tree? : Bool in_tree?, input(event : Event) : Nil input, name : String name, name=(name : String) name=, on_child_added(&block : Node -> Nil) : Proc(Node, Nil) on_child_added, on_child_removed(&block : Node -> Nil) : Proc(Node, Nil) on_child_removed, on_tree_entered(&block : -> Nil) : Proc(Nil) on_tree_entered, on_tree_exited(&block : -> Nil) : Proc(Nil) on_tree_exited, parent : Node | Nil parent, path : String path, physics_process(dt : Float32) : Nil physics_process, process(dt : Float32) : Nil process, process_mode : Eagle::Node::ProcessMode process_mode, process_mode=(process_mode : Eagle::Node::ProcessMode) process_mode=, queue_free : Nil queue_free, queued_free? : Bool queued_free?, ready : Nil ready, ready_called? : Bool ready_called?, remove(child : Node) : Nil remove, remove_child(child : Node) : Nil remove_child, remove_from_group(group : String) : Nil remove_from_group, remove_from_parent : Nil remove_from_parent, resized(width : Int32, height : Int32) : Nil resized, root : Node root, to_s(io : IO) : Nil to_s, tree_entered : Eagle::Emitter() tree_entered, tree_exited : Eagle::Emitter() tree_exited, visible=(visible : Bool) visible=, visible? : Bool visible?, z_index : Int32 z_index, z_index=(z_index : Int32) z_index=

Constructor methods inherited from class Eagle::Node

new(name : String = "") new

Constructor Detail

def self.new(name : String = "", position : Eagle::Vec2 = Vec2::ZERO, rotation : Float32 = 0_f32, scale : Eagle::Vec2 = Vec2::ONE) #

Creates a 2D node at position.


Instance Method Detail

def direction_to(other : Node2D) : Vec2 #

Unit vector pointing from this node toward other.


def distance_to(other : Node2D) : Float32 #

Distance to another 2D node, in world space.


def down : Vec2 #

The unit vector 90° clockwise from #right.


def forward : Vec2 #

Same as #right: the direction the node faces.


def global_position : Vec2 #

Position in world space.


def global_position=(p : Vec2) #

Moves the node so it ends up at p in world space.


def global_rotation : Float32 #

Rotation in world space.


def global_scale : Vec2 #

Scale in world space.


def global_transform : Transform2D #

The transform from this node's space to world space.


def look_at(target : Vec2) : Nil #

Rotates the node to face a world-space point.

turret = Node2D.new
turret.look_at(Input.mouse)

def modulate : Color #

Color multiplied into this node and all of its children.


def modulate=(modulate : Color) #

Color multiplied into this node and all of its children.


def parent_2d : Node2D | Nil #

The nearest ancestor that is a Node2D, or nil.


def position : Vec2 #

Position relative to the parent, in points.


def position=(p : Vec2) #

Sets the position.


def right : Vec2 #

The unit vector this node faces, in world space. Rotation 0 faces right.


def rotate(rad : Number) : Nil #

Adds rad radians to the rotation.


def rotation : Float32 #

Rotation relative to the parent, in radians. Positive is clockwise on screen.


def rotation=(rotation : Float32) #

Rotation relative to the parent, in radians. Positive is clockwise on screen.


def rotation_degrees : Float32 #

Rotation in degrees.


def rotation_degrees=(d : Number) #

Sets the rotation in degrees.


def scale : Vec2 #

Scale relative to the parent. Negative x mirrors horizontally.


def scale=(s : Vec2) #

Sets the scale per axis.


def scale=(s : Number) #

Sets a uniform scale.


def to_global(local : Vec2) : Vec2 #

Converts a local point into world space.


def to_local(global : Vec2) : Vec2 #

Converts a world-space point into this node's local space.


def top_level=(top_level : Bool) #

When true, the node ignores its parent's transform and positions itself in world space.


def top_level? : Bool #

When true, the node ignores its parent's transform and positions itself in world space.


def transform : Transform2D #

The local transform built from position, rotation and scale.


def transform=(t : Transform2D) #

Sets position, rotation and scale from a transform.


def translate(x : Number, y : Number) : Nil #

Moves the node by x, y in parent space.


def translate(v : Vec2) : Nil #

Moves the node by v in parent space.


def x : Float32 #

The x position.


def x=(v : Number) #

Sets the x position.


def y : Float32 #

The y position.


def y=(v : Number) #

Sets the y position.