Singleton und Entity für Player - SamToorchi/Godot-Zombi-Game GitHub Wiki

Entity.gd
extends KinematicBody2D

export(int) var SPEED = 0

var movedir = dir.center
var spritedir = "down"

func movement_loop():
	var motion = movedir.normalized() * SPEED
	move_and_slide(motion, Vector2(0, 0))
	
func spritedir_loop():
	match movedir:
		dir.left:
			spritedir = "left"
		dir.right:
			spritedir = "right"
		dir.up:
			spritedir = "up"
		dir.down:
			spritedir = "down"

func anim_switch(animation):
	var newanim = str(animation,spritedir)
	if $anim.current_animation != newanim:
		$anim.play(newanim)
singleton (directions.gd)
extends Node

const center = Vector2(0,0)
const left = Vector2(-1,0)
const right = Vector2(1,0)
const down = Vector2(0,1)
const up = Vector2 (0, -1)


func rand():
	var d = randi() % 4 + 1
	match d:
		1:
			return left
		2: 
			return right
		3:
			return up
		4: 
			return down
player
extends "res://scripts/entity.gd"


func controls_loop():
	var LEFT = Input.is_action_pressed("ui_left")
	var RIGHT = Input.is_action_pressed("ui_right")
	var UP = Input.is_action_pressed("ui_up")
	var DOWN = Input.is_action_pressed("ui_down")
	
	movedir.x = -int(LEFT) + int(RIGHT)
	movedir.y = -int(UP) + int(DOWN)



	
func _physics_process(delta):
	controls_loop()
	movement_loop()
	spritedir_loop()
	
	if is_on_wall():
		if spritedir == "left" and test_move(transform, dir.left):
			anim_switch("push")
		if spritedir == "right" and test_move(transform, dir.right):
			anim_switch("push")
		if spritedir == "up" and test_move(transform, dir.up):
			anim_switch("push")
		if spritedir == "down" and test_move(transform, dir.down):
			anim_switch("push")
	
	elif movedir != dir.center:
		anim_switch("walk")
	else:
		anim_switch("idle")