Update 2D physics platformer

This commit is contained in:
Aaron Franke
2020-02-03 03:08:00 -05:00
parent 375d5d13d2
commit 3eeba859b1
17 changed files with 143 additions and 156 deletions

View File

@@ -1,53 +1,30 @@
class_name Enemy
extends RigidBody2D
class_name Enemy
# Member variables
const WALK_SPEED = 50
const STATE_WALKING = 0
const STATE_DYING = 1
# state machine
var state = STATE_WALKING
enum State {
WALKING,
DYING
}
var state = State.WALKING
var direction = -1
var anim = ""
onready var rc_left = $RaycastLeft
onready var rc_right = $RaycastRight
var Bullet = preload("res://player/bullet.gd")
func _die():
queue_free()
func _pre_explode():
#make sure nothing collides against this
$Shape1.queue_free()
$Shape2.queue_free()
$Shape3.queue_free()
# Stay there
mode = MODE_STATIC
($SoundExplode as AudioStreamPlayer2D).play()
func _bullet_collider(cc, s, dp):
mode = MODE_RIGID
state = STATE_DYING
s.set_angular_velocity(sign(dp.x) * 33.0)
set_friction(1)
cc.disable()
($SoundHit as AudioStreamPlayer2D).play()
onready var rc_left = $RaycastLeft
onready var rc_right = $RaycastRight
func _integrate_forces(s):
var lv = s.get_linear_velocity()
var new_anim = anim
if state == STATE_DYING:
if state == State.DYING:
new_anim = "explode"
elif state == STATE_WALKING:
elif state == State.WALKING:
new_anim = "walk"
var wall_side = 0.0
@@ -81,6 +58,31 @@ func _integrate_forces(s):
if anim != new_anim:
anim = new_anim
($Anim as AnimationPlayer).play(anim)
($AnimationPlayer as AnimationPlayer).play(anim)
s.set_linear_velocity(lv)
func _die():
queue_free()
func _pre_explode():
#make sure nothing collides against this
$Shape1.queue_free()
$Shape2.queue_free()
$Shape3.queue_free()
# Stay there
mode = MODE_STATIC
($SoundExplode as AudioStreamPlayer2D).play()
func _bullet_collider(cc, s, dp):
mode = MODE_RIGID
state = State.DYING
s.set_angular_velocity(sign(dp.x) * 33.0)
set_friction(1)
cc.disable()
($SoundHit as AudioStreamPlayer2D).play()