| 
									
										
										
										
											2024-07-22 22:23:44 +00:00
										 |  |  | extends CharacterBody2D | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @export var SPEED = 300.0 | 
					
						
							|  |  |  | @export var JUMP_VELOCITY = -300.0 | 
					
						
							|  |  |  | @export var BUMP_FORCE = 80.0 | 
					
						
							|  |  |  | @export var PUSH_FORCE = 750 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @onready var right_ray = $RightRay | 
					
						
							|  |  |  | @onready var left_ray = $LeftRay | 
					
						
							|  |  |  | @onready var marker_right = $MarkerRight | 
					
						
							|  |  |  | @onready var marker_left = $MarkerLeft | 
					
						
							| 
									
										
										
										
											2024-07-22 23:38:39 +00:00
										 |  |  | @onready var animated_sprite = $AnimatedSprite2D | 
					
						
							| 
									
										
										
										
											2024-07-22 22:23:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | var faceLeft = false | 
					
						
							|  |  |  | var pushLeftEnabled = false | 
					
						
							|  |  |  | var pushRightEnabled = false | 
					
						
							|  |  |  | var pushTarget | 
					
						
							| 
									
										
										
										
											2024-08-13 01:01:40 +00:00
										 |  |  | var living = true | 
					
						
							| 
									
										
										
										
											2024-07-22 22:23:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 23:45:33 +00:00
										 |  |  | #preload bullet scene to be able to shoot it | 
					
						
							| 
									
										
										
										
											2024-07-22 22:23:44 +00:00
										 |  |  | var bullet = preload("res://scenes/bullet.tscn") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Get the gravity from the project settings to be synced with RigidBody nodes. | 
					
						
							|  |  |  | var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-13 01:01:40 +00:00
										 |  |  | func die(): | 
					
						
							|  |  |  | 	print("You died!") | 
					
						
							|  |  |  | 	animated_sprite.play("death") | 
					
						
							|  |  |  | 	living = false | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 22:23:44 +00:00
										 |  |  | func _physics_process(delta): | 
					
						
							| 
									
										
										
										
											2024-08-13 01:01:40 +00:00
										 |  |  | 	# Add the gravity. | 
					
						
							| 
									
										
										
										
											2024-07-22 22:23:44 +00:00
										 |  |  | 	if not is_on_floor(): | 
					
						
							|  |  |  | 		velocity.y += gravity * delta | 
					
						
							| 
									
										
										
										
											2024-08-13 01:01:40 +00:00
										 |  |  | 	if living: | 
					
						
							|  |  |  | 		# Handle jump. | 
					
						
							|  |  |  | 		if Input.is_action_just_pressed("ui_accept") and is_on_floor(): | 
					
						
							|  |  |  | 			velocity.y = JUMP_VELOCITY | 
					
						
							|  |  |  | 		# Hand force push (melee attack) | 
					
						
							|  |  |  | 		if Input.is_action_just_pressed("push") && pushRightEnabled && faceLeft == false: | 
					
						
							|  |  |  | 			print("I should force push") | 
					
						
							|  |  |  | 			pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 10) | 
					
						
							|  |  |  | 			pushRightEnabled = false | 
					
						
							|  |  |  | 		if Input.is_action_just_pressed("push") && pushLeftEnabled: | 
					
						
							|  |  |  | 			pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 10) | 
					
						
							|  |  |  | 			pushLeftEnabled = false | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		if Input.is_action_just_pressed("shoot"): | 
					
						
							|  |  |  | 			#make a bullet -- must be a scene in order to "instantiate" | 
					
						
							|  |  |  | 			var myBullet = bullet.instantiate() | 
					
						
							|  |  |  | 			#set bullet speed? | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			#"owner" adds it to the character's parent node - the game scene | 
					
						
							|  |  |  | 			owner.add_child(myBullet) | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			#put the bullet at the Marker, specifically at its global coordinates | 
					
						
							|  |  |  | 			if not faceLeft: | 
					
						
							|  |  |  | 				#facing right | 
					
						
							|  |  |  | 				myBullet.transform = marker_right.global_transform | 
					
						
							|  |  |  | 			else: | 
					
						
							|  |  |  | 				#facing left | 
					
						
							|  |  |  | 				myBullet.transform = marker_left.global_transform | 
					
						
							|  |  |  | 				myBullet.setSpeed(-750) | 
					
						
							|  |  |  | 			pass | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		# Get the input direction and handle the movement/deceleration. | 
					
						
							|  |  |  | 		# As good practice, you should replace UI actions with custom gameplay actions. | 
					
						
							|  |  |  | 		var direction = Input.get_axis("ui_left", "ui_right") | 
					
						
							|  |  |  | 		if direction <0: | 
					
						
							|  |  |  | 			faceLeft = true | 
					
						
							|  |  |  | 		if direction >0: | 
					
						
							|  |  |  | 			faceLeft = false | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		if direction: | 
					
						
							|  |  |  | 			velocity.x = direction * SPEED | 
					
						
							|  |  |  | 			animated_sprite.flip_h = true if direction <0 else false | 
					
						
							| 
									
										
										
										
											2024-07-29 23:45:33 +00:00
										 |  |  | 		else: | 
					
						
							| 
									
										
										
										
											2024-08-13 01:01:40 +00:00
										 |  |  | 			velocity.x = move_toward(velocity.x, 0, SPEED) | 
					
						
							|  |  |  | 		if is_on_floor(): | 
					
						
							|  |  |  | 			if direction == 0: | 
					
						
							|  |  |  | 				animated_sprite.play("idle") | 
					
						
							|  |  |  | 			else: | 
					
						
							|  |  |  | 				animated_sprite.play("run") | 
					
						
							| 
									
										
										
										
											2024-07-22 23:38:39 +00:00
										 |  |  | 		else: | 
					
						
							| 
									
										
										
										
											2024-08-13 01:01:40 +00:00
										 |  |  | 			animated_sprite.play("jump") | 
					
						
							| 
									
										
										
										
											2024-07-22 22:23:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-13 01:01:40 +00:00
										 |  |  | 		move_and_slide() | 
					
						
							|  |  |  | 		if right_ray.is_colliding(): | 
					
						
							|  |  |  | 			print("right ray is colliding") | 
					
						
							|  |  |  | 			var collider = right_ray.get_collider() | 
					
						
							|  |  |  | 			#what did we hit? | 
					
						
							|  |  |  | 			if collider is Node: | 
					
						
							|  |  |  | 				if collider.is_in_group("boxes"): | 
					
						
							|  |  |  | 					print("This is a pushable box") | 
					
						
							|  |  |  | 					pushRightEnabled = true | 
					
						
							|  |  |  | 					pushTarget = collider | 
					
						
							|  |  |  | 		else: | 
					
						
							|  |  |  | 			pushRightEnabled = false | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		if left_ray.is_colliding(): | 
					
						
							|  |  |  | 			print("left ray is colliding") | 
					
						
							|  |  |  | 			var collider = left_ray.get_collider() | 
					
						
							|  |  |  | 			if collider is Node: | 
					
						
							|  |  |  | 				if collider.is_in_group("boxes"): | 
					
						
							|  |  |  | 					pushLeftEnabled = true | 
					
						
							|  |  |  | 					pushTarget = collider | 
					
						
							|  |  |  | 		else: | 
					
						
							|  |  |  | 			pushLeftEnabled = false | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 		#loop through the collisions | 
					
						
							|  |  |  | 		for i in get_slide_collision_count(): | 
					
						
							|  |  |  | 			var c = get_slide_collision(i) | 
					
						
							|  |  |  | 			if c.get_collider() is RigidBody2D: | 
					
						
							|  |  |  | 				c.get_collider().apply_central_impulse(-c.get_normal()* BUMP_FORCE) |