Skip to content

Instantly share code, notes, and snippets.

@YeOldeDM
Created June 12, 2018 19:01
Show Gist options
  • Save YeOldeDM/09dc4159feb05b9925a9cc9a6707045c to your computer and use it in GitHub Desktop.
Save YeOldeDM/09dc4159feb05b9925a9cc9a6707045c to your computer and use it in GitHub Desktop.
extends KinematicBody2D
var RISING_GRAVITY = 400
var FALLING_GRAVITY = 1200
var MAX_FLOOR_SPEED = 140
var FLOOR_RUN_FORCE = 600
var FLOOR_STOP_FORCE = 1600
var AIR_RUN_FORCE = 100
var AIR_STOP_FORCE = 160
var JUMP_FORCE = 300
var DOUBLEJUMP_FORCE = 240
var JUMP_STOP_FORCE = 1280
var WALLJUMP_FORCE = 110
var WALLSLIDE_FRICTION = 0.75
var COYOTE_TIME = 0.21
var PUNCH_KICK = 150
var facing = 1 setget _set_facing
var velocity = Vector2()
var airtime = 0.0
var double_jumped = false
var jumping = false
var smush_h = false setget _set_smush_h
var smush_v = false setget _set_smush_v
func punch(direction):
var p = preload("res://Player/DuckPunch.tscn").instance()
get_parent().add_child(p)
p.position = position
if direction == 0:
p.scale.x = $Body.scale.x
var anim = "punch_side"
if direction == -1:
anim = "punch_up"
p.rotate(-PI/2)
elif direction == 1:
anim = "punch_down"
p.rotate(PI/2)
$Body/Arm/Animator.play(anim)
velocity -= Vector2( int(direction==0)*facing*PUNCH_KICK, direction*PUNCH_KICK)
func _physics_process(delta):
var G = FALLING_GRAVITY
if velocity.y < 0:
G = RISING_GRAVITY
var force = Vector2( 0,G )
var U = Input.is_action_pressed("UP")
var D = Input.is_action_pressed("DOWN")
var L = Input.is_action_pressed("LEFT")
var R = Input.is_action_pressed("RIGHT")
var J = Input.is_action_just_pressed("JUMP")
var P = Input.is_action_just_pressed("PUNCH")
var h = int(R) - int(L)
var v = int(D) - int(U)
var stop = true
if h <= -1:
if velocity.x <= MAX_FLOOR_SPEED and velocity.x > -MAX_FLOOR_SPEED:
force.x -= FLOOR_RUN_FORCE
stop = false
if self.facing != -1:
self.facing = -1
elif h >= 1:
if velocity.x >= -MAX_FLOOR_SPEED and velocity.x < MAX_FLOOR_SPEED:
force.x += FLOOR_RUN_FORCE
stop = false
if self.facing != 1:
self.facing = 1
if stop:
var vsign = sign(velocity.x)
var vlen = abs(velocity.x)
vlen -= FLOOR_STOP_FORCE * delta
if vlen < 0: vlen = 0
velocity.x = vlen * vsign
velocity += force * delta
$Body/Face.position.y = v * 3
if v != 0:
$Body/Face.scale.y =0.75
else:
$Body/Face.scale.y = 1
var slid = move_and_slide( velocity, Vector2(0,-1) )
if is_on_ceiling() or is_on_floor():
velocity.y = 0
if is_on_wall():
velocity.x = 0
if airtime > 0:
velocity *= WALLSLIDE_FRICTION
if is_on_floor():
airtime = 0
double_jumped = false
if J and airtime <= COYOTE_TIME or J and is_on_wall():
velocity.y = -JUMP_FORCE
airtime += COYOTE_TIME
if is_on_wall():
var n = get_slide_collision( get_slide_count()-1 ).normal
velocity.x = n.x * WALLJUMP_FORCE
if !Input.is_action_pressed("JUMP") and airtime > COYOTE_TIME and velocity.y < 0:
velocity.y += JUMP_STOP_FORCE * delta
if J and not double_jumped and airtime > COYOTE_TIME:
velocity.y = -DOUBLEJUMP_FORCE
double_jumped = true
if P:
punch(v)
airtime += delta
func _set_smush_h(what):
smush_h = what
if smush_h:
$Body.scale.x *= 0.75
else:
$Body.scale.x = facing
func _set_smush_v(what):
smush_v = what
func _set_facing( what ):
facing = what
$Body.scale.x = facing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment