Untitled

 avatar
unknown
plain_text
a year ago
3.7 kB
20
Indexable
extends CharacterBody2D
class_name player_r
@onready var bullettscn = preload("res://Scenes/bullet.tscn")
@export var gravity = 750
@export var mspeed = 300
@export var bullets: int = 15
@export var mags: int = 2
@export var lives: int = 3
var sprint

var busy: bool


##TODO rework Zombie script, Melee script, hurt logic, rework bullets queue_free()

func _ready() -> void:
	busy = false
	_statemachine("idle")
	sprint = false
	


func _process(delta: float) -> void:
	
	if Input.is_action_just_pressed("melee"):
		_statemachine("Melee")
		
		
	if Input.is_action_pressed("sprintpressed"):
		sprint = true
	else:
		sprint = false
	
	if Input.is_action_pressed("ui_right"):
		if sprint == false:
			_statemachine("walkR", delta)
		else:
			_statemachine("RunR", delta)
			
	elif Input.is_action_pressed("ui_left"):
		if sprint == false:
			_statemachine("walkL", delta)
		else:
			_statemachine("RunL", delta)
			
	elif Input.is_action_pressed("ui_accept") and bullets > 0:
		if $AnimatedSprite2D.flip_h == false:
			_statemachine("shootR")
		elif $AnimatedSprite2D.flip_h == true:
			_statemachine("shootL")
			
		
	elif Input.is_action_just_pressed("reload"):
		_statemachine("Reload")
	
	else:
		if !busy:
			_statemachine("idle")
			
	#GRAVITY
	if is_on_floor() == false:
		global_position.y += gravity * delta
		
	move_and_slide()

func _statemachine(state, delta: float = 0):
	if state == "walkR":
		if busy == true:
			return
		$AnimatedSprite2D.flip_h = false
		$AnimatedSprite2D.play("Walk")
		global_position.x += mspeed * delta
		
	elif state == "RunR":
		if busy == true:
			return
		$AnimatedSprite2D.flip_h = false
		$AnimatedSprite2D.play("Run")
		global_position.x += mspeed * delta * 1.5
		
	elif state == "RunL":
		if busy == true:
			return
		$AnimatedSprite2D.flip_h = true
		$AnimatedSprite2D.play("Run")
		global_position.x -= mspeed * delta * 1.5
		
	elif state == "walkL":
		if busy == true:
			return
		$AnimatedSprite2D.flip_h = true
		$AnimatedSprite2D.play("Walk")
		global_position.x -= mspeed*delta
		
	elif state == "shootR":
		print(bullets)
		if busy == true:
			return
		busy = true
		var newbullet = bullettscn.instantiate()
		$Node.add_child(newbullet)
		newbullet.global_position = $ShootRight.global_position
		newbullet.dir = 1
		$AnimatedSprite2D.play("Shoot")
		$audioplayer.play(0.0)
		var randomsound = randf_range(0.85,1.225)
		$audioplayer.pitch_scale = randomsound
		bullets-=1
		await get_tree().create_timer(0.1).timeout
		busy = false
	
	elif state == "shootL":
		print(bullets)
		if busy == true:
			return
		busy = true
		var newbullet = bullettscn.instantiate()
		$Node.add_child(newbullet)
		newbullet.global_position = $ShootLeft.global_position
		newbullet.dir = -1
		$audioplayer.play(0.0)
		$AnimatedSprite2D.play("Shoot")
		var randomsound = randf_range(0.85,1.225)
		$audioplayer.pitch_scale = randomsound
		bullets-=1
		await get_tree().create_timer(0.1).timeout
		busy = false
		
	elif state == "Reload" and mags > 0:
		busy = 1
		bullets = 0
		mags -=1
		bullets = 15
		$reload.play(0.0)
		await get_tree().create_timer(0.06).timeout
		busy = 0
		
		
	
	elif state == "hurt":
		lives -=1
		$AnimatedSprite2D.play("Hurt")
		await get_tree().create_timer(0.1).timeout
		state = "idle"
	#TODO Death, Melee, State logic (hurt, shoot, melee, death)
	elif state == "Melee" and !busy:
		print("melee")
		busy = 1
		$AnimatedSprite2D.play("Melee")
		await get_tree().create_timer(0.2).timeout
		busy = 0
		_statemachine("idle")
		
	elif state == "idle":
		if busy == true:
			return
		$AnimatedSprite2D.play("Idle")
Editor is loading...
Leave a Comment