<commit_before>extends Node

signal controls_changed

var debug = false

const CONTROL_UP = "move_up"
const CONTROL_DOWN = "move_down"
const CONTROL_LEFT = "move_left"
const CONTROL_RIGHT = "move_right"
const CONTROL_RUN = "run"
const CONTROL_JUMP = "jump"

export(String, "move_up", "move_down", "move_left", "move_right", "run", "jump") var control_1 = "move_right"
export(String, "move_up", "move_down", "move_left", "move_right", "run", "jump") var control_2 = "move_down"

onready var climb_area = preload("res://sources/scripts/levels/climb_area.gd")

onready var player_sprites = get_node("sprites")
onready var interaction_area = get_node("interaction_area")

const WALK_SPEED = 200
const JUMP_SPEED = 300
const RUN_SPEED = 500
const CLIMB_SPEED = 200
const GROUND_FRICTION_MULTIPLIER = 0.7
const GROUND_ACCELERATION_MULTIPLIER = 0.2
const AIR_FRICTION_MULTIPLIER = 0.95
const AIR_DIRECTION_MULTIPLIER = 0.15
const JUMP_TIME_MAX = 0.2
const FALLING_JUMP_DELAY = 0.2

var jumping = false
var jump_time = 0
var falling_time = 0

const IDLE = 0
const WALKING = 1
const RUNNING = 2
const JUMPING = 3
const FALLING = 4
const CLIMBING = 5
var fsm = IDLE

var touchscreen_left = -1
var touchscreen_right = -1

const JUMP_NOT_PRESSED = -1
const JUMP_PRESSED_LEFT = 0
const JUMP_PRESSED_RIGHT = 1
var jump_pressed = JUMP_NOT_PRESSED

func _ready():
	set_process(true)
	set_process_input(true)
	
func _input(event):
	if event.type == InputEvent.SCREEN_TOUCH:
		if event.pressed:
			if event.pos.y > 200:
				if event.pos.x > 1024 / 2 && touchscreen_right == -1:
					touchscreen_right = event.index
				elif event.pos.x <= 1024 / 2 && touchscreen_left == -1:
					touchscreen_left = event.index
		else:
			if event.index == touchscreen_right:
				touchscreen_right = -1
			elif event.index == touchscreen_left:
				touchscreen_left = -1
		
			#if we release jump button we can jump again
			if touchscreen_left == -1 && jump_pressed == JUMP_PRESSED_LEFT:
				jump_pressed = JUMP_NOT_PRESSED
			elif touchscreen_right == -1 && jump_pressed == JUMP_PRESSED_RIGHT:
				jump_pressed = JUMP_NOT_PRESSED
	elif event.type == InputEvent.KEY:
		if event.is_action_released("jump"):
			jump_pressed = JUMP_NOT_PRESSED
	
func release_controls():
	touchscreen_right = -1
	touchscreen_left = -1
	
func _integrate_forces(state):
	if fsm == IDLE:
		integrate_idle(state)
	elif fsm == WALKING:
		integrate_walking(state)
	elif fsm == RUNNING:
		integrate_running(state)
	elif fsm == JUMPING:
		integrate_jumping(state)
	elif fsm == FALLING:
		integrate_falling(state)
	elif fsm == CLIMBING:
		integrate_climbing(state)
		
func integrate_idle(state):
	player_sprites.play("stand")
	var velocity = state.get_linear_velocity()
	velocity.x *= GROUND_FRICTION_MULTIPLIER
	state.set_linear_velocity(velocity)

func integrate_walking(state):
	player_sprites.play("walk")
	var velocity = state.get_linear_velocity()
	
	if abs(velocity.x) > WALK_SPEED:
		if is_action_pressed_and_available(CONTROL_LEFT):
			player_sprites.set_flip_h(true)
		elif is_action_pressed_and_available(CONTROL_RIGHT):
			player_sprites.set_flip_h(false)
		velocity.x = clamp(velocity.x * (1 - GROUND_ACCELERATION_MULTIPLIER), -RUN_SPEED, RUN_SPEED)
	else:
		if is_action_pressed_and_available(CONTROL_LEFT):
			velocity.x -= WALK_SPEED * GROUND_ACCELERATION_MULTIPLIER
			player_sprites.set_flip_h(true)
		elif is_action_pressed_and_available(CONTROL_RIGHT):
			velocity.x += WALK_SPEED * GROUND_ACCELERATION_MULTIPLIER
			player_sprites.set_flip_h(false)

		velocity.x = clamp(velocity.x, -WALK_SPEED, WALK_SPEED)

	state.set_linear_velocity(velocity)
		
