8 Adding Slime and Spike to the Game

We will add Slime, which won't interact with the player, just in the env, will move to and fro.

Spike is a stationary obstracle to the player.

For Slime, we just use the same functionality from the Saw, but as addition we flip the AnimatedSprite which we have already learned on Player as well.

func move(delta):
	if moving_left:
		animated_sprite_2d.position.x -= SPEED * delta
		if animated_sprite_2d.position.x < -1 * MAX_LEFT:
			animated_sprite_2d.position.x = -1 * MAX_LEFT
			moving_left = false
			animated_sprite_2d.flip_h = true
	else:
		animated_sprite_2d.position.x += SPEED * delta
		if animated_sprite_2d.position.x > MAX_RIGHT:
			animated_sprite_2d.position.x = MAX_RIGHT
			moving_left = true
			animated_sprite_2d.flip_h = false
			
	pass

For the Spike, we use the same as in Saw to detech the player and call the hit function in the player passing the direction info.

func _on_body_entered(body):
	var direction = 1
	if body.global_position.x < global_position.x:
		direction = -1
	body.hit(direction)
	pass # Replace with function body.