basic movements

This commit is contained in:
2023-10-17 16:42:21 -03:00
parent a30c178a0a
commit 6d4cf16dd3
3 changed files with 52 additions and 21 deletions

View File

@@ -14,3 +14,8 @@ config/name="gamejam"
run/main_scene="res://src/levels/level_template.tscn"
config/features=PackedStringArray("4.1", "Forward Plus")
config/icon="res://icon.svg"
[layer_names]
2d_physics/layer_1="Player"
2d_physics/layer_2="Inimigos"

View File

@@ -1,29 +1,55 @@
extends CharacterBody2D
const speed = 400
const jump_power = -1400.0
const SPEED = 300.0
const JUMP_VELOCITY = -450.0
const acc = 25.0
const acc = 25
const friction = 70
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
const grav = 100
const max_jumps = 2
var current_jumps = 1
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
var input_dir: Vector2 = input()
if input_dir != Vector2.ZERO:
accelerate(input_dir)
#play_animation()
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
add_friction()
#play_animation()
jump()
player_moviment()
func input() -> Vector2:
var input_dir = Vector2.ZERO
input_dir.x = Input.get_axis("ui_left", "ui_right")
input_dir = input_dir.normalized()
return input_dir
func accelerate(direction):
velocity = velocity.move_toward(speed * direction, acc)
func add_friction():
velocity = velocity.move_toward(Vector2.ZERO, friction)
func player_moviment():
move_and_slide()
func jump():
if Input.is_action_just_pressed("ui_up"):
if current_jumps < max_jumps:
velocity.y = jump_power
current_jumps += 1
else:
velocity.y += grav
if is_on_floor():
current_jumps = 1

View File

@@ -4,7 +4,7 @@
[ext_resource type="Texture2D" uid="uid://bglwfp5ngk8xr" path="res://assests/placeholder.png" id="2_ctbi8"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_xv46f"]
size = Vector2(121, 147)
size = Vector2(121, 149.5)
[node name="Player" type="CharacterBody2D"]
scale = Vector2(0.5, 0.5)
@@ -16,6 +16,6 @@ scale = Vector2(0.5, 0.5)
texture = ExtResource("2_ctbi8")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, -38)
position = Vector2(0, -37.375)
scale = Vector2(0.5, 0.5)
shape = SubResource("RectangleShape2D_xv46f")