Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- onready var animPlayer = $AnimationPlayer
- onready var state = runState.new(self) #this creates a new instance of the state. It will call the states _init function of course kicking it all off
- onready var floorRay = $RayCastFloor
- onready var forwardRay = $RayCastForward
- var motion = Vector2()
- var soldatState = "running" #walking, running, climbing, shooting, paused, idle, dying, falling
- const up = Vector2(0, -1)
- const gravity = 20
- const walkSpeed = 12
- const runSpeed = 24
- const fallspeed = 60
- const STATE_WALKING = 0 #done
- const STATE_RUNNING = 1
- const STATE_CLIMBING = 2
- const STATE_SHOOTING = 3
- const STATE_PAUSED = 4
- const STATE_IDLE = 5
- const STATE_DYING = 6
- const STATE_FALLING = 7 #done
- func _ready():
- motion.x = walkSpeed
- motion.y += gravity
- func _physics_process(delta):
- #call the update function within the state class. Very cool
- #It doesn't matter which state we're in, this will call the function with the same name in each class that is "state" as per the onready above
- state.update(delta)
- func set_state(new_state):
- #first exit the current state
- state.exit()
- if new_state == STATE_WALKING:
- state = walkState.new(self)
- elif new_state == STATE_IDLE:
- state = idleState.new(self)
- elif new_state == STATE_FALLING:
- state = fallingState.new(self)
- elif new_state == STATE_CLIMBING:
- state = climbingState.new(self)
- elif new_state == STATE_RUNNING:
- state = runState.new(self)
- emit_signal("state_changed", self) #we'll send the bat itself with "self"
- # class walkState --------------------------------------------------------------------
- # we're walking
- class walkState:
- var soldat
- #init gets called when an object of this class gets created
- func _init(soldat):
- self.soldat = soldat
- #print("walkin")
- soldat.animPlayer.play("walk")
- #frame by frame update
- func update(delta):
- if soldat.floorRay.is_colliding():
- soldat.motion.y += 0
- soldat.motion.x = walkSpeed
- soldat.move_and_slide(soldat.motion) #walk right
- else:
- soldat.set_state(STATE_FALLING) #start falling
- if soldat.forwardRay.is_colliding():
- soldat.set_state(STATE_CLIMBING) #start climbing
- #it's good to be able to handle input from within the state... sometimes :-)
- func input(event):
- #nothing at the moment
- pass
- #so we know when it's the end of the state, and we can perform some actions before going to another state
- func exit():
- pass
- # class runState --------------------------------------------------------------------
- # we're runing
- class runState:
- var soldat
- #init gets called when an object of this class gets created
- func _init(soldat):
- self.soldat = soldat
- #print("running")
- soldat.animPlayer.play("run")
- #frame by frame update
- func update(delta):
- if soldat.floorRay.is_colliding():
- soldat.motion.y += 0
- soldat.motion.x = runSpeed
- soldat.move_and_slide(soldat.motion) #run right
- else:
- soldat.set_state(STATE_FALLING) #start falling
- if soldat.forwardRay.is_colliding():
- soldat.set_state(STATE_CLIMBING) #start climbing
- #it's good to be able to handle input from within the state... sometimes :-)
- func input(event):
- #nothing at the moment
- pass
- #so we know when it's the end of the state, and we can perform some actions before going to another state
- func exit():
- pass
- # class idleState --------------------------------------------------------------------
- # we're chillin
- class idleState:
- var soldat
- #init gets called when an object of this class gets created
- func _init(soldat):
- self.soldat = soldat
- #print("Idle")
- #frame by frame update
- func update(delta):
- soldat.motion.y += 0
- soldat.motion.y += 0
- soldat.move_and_slide(soldat.motion)
- #so we know when it's the end of the state, and we can perform some actions before going to another state
- func exit():
- pass
- # class fallingState (7) --------------------------------------------------------------------
- # we're fallin
- class fallingState:
- var soldat
- #init gets called when an object of this class gets created
- func _init(soldat):
- self.soldat = soldat
- #print("Falling")
- soldat.animPlayer.play("falling")
- #frame by frame update
- func update(delta):
- if soldat.floorRay.is_colliding():
- soldat.set_state(STATE_RUNNING) #start walking again
- # if soldat.forwardRay.is_colliding():
- # soldat.set_state(STATE_CLIMBING) #start walking again
- else: #we're falling
- soldat.motion.y = fallspeed #fall down
- soldat.motion.x +- 0 #dont move forward whilst falling
- soldat.move_and_slide(soldat.motion)
- soldat.get_node("/root/gameLevel/HUD/debugStuff/Label4").text = str(soldat.motion)
- #so we know when it's the end of the state, and we can perform some actions before going to another state
- func exit():
- pass
- # class climbingState (7) --------------------------------------------------------------------
- # we're climbin
- class climbingState:
- var soldat
- #init gets called when an object of this class gets created
- func _init(soldat):
- self.soldat = soldat
- #print("CLIMBING")
- soldat.animPlayer.play("climb")
- #frame by frame update
- func update(delta):
- soldat.motion.y = -fallspeed #climb up
- soldat.motion.x +- 0 #no forward whilst climbing
- soldat.move_and_slide(soldat.motion)
- if !soldat.forwardRay.is_colliding(): #note the NOT
- #print("no longer climbing")
- soldat.motion.y = 0
- soldat.motion.x = walkSpeed
- soldat.set_state(STATE_RUNNING) #go back to walking
- soldat.get_node("/root/gameLevel/HUD/debugStuff/Label4").text = str(soldat.motion)
- #so we know when it's the end of the state, and we can perform some actions before going to another state
- func exit():
- pass
- #if we leave the screen, kill ourselves
- func _on_VisibilityNotifier2D_screen_exited():
- #make sure we don't die just by being added to the left of the screen
- if position.x >= 1:
- queue_free()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement