본문 바로가기

고도(Godot Enguine)/걸음마(Step by Step)

이동, 에니메이션, 충돌체 재정의

아래 유튜브 영상을 통해 이동함수와

충돌체를 맞았을 때 애니메이션과 동작을 작성하였다

https://youtu.be/3rrPr90Oras

플레이어 화면

떨어지는 물체(돌)과 플레이어의 충돌 함수 정의

1. 충돌하였을 경우 없어지는 돌

2. 넘어지는 동작을 하는 플레이어

3. 플레이어가 돌에 맞으면 씬 재 로드

 

extends KinematicBody2D

enum {MOVING, STOP}
export(int) var speed = 10
var screen_size
var sprite_size
var velocity = Vector2.ZERO
var state = MOVING

func _ready():
	$AnimationPlayer.play("idle")
	$Sprite.rotation_degrees = 0
	$Sprite.position = Vector2(0,0)
	screen_size = get_viewport_rect().size
	sprite_size = 10
	

func _physics_process(delta):
	var direction = get_direction()
	
	move_velocity(direction)
	if (state == STOP):
		velocity.x = 0
		
	move_and_slide(velocity * speed)
	set_flip()
	set_ani()
	
	position.x = clamp(position.x, 0 + sprite_size, screen_size.x - sprite_size)
	#스크린 밖으로 나가지 못하게

func die():
	state = STOP
	print("die")
	$AnimationPlayer.play("die")
	yield(get_node("AnimationPlayer"), "animation_finished")
	get_tree().reload_current_scene()

func move_velocity(direction):
	var velo = velocity
	velo.x = speed * direction
	
	velocity = velo

func get_direction():
	return Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")

func set_flip():
	if velocity.x == 0:
		return
	$Sprite.flip_h = true if velocity.x < 0 else false
	
func set_ani():
	if state == STOP:
		return
		
	var anim_name = "idle"
	if velocity.x != 0:
		anim_name = "run"	
	$AnimationPlayer.play(anim_name)

 

extends RigidBody2D


func _ready():
	$Sprite.frame = rand_range(0, 3)

func _process(delta):
	if position.y > 250:
		queue_free()
	

func _on_PlayerDetector_body_entered(body):
	body.die()

func _on_SelfDetector_body_entered(body):
	die()


func die():
	queue_free()