func integrate_running(state):
	player_sprites.play("walk")
	var velocity = state.get_linear_velocity()
	
	if is_action_pressed_and_available(CONTROL_LEFT):
		velocity.x -= RUN_SPEED * GROUND_ACCELERATION_MULTIPLIER
		player_sprites.set_flip_h(true)
	elif is_action_pressed_and_available(CONTROL_RIGHT):
		velocity.x += RUN_SPEED * GROUND_ACCELERATION_MULTIPLIER
		player_sprites.set_flip_h(false)
		
	velocity.x = clamp(velocity.x, -RUN_SPEED, RUN_SPEED)
	
	state.set_linear_velocity(velocity)
		
func integrate_falling(state):
	player_sprites.play("jump")
	var velocity = state.get_linear_velocity()
	
	if velocity.x > -WALK_SPEED && is_action_pressed_and_available(CONTROL_LEFT):
		player_sprites.set_flip_h(true)
		velocity.x -= WALK_SPEED * AIR_DIRECTION_MULTIPLIER
	elif velocity.x < WALK_SPEED && is_action_pressed_and_available(CONTROL_RIGHT):
		player_sprites.set_flip_h(false)
		velocity.x += WALK_SPEED * AIR_DIRECTION_MULTIPLIER
	elif !is_action_pressed_and_available(CONTROL_LEFT) && !is_action_pressed_and_available(CONTROL_RIGHT):
		velocity.x *= AIR_FRICTION_MULTIPLIER
		
	velocity.x = clamp(velocity.x, -RUN_SPEED, RUN_SPEED)
	state.set_linear_velocity(velocity)
	
func integrate_jumping(state):
	player_sprites.play("jump")
	var velocity = state.get_linear_velocity()
	
	if velocity.x > -WALK_SPEED && is_action_pressed_and_available(CONTROL_LEFT):
		player_sprites.set_flip_h(true)
		velocity.x -= WALK_SPEED * AIR_DIRECTION_MULTIPLIER
	elif velocity.x < WALK_SPEED && is_action_pressed_and_available(CONTROL_RIGHT):
		player_sprites.set_flip_h(false)
		velocity.x += WALK_SPEED * AIR_DIRECTION_MULTIPLIER
	elif !is_action_pressed_and_available(CONTROL_LEFT) && !is_action_pressed_and_available(CONTROL_RIGHT):
		velocity.x *= AIR_FRICTION_MULTIPLIER
		
	velocity.x = clamp(velocity.x, -RUN_SPEED, RUN_SPEED)<commit_msg>fix : character sprite flips slide while falling and moving in an opposite direction<commit_after>extends Node

signal controls_changed

var debug = false

const CONTROL_UP = "move_up"
const CONTROL_DOWN = "move_down"
const CONTROL_LEFT = "move_left"
const CONTROL_RIGHT = "move_right"
const CONTROL_RUN = "run"
const CONTROL_JUMP = "jump"

export(String, "move_up", "move_down", "move_left", "move_right", "run", "jump") var control_1 = "move_right"
export(String, "move_up", "move_down", "move_left", "move_right", "run", "jump") var control_2 = "move_down"

onready var climb_area = preload("res://sources/scripts/levels/climb_area.gd")

onready var player_sprites = get_node("sprites")
onready var interaction_area = get_node("interaction_area")

const WALK_SPEED = 200
const JUMP_SPEED = 300
const RUN_SPEED = 500
const CLIMB_SPEED = 200
const GROUND_FRICTION_MULTIPLIER = 0.7
const GROUND_ACCELERATION_MULTIPLIER = 0.2
const AIR_FRICTION_MULTIPLIER = 0.95
const AIR_DIRECTION_MULTIPLIER = 0.15
const JUMP_TIME_MAX = 0.2
const FALLING_JUMP_DELAY = 0.2

var jumping = false
var jump_time = 0
var falling_time = 0

const IDLE = 0
const WALKING = 1
const RUNNING = 2
const JUMPING = 3
const FALLING = 4
const CLIMBING = 5
var fsm = IDLE

var touchscreen_left = -1
var touchscreen_right = -1

const JUMP_NOT_PRESSED = -1
const JUMP_PRESSED_LEFT = 0
const JUMP_PRESSED_RIGHT = 1
var jump_pressed = JUMP_NOT_PRESSED

func _ready():
	set_process(true)
	set_process_input(true)
	
func _input(event):
	if event.type == InputEvent.SCREEN_TOUCH:
		if event.pressed:
			if event.pos.y > 200:
				if event.pos.x > 1024 / 2 && touchscreen_right == -1:
					touchscreen_right = event.index
				elif event.pos.x <= 1024 / 2 && touchscreen_left == -1:
					touchscreen_left = event.index
		else:
			if event.index == touchscreen_right:
				touchscreen_right = -1
			elif event.index == touchscreen_left:
				touchscreen_left = -1
		
			#if we release jump button we can jump again
			if touchscreen_left == -1 && jump_pressed == JUMP_PRESSED_LEFT:
				jump_pressed = JUMP_NOT_PRESSED
			elif touchscreen_right == -1 && jump_pressed == JUMP_PRESSED_RIGHT:
				jump_pressed = JUMP_NOT_PRESSED
	elif event.type == InputEvent.KEY:
		if event.is_action_released("jump"):
			jump_pressed = JUMP_NOT_PRESSED
	
func release_controls():
	touchscreen_right = -1
	touchscreen_left = -1
	
func _integrate_forces(state):
	if fsm == IDLE:
		integrate_idle(state)
	elif fsm == WALKING:
		integrate_walking(state)
	elif fsm == RUNNING:
		integrate_running(state)
	elif fsm == JUMPING:
		integrate_jumping(state)
	elif fsm == FALLING:
		integrate_falling(state)
	elif fsm == CLIMBING:
		integrate_climbing(state)
		
func integrate_idle(state):
	player_sprites.play("stand")
	var velocity = state.get_linear_velocity()
	velocity.x *= GROUND_FRICTION_MULTIPLIER
	state.set_linear_velocity(velocity)

func integrate_walking(state):
	player_sprites.play("walk")
	var velocity = state.get_linear_velocity()
	
	if abs(velocity.x) > WALK_SPEED:
		if is_action_pressed_and_available(CONTROL_LEFT):
			player_sprites.set_flip_h(true)
		elif is_action_pressed_and_available(CONTROL_RIGHT):
			player_sprites.set_flip_h(false)
		velocity.x = clamp(velocity.x * (1 - GROUND_ACCELERATION_MULTIPLIER), -RUN_SPEED, RUN_SPEED)
	else:
		if is_action_pressed_and_available(CONTROL_LEFT):
			velocity.x -= WALK_SPEED * GROUND_ACCELERATION_MULTIPLIER
			player_sprites.set_flip_h(true)
		elif is_action_pressed_and_available(CONTROL_RIGHT):
			velocity.x += WALK_SPEED * GROUND_ACCELERATION_MULTIPLIER
			player_sprites.set_flip_h(false)

		velocity.x = clamp(velocity.x, -WALK_SPEED, WALK_SPEED)

	state.set_linear_velocity(velocity)
		
func integrate_running(state):
	player_sprites.play("walk")
	var velocity = state.get_linear_velocity()
	
	if is_action_pressed_and_available(CONTROL_LEFT):
		velocity.x -= RUN_SPEED * GROUND_ACCELERATION_MULTIPLIER
		player_sprites.set_flip_h(true)
	elif is_action_pressed_and_available(CONTROL_RIGHT):
		velocity.x += RUN_SPEED * GROUND_ACCELERATION_MULTIPLIER
		player_sprites.set_flip_h(false)
		
	velocity.x = clamp(velocity.x, -RUN_SPEED, RUN_SPEED)
	
	state.set_linear_velocity(velocity)
		
func integrate_falling(state):
	player_sprites.play("jump")
	var velocity = state.get_linear_velocity()
	
	if velocity.x > -WALK_SPEED && is_action_pressed_and_available(CONTROL_LEFT):
		velocity.x -= WALK_SPEED * AIR_DIRECTION_MULTIPLIER
	elif velocity.x < WALK_SPEED && is_action_pressed_and_available(CONTROL_RIGHT):
		velocity.x += WALK_SPEED * AIR_DIRECTION_MULTIPLIER
	elif !is_action_pressed_and_available(CONTROL_LEFT) && !is_action_pressed_and_available(CONTROL_RIGHT):
		velocity.x *= AIR_FRICTION_MULTIPLIER
		
	velocity.x = clamp(velocity.x, -RUN_SPEED, RUN_SPEED)
	state.set_linear_velocity(velocity)
	
func integrate_jumping(state):
	player_sprites.play("jump")
	var velocity = state.get_linear_velocity()
	
	if velocity.x > -WALK_SPEED && is_action_pressed_and_available(CONTROL_LEFT):
		player_sprites.set_flip_h(true)
		velocity.x -= WALK_SPEED * AIR_DIRECTION_MULTIPLIER
	elif velocity.x < WALK_SPEED && is_action_pressed_and_available(CONTROL_RIGHT):
		player_sprites.set_flip_h(false)
		velocity.x += WALK_SPEED * AIR_DIRECTION_MULTIPLIER
	elif !is_action_pressed_and_available(CONTROL_LEFT) && !is_action_pressed_and_available(CONTROL_RIGHT):
		velocity.x *= AIR_FRICTION_MULTIPLIER
		
	velocity.x = clamp(velocity.x, -RUN_SPEED, RUN_SPEED)
{'fraction_non_alphanumeric': 0.09390490178079677, 'fraction_numerical': 0.011565999632825409, 'mean_word_length': 5.675857843137255, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3094139', 'n_tokens_mistral': 4582, 'n_tokens_neox': 3973, 'n_words': 753}