Skip to content

Commit 4e75147

Browse files
authored
Merge pull request #118 from endlessm/simple-character-improvements
SimpleCharacter: Use methods for player movement
2 parents ab3eeda + 52d0c5f commit 4e75147

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ extends CharacterBody2D
55
@export var texture: Texture2D:
66
set = _set_texture
77

8+
const PLAYER_KEYS = {
9+
"player_1":
10+
{
11+
"up": KEY_W,
12+
"down": KEY_S,
13+
"left": KEY_A,
14+
"right": KEY_D,
15+
},
16+
"player_2":
17+
{
18+
"up": KEY_UP,
19+
"down": KEY_DOWN,
20+
"left": KEY_LEFT,
21+
"right": KEY_RIGHT,
22+
}
23+
}
24+
825

926
func _set_texture(new_texture):
1027
texture = new_texture
@@ -30,40 +47,29 @@ func get_custom_class():
3047
return "SimpleCharacter"
3148

3249

50+
func move_with_player_buttons(player: String, speed: Vector2):
51+
var dir = Vector2()
52+
dir.x += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["right"]))
53+
dir.x -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["left"]))
54+
dir.y += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["down"]))
55+
dir.y -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]))
56+
velocity = dir.normalized() * speed
57+
move_and_slide()
58+
59+
3360
static func get_custom_blocks() -> Array[Block]:
3461
var b: Block
3562
var block_list: Array[Block] = []
3663

3764
# Movement
3865
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
3966
b.block_type = Types.BlockType.EXECUTE
40-
b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}"
41-
b.statement = (
42-
"var dir = Vector2()\n"
43-
+ "dir.x += float(Input.is_physical_key_pressed(KEY_D))\n"
44-
+ "dir.x -= float(Input.is_physical_key_pressed(KEY_A))\n"
45-
+ "dir.y += float(Input.is_physical_key_pressed(KEY_S))\n"
46-
+ "dir.y -= float(Input.is_physical_key_pressed(KEY_W))\n"
47-
+ "dir = dir.normalized()\n"
48-
+ "velocity = dir*{speed}\n"
49-
+ "move_and_slide()"
50-
)
51-
b.category = "Input"
52-
block_list.append(b)
53-
54-
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
55-
b.block_type = Types.BlockType.EXECUTE
56-
b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}"
57-
b.statement = (
58-
"var dir = Vector2()\n"
59-
+ "dir.x += float(Input.is_physical_key_pressed(KEY_RIGHT))\n"
60-
+ "dir.x -= float(Input.is_physical_key_pressed(KEY_LEFT))\n"
61-
+ "dir.y += float(Input.is_physical_key_pressed(KEY_DOWN))\n"
62-
+ "dir.y -= float(Input.is_physical_key_pressed(KEY_UP))\n"
63-
+ "dir = dir.normalized()\n"
64-
+ "velocity = dir*{speed}\n"
65-
+ "move_and_slide()"
66-
)
67+
b.block_format = "Move with {player: OPTION} buttons, speed {speed: VECTOR2}"
68+
b.statement = 'move_with_player_buttons("{player}", {speed})'
69+
b.defaults = {
70+
"player": OptionData.new(["player_1", "player_2"]),
71+
"speed": "300,300",
72+
}
6773
b.category = "Input"
6874
block_list.append(b)
6975

0 commit comments

Comments
 (0)