Untitled

 avatar
unknown
python
4 months ago
3.2 kB
2
Indexable
extends SGCharacterBody2D
@export var moveAccel := 5
@export var speedLimit := 300
@export var gravity := 10
@export var othersRid: RID

var onFloor: bool = false
var PlayerCollisions: Array = [null,null,null,null]

const GameLeft := "GameLeft"
const GameRight := "GameRight"
const GameUp := "GameUp"
const GameDown := "GameDown"
const GameJump := "GameJump"

const floorThreshold := -32768 #0.5 in fixed point world, x * 65536

func get_grav() -> SGFixedVector2:
	return SGFixed.from_float_vector2(Vector2(0,-1*gravity))

func _get_local_input() -> Dictionary:
	var dirVector := Input.get_vector(GameLeft,GameRight,GameUp,GameDown)
	var jumpPressed: bool = Input.is_action_just_pressed(GameJump) 
	var input:= { "inputVector": dirVector, "jump": jumpPressed}
	
	input["inputVector"] = dirVector
	return input
# Called every frame. 'delta' is the elapsed time since the previous frame.
		
func _network_process(input: Dictionary) -> void:
	var inputVector: Vector2 = input["inputVector"].normalized()
	var modifiedVel1 := Gravity(velocity)
	var modifiedVel2 := MoveRightLeft(modifiedVel1,inputVector) 
	velocity.x = modifiedVel2.x
	velocity.y = modifiedVel2.y
	move_and_slide()
	#move_and_collide(modifiedVel2)
	sync_to_physics_engine()
	DetectCollisions()
	
func DetectCollisions() -> void:
	var bodyCount = get_slide_count()
	var i: int = 0
	var j: int = 0
	if bodyCount == 0:
		print_debug("no bodies detected")
		onFloor = false
		return
	while i < get_slide_count():
		var collision = get_slide_collision(i)
		if collision.collider_rid.get_id() == othersRid.get_id():
			PlayerCollisions[j] = collision
			j += 1
		i += 1
		onFloor = FloorDetect(collision)
	print_debug(str(onFloor) + "||" + str(multiplayer.get_unique_id()))

func FloorDetect(collision: SGKinematicCollision2D) -> bool:
	return collision.normal.y < floorThreshold

func Gravity(currentVel: SGFixedVector2) -> SGFixedVector2:
	if !is_on_floor():
		var gravVel: SGFixedVector2 = addFixedV(currentVel,get_grav(),false)
		return gravVel
	return currentVel

func MoveRightLeft(currentVel: SGFixedVector2, inputVector: Vector2) -> SGFixedVector2:
	var a: SGFixedVector2 = SGFixed.from_float_vector2(inputVector).normalized()
	var b: SGFixedVector2 = SGFixed.vector2(a.x * SGFixed.from_float(moveAccel),a.y)
	return addFixedVx(currentVel,b.x,true)


func _save_state() -> Dictionary:
	var input = { "speed" = moveAccel, "position" = position, "velocity" = velocity  }
	return input
	
func _load_state(state: Dictionary) -> void:
	moveAccel = state["speed"]
	position = state["position"]
	velocity = state["velocity"]
	sync_to_physics_engine()

func addFixedV(fixedA: SGFixedVector2, fixedB: SGFixedVector2, add: bool) -> SGFixedVector2:
	var s: int
	if add:
		s = 1
	else:
		s = -1
	return SGFixed.vector2(fixedA.x + s * fixedB.x, fixedA.y + s * fixedB.y)
	
func addFixedVx(fixedA:SGFixedVector2, fixedB: int, add: bool) -> SGFixedVector2:
	var s: int
	if add:
		s = 1
	else:
		s = -1
	return SGFixed.vector2(fixedA.x + s*fixedB, fixedA.y)

func addFixedVy(fixedA:SGFixedVector2, fixedB: int, add: bool) -> SGFixedVector2:
	var s: int
	if add:
		s = 1
	else:
		s = -1
	return SGFixed.vector2(fixedA.x, fixedA.y + s*fixedB)
Editor is loading...
Leave a Comment