Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- var _body:RID
- var _invalid_rid:RID #default RID
- func _enter_tree() -> void:
- _body = Physics2DServer.body_create() #first a body is made so physics can be applied to it
- Physics2DServer.body_set_space(_body, get_world_2d().space) #the body is placed in the 2D world
- _update_shape() #shape is added so that physics can be applied according to the shape of the body
- Physics2DServer.body_set_state(_body, Physics2DServer.BODY_STATE_TRANSFORM, self.global_transform) #???
- Physics2DServer.body_set_force_integration_callback(_body, self, "_body_moved", 0) #Once the forces are applied, the _body_moved function is called to change the transform (position) according to the forces
- set_notify_transform(true) #setting it true will draw the changes onto the screen once some property is changed in script
- func _exit_tree() -> void:
- Physics2DServer.free_rid(_body) #removes RID so that physicsServer no longer has to check for that RID
- _body = _invalid_rid #default RID
- export var shape:Shape2D setget set_shape
- export var disabled:bool setget set_disabled
- var _shape:RID
- func _update_shape() -> void:
- var new_shape = _invalid_rid if shape == null else shape.get_rid() #if no shape is assign give _shape default RID
- if new_shape == _shape: #if shape is the same as before then skip
- return
- if _shape.get_id() != 0: #???
- Physics2DServer.body_remove_shape(_body, 0) #removes shape to make sure there arn't multiple shapes
- _shape = new_shape
- if _shape.get_id() != 0: #???
- Physics2DServer.body_add_shape(_body, _shape, Transform2D.IDENTITY, disabled)
- func set_shape(new_value:Shape2D) -> void:
- if shape == new_value: #skips if new_shape is same
- return
- shape = new_value
- if _body.get_id() == 0: #???
- return
- _update_shape()
- func set_disabled(new_value:bool) -> void:
- if disabled == new_value:
- return
- disabled = new_value
- if _body.get_id() == 0: #???
- return
- if _shape.get_id() != 0: #???
- Physics2DServer.body_set_shape_disabled(_body, 0, disabled) #disables shape at index 0, if shape is disabled the body won't collide
- func _body_moved(state:Physics2DDirectBodyState, _user_data) -> void:
- self.global_transform = state.transform #takes the transformations made by the physics server and applies them onto the body
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement