Skip to content
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

/claim #257 #339

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 44 additions & 15 deletions addons/block_code/simple_nodes/simple_character/simple_character.gd
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,45 @@ func get_custom_class():

func _player_input_to_direction(player: String):
var direction = Vector2()

# Keyboard input (existing)
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"]))

# Gamepad input (new)
if player == "player_1" or player == "player_2":
var joy_index = 0 if player == "player_1" else 1 # Assuming player 1 uses the first gamepad and player 2 uses the second
# Horizontal movement (left stick x-axis)
direction.x += Input.get_joy_axis(joy_index, JOY_AXIS_0) # Left stick X-axis
# Vertical movement (left stick y-axis)
direction.y += Input.get_joy_axis(joy_index, JOY_AXIS_1) # Left stick Y-axis

# You can also check for specific gamepad button presses if needed
# For example, A button for jumping (Button 0) on player 1 and player 2
if Input.is_joy_button_pressed(joy_index, JOY_BUTTON_0): # Button 0 (A button on most controllers)
# Handle button press (for jumping or other actions)
pass

return direction


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

# Handle input based on the selected input type (keyboard or gamepad)
if input_type == "keyboard":
# Get direction using keyboard input (existing)
direction = _player_input_to_direction(player)
elif input_type == "gamepad":
# Get direction using gamepad input (newly added)
direction = _player_input_to_direction(player) # This function now also supports gamepad

direction_x = direction.x

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

elif kind == "platformer":
velocity.x = direction.x * speed.x
if not is_on_floor():
Expand All @@ -116,40 +141,44 @@ func move_with_player_buttons(player: String, kind: String, delta: float):
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()


static func setup_custom_blocks():
var _class_name = "SimpleCharacter"
var block_list: Array[BlockDefinition] = []

# Movement
# Movement block definition
var block_definition: BlockDefinition = BlockDefinition.new()
block_definition.name = &"simplecharacter_move"
block_definition.target_node_class = _class_name
block_definition.category = "Input"
block_definition.type = Types.BlockType.STATEMENT
block_definition.display_template = "move with {player: NIL} buttons as {kind: NIL}"
block_definition.description = """Move the character using the “Player 1” or “Player 2” controls as configured in Godot.
block_definition.display_template = "move with {player: NIL} buttons as {kind: NIL} using {input_type: NIL}"
block_definition.description = """Move the character using the configured controls. You can select between keyboard or gamepad for the input.

“Top-down” enables the character to move in both x (vertical) and y (horizontal) dimensions, as if the camera is above the character, looking down. No gravity is added.
“Top-down” enables the character to move in both x (horizontal) and y (vertical) dimensions, like a top-down view.

“Platformer” enables the character to move as if the camera is looking from the side, like a side-scroller. Gravity is applied on the x (vertical) axis, making the character fall down until they collide.
“Platformer” enables the character to move as in a side-scroller, with gravity affecting vertical movement.

“Spaceship” uses the left/right controls to turn the character and up/down controls to go forward or backward."""
# TODO: delta here is assumed to be the parameter name of
# the _process or _physics_process method:
block_definition.code_template = "move_with_player_buttons({player}, {kind}, delta)"
“Spaceship” uses the left/right controls to rotate and up/down controls to move forward or backward."""

# Updated code template to include input type
block_definition.code_template = "move_with_player_buttons({player}, {kind}, delta, {input_type})"

# Add new input type option (keyboard or gamepad)
block_definition.defaults = {
"player": OptionData.new(["player_1", "player_2"]),
"kind": OptionData.new(["top-down", "platformer", "spaceship"]),
"player": OptionData.new(["player_1", "player_2"]), "kind": OptionData.new(["top-down", "platformer", "spaceship"]), "input_type": OptionData.new(["keyboard", "gamepad"]) # Add gamepad option
}

# Add block definition to the block list
block_list.append(block_definition)

# Define custom properties
var property_list: Array[Dictionary] = [
{
"name": "speed",
Expand Down
Loading