Skip to content

Make SimpleCharacter more flexible #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 65 additions & 10 deletions addons/block_code/simple_nodes/simple_character/simple_character.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ extends CharacterBody2D
@export var texture: Texture2D:
set = _set_texture

@export var speed: Vector2 = Vector2(300, 300):
set = _set_speed

const PLAYER_KEYS = {
"player_1":
{
Expand All @@ -22,6 +25,11 @@ const PLAYER_KEYS = {
}
}

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

var _jumping = false


func _set_texture(new_texture):
texture = new_texture
Expand All @@ -35,25 +43,58 @@ func _set_texture(new_texture):
$CollisionShape2D.shape = shape


func _set_speed(new_speed):
speed = new_speed


## Nodes in the "affected_by_gravity" group will receive gravity changes:
func on_gravity_changed(new_gravity):
gravity = new_gravity


func _ready():
add_to_group("affected_by_gravity")
simple_setup()


func simple_setup():
_set_texture(texture)
_set_speed(speed)


func get_custom_class():
return "SimpleCharacter"


func move_with_player_buttons(player: String, speed: Vector2):
var dir = Vector2()
dir.x += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["right"]))
dir.x -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["left"]))
dir.y += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["down"]))
dir.y -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]))
velocity = dir.normalized() * speed
func _player_input_to_direction(player: String):
var direction = Vector2()
direction.x += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["right"]))
direction.x -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["left"]))
direction.y += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["down"]))
direction.y -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]))
return direction


func move_with_player_buttons(player: String, kind: String, delta: float):
var direction = _player_input_to_direction(player)

if kind == "top-down":
velocity = direction * speed

elif kind == "platformer":
velocity.x = direction.x * speed.x
if not is_on_floor():
velocity.y += gravity * delta
else:
if not _jumping and Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]):
_jumping = true
velocity.y -= speed.y
else:
_jumping = false

elif kind == "spaceship":
rotation_degrees += direction.x * speed.x / 100.0
velocity = Vector2.DOWN.rotated(rotation) * speed.y * direction.y
move_and_slide()


Expand All @@ -64,13 +105,27 @@ static func get_custom_blocks() -> Array[Block]:
# Movement
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with {player: OPTION} buttons, speed {speed: VECTOR2}"
b.statement = 'move_with_player_buttons("{player}", {speed})'
b.block_format = "Move with {player: OPTION} buttons as {kind: OPTION}"
# TODO: delta here is assumed to be the parameter name of
# the _process or _physics_process method:
b.statement = 'move_with_player_buttons("{player}", "{kind}", delta)'
b.defaults = {
"player": OptionData.new(["player_1", "player_2"]),
"speed": "300,300",
"kind": OptionData.new(["top-down", "platformer", "spaceship"]),
}
b.category = "Input"
block_list.append(b)

var property_blocks = (
CategoryFactory
. property_to_blocklist(
{
"name": "speed",
"type": TYPE_VECTOR2,
"category": "Physics | Velocity",
}
)
)
block_list.append_array(property_blocks)

return block_list
4 changes: 0 additions & 4 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ config/icon="res://icon.svg"

enabled=PackedStringArray("res://addons/block_code/plugin.cfg", "res://addons/gut/plugin.cfg", "res://addons/plugin_refresher/plugin.cfg")

[physics]

2d/default_gravity=0.0

[rendering]

renderer/rendering_method="gl_compatibility"
Expand Down
Loading