Advertisement
IronBanes

Untitled

Dec 22nd, 2022
2,705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class_name Player
  2. extends KinematicBody2D
  3.  
  4. #export (int) var speed = 120
  5. #export (int) var jump_speed = -180
  6. #export (int) var gravity = 400
  7. #
  8. #export (float, 0, 1.0) var friction = 0.1
  9. #export (float, 0, 1.0) var acceleration = 0.25
  10. #
  11. #var velocity = Vector2.ZERO
  12. #
  13. #func get_input():
  14. #   var dir = 0
  15. #   if Input.is_action_pressed("move_right"):
  16. #       dir += 1
  17. #   if Input.is_action_pressed("move_left"):
  18. #       dir -= 1
  19. #   if dir != 0:
  20. #       velocity.x = lerp(velocity.x, dir * speed, acceleration)
  21. #   else:
  22. #       velocity.x = lerp(velocity.x, 0, friction)
  23. #
  24. #
  25. #func _physics_process(delta):
  26. #   get_input()
  27. #   velocity.y += gravity * delta
  28. #   velocity = move_and_slide(velocity, Vector2.UP)
  29. #   if Input.is_action_just_pressed("jump"):
  30. #       if is_on_floor():
  31. #           velocity.y = jump_speed
  32.  
  33. ############################################################################################################################################
  34. #const MAXFALLSPEED = 200
  35. #const MAXSPEED = 80
  36. #const JUMPFORCE = 375
  37.  
  38. export (int) var speed = 1200
  39. export (int) var jump_speed = -1800
  40. export (int) var gravity = 4000
  41.  
  42. #var is_on_floor = 0
  43.  
  44. #var state_machine
  45.  
  46. var attacks = ["Sword Slash","Sword Slash Down"]
  47.  
  48. var velocity = Vector2.ZERO
  49.  
  50. var sworddrawn = false
  51.  
  52.  
  53.  
  54. func _ready():
  55.     state_machine = $AnimationTree.get("parameters/playback")
  56.     $AnimationTree["parameters/conditions/Landed"] = 0
  57.  
  58. func get_input():
  59.     var current = state_machine.get_current_node()
  60.  
  61.     velocity.y += _gravity
  62.     if velocity.y > MAXFALLSPEED:
  63.         velocity.y = MAXFALLSPEED
  64.     elif velocity.y > 0:
  65.         state_machine.travel("fall 2")
  66.     else:
  67.         if sworddrawn && current == "idle":
  68.             state_machine.travel("idle-2")
  69.  
  70.     if Input.is_action_just_pressed("Light Attack"):
  71.         if (current == "idle-2" || current == "walk 2"):
  72.             state_machine.travel(attacks[randi()%2])
  73.             return
  74.         elif current == "idle":
  75.             state_machine.travel("punch")
  76.             return
  77.         elif current == "walk":
  78.             state_machine.travel("run-punch")
  79.             return
  80.  
  81.     if Input.is_action_just_pressed("Heavy Attack"):
  82.         if (current == "idle-2" || current == "walk 2"):
  83.             state_machine.travel("attack1")
  84.             return
  85.         elif current == "idle":
  86.             state_machine.travel("kick")
  87.             return
  88.  
  89.     if Input.is_action_just_pressed("Draw Sword Sheathe Sword"):
  90.  
  91.         if sworddrawn:
  92.             state_machine.travel("idle")
  93.             sworddrawn = false
  94.  
  95.         elif !sworddrawn:
  96.             state_machine.travel("idle-2")
  97.             sworddrawn = true
  98.  
  99.     if is_on_floor:
  100.         if Input.is_action_pressed("move_right"):
  101.  
  102.             if sworddrawn:
  103.                 state_machine.travel("walk 2")
  104.  
  105.             if not sworddrawn:
  106.                 state_machine.travel("walk")
  107.  
  108.             velocity.x = MAXSPEED
  109.             $Sprite.scale.x = 1
  110.  
  111.         elif Input.is_action_pressed("move_left"):
  112.  
  113.             if sworddrawn:
  114.                 state_machine.travel("walk 2")
  115.  
  116.             if not sworddrawn:
  117.                 state_machine.travel("walk")
  118.  
  119.             velocity.x = -MAXSPEED
  120.             $Sprite.scale.x = -1
  121.  
  122.         else:
  123.             velocity.x = 0
  124.             if sworddrawn:
  125.                 state_machine.travel("idle-2")
  126.             elif not sworddrawn:
  127.                 state_machine.travel("idle")
  128.  
  129.         if Input.is_action_just_pressed("jump"):
  130.             state_machine.travel("jump")
  131.             velocity.y = -JUMPFORCE
  132.  
  133.  
  134.     if Input.is_action_pressed("move_right"):
  135.         velocity.x = MAXSPEED
  136.         $Sprite.scale.x = 1
  137.  
  138.     elif Input.is_action_pressed("move_left"):
  139.         velocity.x = -MAXSPEED
  140.         $Sprite.scale.x = -1
  141.  
  142.     else:
  143.         velocity.x = 0
  144.  
  145.  
  146.     velocity = move_and_slide(velocity,vector2.UP)
  147.    
  148.  
  149. func _physics_process(delta):
  150. #   if $RayCast2D.is_colliding():
  151. #       is_on_floor = 1
  152. #       $AnimationTree["parameters/conditions/Landed"] = 1
  153. #
  154. #   if not $RayCast2D.is_colliding():
  155. #       is_on_floor = 0
  156. #       $AnimationTree["parameters/conditions/Landed"] = 0
  157.  
  158.  
  159.     var current = state_machine.get_current_node()
  160.  
  161.     velocity.y += _gravity
  162.     if velocity.y > MAXFALLSPEED:
  163.         velocity.y = MAXFALLSPEED
  164.     elif velocity.y > 0:
  165.         state_machine.travel("fall 2")
  166.     else:
  167.         if sworddrawn && current == "idle":
  168.             state_machine.travel("idle-2")
  169.  
  170.     if Input.is_action_just_pressed("Light Attack"):
  171.         if (current == "idle-2" || current == "walk 2"):
  172.             state_machine.travel(attacks[randi()%2])
  173.             return
  174.         elif current == "idle":
  175.             state_machine.travel("punch")
  176.             return
  177.         elif current == "walk":
  178.             state_machine.travel("run-punch")
  179.             return
  180.  
  181.     if Input.is_action_just_pressed("Heavy Attack"):
  182.         if (current == "idle-2" || current == "walk 2"):
  183.             state_machine.travel("attack1")
  184.             return
  185.         elif current == "idle":
  186.             state_machine.travel("kick")
  187.             return
  188.  
  189.     if Input.is_action_just_pressed("Draw Sword Sheathe Sword"):
  190.  
  191.         if sworddrawn:
  192.             state_machine.travel("idle")
  193.             sworddrawn = false
  194.  
  195.         elif !sworddrawn:
  196.             state_machine.travel("idle-2")
  197.             sworddrawn = true
  198.  
  199.     if is_on_floor:
  200.         if Input.is_action_pressed("move_right"):
  201.  
  202.             if sworddrawn:
  203.                 state_machine.travel("walk 2")
  204.  
  205.             if not sworddrawn:
  206.                 state_machine.travel("walk")
  207.  
  208.             velocity.x = MAXSPEED
  209.             $Sprite.scale.x = 1
  210.  
  211.         elif Input.is_action_pressed("move_left"):
  212.  
  213.             if sworddrawn:
  214.                 state_machine.travel("walk 2")
  215.  
  216.             if not sworddrawn:
  217.                 state_machine.travel("walk")
  218.  
  219.             velocity.x = -MAXSPEED
  220.             $Sprite.scale.x = -1
  221.  
  222.         else:
  223.             velocity.x = 0
  224.             if sworddrawn:
  225.                 state_machine.travel("idle-2")
  226.             elif not sworddrawn:
  227.                 state_machine.travel("idle")
  228.  
  229.         if Input.is_action_just_pressed("jump"):
  230.             state_machine.travel("jump")
  231.             velocity.y = -JUMPFORCE
  232.  
  233.  
  234.     if Input.is_action_pressed("move_right"):
  235.         velocity.x = MAXSPEED
  236.         $Sprite.scale.x = 1
  237.  
  238.     elif Input.is_action_pressed("move_left"):
  239.         velocity.x = -MAXSPEED
  240.         $Sprite.scale.x = -1
  241.  
  242.     else:
  243.         velocity.x = 0
  244.  
  245.  
  246.     velocity = move_and_slide(velocity,vector2.UP)
  247.  
  248. func hurt():
  249.     state_machine.travel("hurt")
  250.  
  251. func die():
  252.     state_machine.travel("die")
  253.     set_physics_process(false)
  254.  
  255.  
  256. #const GRAVITY = 100.0
  257. #const WALK_SPEED = 200
  258. #const Jump_speed = 40
  259. #
  260. #var velocity = Vector2()
  261. #var is_on_floor = 0
  262. #onready var _animation_player = $AnimationPlayer
  263. #
  264. #func _physics_process(delta):
  265. #   velocity.y += delta * GRAVITY
  266. #
  267. #   if $RayCast2D.is_colliding():
  268. #       is_on_floor = 1
  269. #
  270. #   if Input.is_action_pressed("move_left"):
  271. #       velocity.x = -WALK_SPEED
  272. #   elif Input.is_action_pressed("move_right"):
  273. #       velocity.x =  WALK_SPEED
  274. #   elif Input.is_action_just_released("jump")&& is_on_floor():
  275. #       velocity.y = -Jump_speed * delta
  276. #   else:
  277. #       velocity.x = 0
  278. #
  279. #   # We dont need to multiply velocity by delta because "move_and_slide" already takes delta time into account.
  280. #
  281. #
  282. #   # The second parameter of "move_and_slide" is the normal pointing up.
  283. #   # In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal.
  284. #   move_and_slide(velocity, Vector2(0, -1))
  285. #
  286. #func _process(_delta):#(Input.is_action_pressed("move_left") or Input.is_action_pressed("move_right"))
  287. #   if  Input.is_action_pressed("move_right") && is_on_floor():
  288. #       $Sprite.flip_h = false
  289. #       _animation_player.play("walk")
  290. #   elif Input.is_action_pressed("move_left")&& is_on_floor():
  291. #       $Sprite.flip_h = true
  292. #       _animation_player.play("walk")
  293. #   elif Input.is_action_just_released("jump")&& is_on_floor():
  294. #       _animation_player.play("jump")
  295. #   elif is_on_floor == 1:
  296. #       _animation_player.play("idle")
  297.  
  298.  
  299.  
  300. #region
  301. #func get_input():
  302. #
  303. #   var current = state_machine.get_current_node()
  304. #   velocity = Vector2.ZERO
  305. #
  306. #   if Input.is_action_just_pressed("Light Attack") && (current == "idle-2" || current == "walk 2"):
  307. #       state_machine.travel("Sword Slash")
  308. #       return
  309. #
  310. #   if Input.is_action_just_pressed("Light Attack") && current == "idle":
  311. #       state_machine.travel("punch")
  312. #       return
  313. #
  314. #   if Input.is_action_just_pressed("Light Attack") && current == "walk":
  315. #       state_machine.travel("run-punch")
  316. #       return
  317. #
  318. #   if Input.is_action_just_pressed("Heavy Attack") && (current == "idle-2" || current == "walk 2"):
  319. #       state_machine.travel("Sword Slash Down")
  320. #       return
  321. #
  322. #   if Input.is_action_just_pressed("Heavy Attack") && current == "idle":
  323. #       state_machine.travel("kick")
  324. #       return
  325. #
  326. #   if Input.is_action_just_pressed("Draw Sword Sheathe Sword"):
  327. #
  328. #       if sworddrawn:
  329. #           state_machine.travel("Sword Away")
  330. #           sworddrawn = false
  331. #           $AnimationTree["parameters/conditions/swordisdrawn"] = false
  332. #
  333. #       elif !sworddrawn:
  334. #           state_machine.travel("Sword Draw")
  335. #           sworddrawn = true
  336. #           $AnimationTree["parameters/conditions/swordisdrawn"] = true
  337. #
  338. #   print(sworddrawn)
  339. #   if Input.is_action_pressed("move_right"):
  340. #
  341. #       if current == "idle-2":
  342. #           state_machine.travel("walk 2")
  343. #
  344. #       if current == "idle":
  345. #           state_machine.travel("walk")
  346. #
  347. #       velocity.x += 1
  348. #       $Sprite.scale.x = 1
  349. #
  350. #   if Input.is_action_pressed("move_left"):
  351. #
  352. #       if current == "idle-2":
  353. #           state_machine.travel("walk 2")
  354. #
  355. #       if current == "idle":
  356. #           state_machine.travel("walk")
  357. #
  358. #       velocity.x -= 1
  359. #       $Sprite.scale.x = -1
  360. #
  361. #   if Input.is_action_just_pressed("jump"):
  362. #       state_machine.travel("jump")
  363. #       velocity.y = -Jump_speed
  364. #
  365. #       return
  366. #
  367. #   #velocity = velocity.normalized() * run_speed
  368. #
  369. #   if velocity.length() != 0 && sworddrawn:
  370. #       state_machine.travel("walk 2")
  371. #
  372. #   elif velocity.length() != 0 && not sworddrawn:
  373. #       state_machine.travel("walk")
  374. #
  375. #   if velocity.length() == 0 && sworddrawn:
  376. #       state_machine.travel("idle-2")
  377. #
  378. #   elif velocity.length() == 0 && not sworddrawn:
  379. #       state_machine.travel("idle")
  380. ##end region
  381.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement