Advertisement
Guest User

Untitled

a guest
May 10th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.75 KB | None | 0 0
  1.     extends KinematicBody2D
  2.    
  3.     onready var animPlayer = $AnimationPlayer
  4.     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
  5.    
  6.     onready var floorRay = $RayCastFloor
  7.     onready var forwardRay = $RayCastForward
  8.     var motion = Vector2()
  9.    
  10.     var soldatState = "running" #walking, running, climbing, shooting, paused, idle, dying, falling
  11.    
  12.     const up = Vector2(0, -1)
  13.     const gravity = 20
  14.     const walkSpeed = 12
  15.     const runSpeed = 24
  16.     const fallspeed = 60
  17.    
  18.     const STATE_WALKING     = 0 #done
  19.     const STATE_RUNNING     = 1
  20.     const STATE_CLIMBING    = 2
  21.     const STATE_SHOOTING    = 3
  22.     const STATE_PAUSED      = 4
  23.     const STATE_IDLE        = 5
  24.     const STATE_DYING       = 6
  25.     const STATE_FALLING     = 7 #done
  26.    
  27.    
  28.     func _ready():
  29.         motion.x = walkSpeed
  30.         motion.y += gravity
  31.        
  32.    
  33.     func _physics_process(delta):
  34.    
  35.         #call the update function within the state class.  Very cool
  36.         #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
  37.         state.update(delta)
  38.    
  39.    
  40.        
  41.     func set_state(new_state):
  42.         #first exit the current state
  43.         state.exit()
  44.         if new_state == STATE_WALKING:
  45.             state = walkState.new(self)
  46.         elif new_state == STATE_IDLE:
  47.             state = idleState.new(self)
  48.         elif new_state == STATE_FALLING:
  49.             state = fallingState.new(self)
  50.         elif new_state == STATE_CLIMBING:
  51.             state = climbingState.new(self)
  52.         elif new_state == STATE_RUNNING:
  53.             state = runState.new(self)
  54.         emit_signal("state_changed", self) #we'll send the bat itself with "self"
  55.    
  56.    
  57.    
  58.    
  59.     # class walkState --------------------------------------------------------------------
  60.     # we're walking
  61.     class walkState:
  62.         var soldat
  63.        
  64.         #init gets called when an object of this class gets created
  65.         func _init(soldat):
  66.             self.soldat = soldat
  67.             #print("walkin")
  68.             soldat.animPlayer.play("walk")
  69.    
  70.         #frame by frame update
  71.         func update(delta):
  72.             if soldat.floorRay.is_colliding():
  73.                 soldat.motion.y += 0
  74.                 soldat.motion.x = walkSpeed
  75.                 soldat.move_and_slide(soldat.motion) #walk right
  76.             else:
  77.                 soldat.set_state(STATE_FALLING) #start falling
  78.             if soldat.forwardRay.is_colliding():
  79.                 soldat.set_state(STATE_CLIMBING) #start climbing
  80.    
  81.         #it's good to be able to handle input from within the state... sometimes :-)
  82.         func input(event):
  83.             #nothing at the moment
  84.             pass
  85.    
  86.         #so we know when it's the end of the state, and we can perform some actions before going to another state
  87.         func exit():
  88.             pass
  89.    
  90.    
  91.    
  92.     # class runState --------------------------------------------------------------------
  93.     # we're runing
  94.     class runState:
  95.         var soldat
  96.        
  97.         #init gets called when an object of this class gets created
  98.         func _init(soldat):
  99.             self.soldat = soldat
  100.             #print("running")
  101.             soldat.animPlayer.play("run")
  102.    
  103.         #frame by frame update
  104.         func update(delta):
  105.             if soldat.floorRay.is_colliding():
  106.                 soldat.motion.y += 0
  107.                 soldat.motion.x = runSpeed
  108.                 soldat.move_and_slide(soldat.motion) #run right
  109.             else:
  110.                 soldat.set_state(STATE_FALLING) #start falling
  111.                
  112.             if soldat.forwardRay.is_colliding():
  113.                 soldat.set_state(STATE_CLIMBING) #start climbing
  114.    
  115.         #it's good to be able to handle input from within the state... sometimes :-)
  116.         func input(event):
  117.             #nothing at the moment
  118.             pass
  119.    
  120.         #so we know when it's the end of the state, and we can perform some actions before going to another state
  121.         func exit():
  122.             pass
  123.    
  124.    
  125.     # class idleState --------------------------------------------------------------------
  126.     # we're chillin
  127.     class idleState:
  128.         var soldat
  129.        
  130.         #init gets called when an object of this class gets created
  131.         func _init(soldat):
  132.             self.soldat = soldat
  133.             #print("Idle")
  134.    
  135.         #frame by frame update
  136.         func update(delta):
  137.             soldat.motion.y += 0
  138.             soldat.motion.y += 0
  139.             soldat.move_and_slide(soldat.motion)
  140.    
  141.         #so we know when it's the end of the state, and we can perform some actions before going to another state
  142.         func exit():
  143.             pass
  144.    
  145.    
  146.    
  147.     # class fallingState (7) --------------------------------------------------------------------
  148.     # we're fallin
  149.     class fallingState:
  150.         var soldat
  151.        
  152.         #init gets called when an object of this class gets created
  153.         func _init(soldat):
  154.             self.soldat = soldat
  155.             #print("Falling")
  156.             soldat.animPlayer.play("falling")
  157.    
  158.         #frame by frame update
  159.         func update(delta):
  160.             if soldat.floorRay.is_colliding():
  161.                 soldat.set_state(STATE_RUNNING) #start walking again
  162.     #       if soldat.forwardRay.is_colliding():
  163.     #           soldat.set_state(STATE_CLIMBING) #start walking again
  164.    
  165.             else: #we're falling
  166.                 soldat.motion.y = fallspeed #fall down
  167.                 soldat.motion.x +- 0                #dont move forward whilst falling
  168.                 soldat.move_and_slide(soldat.motion)
  169.                 soldat.get_node("/root/gameLevel/HUD/debugStuff/Label4").text = str(soldat.motion)
  170.    
  171.    
  172.         #so we know when it's the end of the state, and we can perform some actions before going to another state
  173.         func exit():
  174.             pass
  175.            
  176.     # class climbingState (7) --------------------------------------------------------------------
  177.     # we're climbin
  178.     class climbingState:
  179.         var soldat
  180.        
  181.         #init gets called when an object of this class gets created
  182.         func _init(soldat):
  183.             self.soldat = soldat
  184.             #print("CLIMBING")
  185.             soldat.animPlayer.play("climb")
  186.    
  187.         #frame by frame update
  188.         func update(delta):
  189.             soldat.motion.y = -fallspeed    #climb up
  190.             soldat.motion.x +- 0            #no forward whilst climbing
  191.             soldat.move_and_slide(soldat.motion)
  192.            
  193.             if !soldat.forwardRay.is_colliding():  #note the NOT
  194.                 #print("no longer climbing")
  195.                 soldat.motion.y = 0
  196.                 soldat.motion.x = walkSpeed
  197.                 soldat.set_state(STATE_RUNNING) #go back to walking
  198.            
  199.             soldat.get_node("/root/gameLevel/HUD/debugStuff/Label4").text = str(soldat.motion)
  200.    
  201.    
  202.         #so we know when it's the end of the state, and we can perform some actions before going to another state
  203.         func exit():
  204.             pass
  205.    
  206.    
  207.    
  208.     #if we leave the screen, kill ourselves
  209.     func _on_VisibilityNotifier2D_screen_exited():
  210.         #make sure we don't die just by being added to the left of the screen
  211.         if position.x >= 1:
  212.             queue_free()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement