2015-11-21 15:13:43 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
extends RigidBody2D
|
|
|
|
|
|
|
|
# Character Demo, written by Juan Linietsky.
|
|
|
|
#
|
|
|
|
# Implementation of a 2D Character controller.
|
|
|
|
# This implementation uses the physics engine for
|
|
|
|
# controlling a character, in a very similar way
|
|
|
|
# than a 3D character controller would be implemented.
|
|
|
|
#
|
|
|
|
# Using the physics engine for this has the main
|
|
|
|
# advantages:
|
|
|
|
# -Easy to write.
|
|
|
|
# -Interaction with other physics-based objects is free
|
|
|
|
# -Only have to deal with the object linear velocity, not position
|
|
|
|
# -All collision/area framework available
|
|
|
|
#
|
|
|
|
# But also has the following disadvantages:
|
|
|
|
#
|
|
|
|
# -Objects may bounce a little bit sometimes
|
|
|
|
# -Going up ramps sends the chracter flying up, small hack is needed.
|
|
|
|
# -A ray collider is needed to avoid sliding down on ramps and
|
|
|
|
# undesiderd bumps, small steps and rare numerical precision errors.
|
|
|
|
# (another alternative may be to turn on friction when the character is not moving).
|
|
|
|
# -Friction cant be used, so floor velocity must be considered
|
|
|
|
# for moving platforms.
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Member variables
|
|
|
|
var anim = ""
|
|
|
|
var siding_left = false
|
|
|
|
var jumping = false
|
|
|
|
var stopping_jump = false
|
|
|
|
var shooting = false
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
var WALK_ACCEL = 800.0
|
2015-11-21 15:13:43 +00:00
|
|
|
var WALK_DEACCEL = 800.0
|
|
|
|
var WALK_MAX_VELOCITY = 200.0
|
2014-02-10 01:10:30 +00:00
|
|
|
var AIR_ACCEL = 200.0
|
2015-11-21 15:13:43 +00:00
|
|
|
var AIR_DEACCEL = 200.0
|
|
|
|
var JUMP_VELOCITY = 460
|
|
|
|
var STOP_JUMP_FORCE = 900.0
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
var MAX_FLOOR_AIRBORNE_TIME = 0.15
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
var airborne_time = 1e20
|
|
|
|
var shoot_time = 1e20
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
var MAX_SHOOT_POSE_TIME = 0.3
|
|
|
|
|
|
|
|
var bullet = preload("res://bullet.xml")
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
var floor_h_velocity = 0.0
|
2014-02-10 01:10:30 +00:00
|
|
|
var enemy
|
|
|
|
|
2015-01-03 19:52:37 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
func _integrate_forces(s):
|
2014-02-10 01:10:30 +00:00
|
|
|
var lv = s.get_linear_velocity()
|
|
|
|
var step = s.get_step()
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
var new_anim = anim
|
|
|
|
var new_siding_left = siding_left
|
2015-01-03 19:52:37 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
# Get the controls
|
|
|
|
var move_left = Input.is_action_pressed("move_left")
|
|
|
|
var move_right = Input.is_action_pressed("move_right")
|
|
|
|
var jump = Input.is_action_pressed("jump")
|
|
|
|
var shoot = Input.is_action_pressed("shoot")
|
|
|
|
var spawn = Input.is_action_pressed("spawn")
|
|
|
|
|
|
|
|
if spawn:
|
|
|
|
var e = enemy.instance()
|
|
|
|
var p = get_pos()
|
|
|
|
p.y = p.y - 100
|
|
|
|
e.set_pos(p)
|
|
|
|
get_parent().add_child(e)
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Deapply prev floor velocity
|
|
|
|
lv.x -= floor_h_velocity
|
|
|
|
floor_h_velocity = 0.0
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
# Find the floor (a contact with upwards facing collision normal)
|
2015-11-21 15:13:43 +00:00
|
|
|
var found_floor = false
|
|
|
|
var floor_index = -1
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
for x in range(s.get_contact_count()):
|
|
|
|
var ci = s.get_contact_local_normal(x)
|
2015-11-21 15:13:43 +00:00
|
|
|
if (ci.dot(Vector2(0, -1)) > 0.6):
|
|
|
|
found_floor = true
|
|
|
|
floor_index = x
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
# A good idea when impementing characters of all kinds,
|
2015-11-21 15:13:43 +00:00
|
|
|
# compensates for physics imprecission, as well as human reaction delay.
|
2014-02-10 01:10:30 +00:00
|
|
|
if (shoot and not shooting):
|
2015-11-21 15:13:43 +00:00
|
|
|
shoot_time = 0
|
2014-02-10 01:10:30 +00:00
|
|
|
var bi = bullet.instance()
|
|
|
|
var ss
|
|
|
|
if (siding_left):
|
2015-11-21 15:13:43 +00:00
|
|
|
ss = -1.0
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
ss = 1.0
|
|
|
|
var pos = get_pos() + get_node("bullet_shoot").get_pos()*Vector2(ss, 1.0)
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
bi.set_pos(pos)
|
|
|
|
get_parent().add_child(bi)
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
bi.set_linear_velocity(Vector2(800.0*ss, -80))
|
|
|
|
get_node("sprite/smoke").set_emitting(true)
|
2014-02-10 01:10:30 +00:00
|
|
|
get_node("sound").play("shoot")
|
2015-11-21 15:13:43 +00:00
|
|
|
PS2D.body_add_collision_exception(bi.get_rid(), get_rid()) # Make bullet and this not collide
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
shoot_time += step
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (found_floor):
|
2015-11-21 15:13:43 +00:00
|
|
|
airborne_time = 0.0
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
airborne_time += step # Time it spent in the air
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
var on_floor = airborne_time < MAX_FLOOR_AIRBORNE_TIME
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Process jump
|
2014-02-10 01:10:30 +00:00
|
|
|
if (jumping):
|
2015-11-21 15:13:43 +00:00
|
|
|
if (lv.y > 0):
|
|
|
|
# Set off the jumping flag if going down
|
|
|
|
jumping = false
|
2014-02-10 01:10:30 +00:00
|
|
|
elif (not jump):
|
2015-11-21 15:13:43 +00:00
|
|
|
stopping_jump = true
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
if (stopping_jump):
|
|
|
|
lv.y += STOP_JUMP_FORCE*step
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (on_floor):
|
|
|
|
# Process logic when character is on floor
|
|
|
|
if (move_left and not move_right):
|
|
|
|
if (lv.x > -WALK_MAX_VELOCITY):
|
2015-11-21 15:13:43 +00:00
|
|
|
lv.x -= WALK_ACCEL*step
|
2014-02-10 01:10:30 +00:00
|
|
|
elif (move_right and not move_left):
|
|
|
|
if (lv.x < WALK_MAX_VELOCITY):
|
2015-11-21 15:13:43 +00:00
|
|
|
lv.x += WALK_ACCEL*step
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
|
|
|
var xv = abs(lv.x)
|
2015-11-21 15:13:43 +00:00
|
|
|
xv -= WALK_DEACCEL*step
|
|
|
|
if (xv < 0):
|
|
|
|
xv = 0
|
|
|
|
lv.x = sign(lv.x)*xv
|
|
|
|
|
|
|
|
# Check jump
|
2014-02-10 01:10:30 +00:00
|
|
|
if (not jumping and jump):
|
2015-11-21 15:13:43 +00:00
|
|
|
lv.y = -JUMP_VELOCITY
|
|
|
|
jumping = true
|
|
|
|
stopping_jump = false
|
2014-02-10 01:10:30 +00:00
|
|
|
get_node("sound").play("jump")
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Check siding
|
2014-02-10 01:10:30 +00:00
|
|
|
if (lv.x < 0 and move_left):
|
2015-11-21 15:13:43 +00:00
|
|
|
new_siding_left = true
|
2014-02-10 01:10:30 +00:00
|
|
|
elif (lv.x > 0 and move_right):
|
2015-11-21 15:13:43 +00:00
|
|
|
new_siding_left = false
|
2014-02-10 01:10:30 +00:00
|
|
|
if (jumping):
|
2015-11-21 15:13:43 +00:00
|
|
|
new_anim = "jumping"
|
|
|
|
elif (abs(lv.x) < 0.1):
|
|
|
|
if (shoot_time < MAX_SHOOT_POSE_TIME):
|
|
|
|
new_anim = "idle_weapon"
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
new_anim = "idle"
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
if (shoot_time < MAX_SHOOT_POSE_TIME):
|
|
|
|
new_anim = "run_weapon"
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
new_anim = "run"
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
|
|
|
# Process logic when the character is in the air
|
|
|
|
if (move_left and not move_right):
|
|
|
|
if (lv.x > -WALK_MAX_VELOCITY):
|
2015-11-21 15:13:43 +00:00
|
|
|
lv.x -= AIR_ACCEL*step
|
2014-02-10 01:10:30 +00:00
|
|
|
elif (move_right and not move_left):
|
|
|
|
if (lv.x < WALK_MAX_VELOCITY):
|
2015-11-21 15:13:43 +00:00
|
|
|
lv.x += AIR_ACCEL*step
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
|
|
|
var xv = abs(lv.x)
|
2015-11-21 15:13:43 +00:00
|
|
|
xv -= AIR_DEACCEL*step
|
|
|
|
if (xv < 0):
|
|
|
|
xv = 0
|
|
|
|
lv.x = sign(lv.x)*xv
|
|
|
|
|
|
|
|
if (lv.y < 0):
|
|
|
|
if (shoot_time < MAX_SHOOT_POSE_TIME):
|
|
|
|
new_anim = "jumping_weapon"
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
new_anim = "jumping"
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
if (shoot_time < MAX_SHOOT_POSE_TIME):
|
|
|
|
new_anim = "falling_weapon"
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
new_anim = "falling"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Update siding
|
|
|
|
if (new_siding_left != siding_left):
|
2014-02-10 01:10:30 +00:00
|
|
|
if (new_siding_left):
|
2015-11-21 15:13:43 +00:00
|
|
|
get_node("sprite").set_scale(Vector2(-1, 1))
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
get_node("sprite").set_scale(Vector2(1, 1))
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
siding_left = new_siding_left
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Change animation
|
|
|
|
if (new_anim != anim):
|
|
|
|
anim = new_anim
|
|
|
|
get_node("anim").play(anim)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
shooting = shoot
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Apply floor velocity
|
|
|
|
if (found_floor):
|
|
|
|
floor_h_velocity = s.get_contact_collider_velocity_at_pos(floor_index).x
|
|
|
|
lv.x += floor_h_velocity
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Finally, apply gravity and set back the linear velocity
|
|
|
|
lv += s.get_total_gravity()*step
|
|
|
|
s.set_linear_velocity(lv)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
2015-11-21 15:13:43 +00:00
|
|
|
enemy = ResourceLoader.load("res://enemy.xml")
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
# if !Globals.has_singleton("Facebook"):
|
2015-11-21 15:13:43 +00:00
|
|
|
# return
|
2014-02-10 01:10:30 +00:00
|
|
|
# var Facebook = Globals.get_singleton("Facebook")
|
|
|
|
# var link = Globals.get("facebook/link")
|
|
|
|
# var icon = Globals.get("facebook/icon")
|
|
|
|
# var msg = "I just sneezed on your wall! Beat my score and Stop the Running nose!"
|
|
|
|
# var title = "I just sneezed on your wall!"
|
|
|
|
# Facebook.post("feed", msg, title, link, icon)
|