Advertisement
myLoveOnlyForYou

Untitled

Jun 20th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.87 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. const SPEED = 100
  4. var velocity = Vector2()
  5. const GRAVITY = 30
  6. const JUMP_FORCE = 400
  7. var eps = 300
  8. var START_ENEMY_POS = null
  9. var lose_sight_of = null
  10. var patrol_direction = false
  11. var kind_of_patrol = 0
  12. var counter_timer_looking_player = 0
  13. var counter_timer_stay = 0
  14. var stay = true
  15. var if_enemy_in_start_pos = true
  16. var suspicions = 0
  17. var speed_of_suspicions = 200 # from 0 to 100
  18. var shooting = false
  19. var enemyHeadCoordinates = Vector2()
  20. var playerHeadCoordinates = Vector2()
  21. const HALF_MAN_HEIGHT = 72
  22. var curPoint
  23. var seesPlayer = false
  24. var blockedPoints = 0
  25. const VISIBILITY_STEP = 1
  26. var suspicionsScale = ProgressBar.new()
  27. const HEIGHT_GAP = 200
  28. var ladderCoordinate = 0
  29. var ladderHeight = 0
  30. var shooting_delay = 0
  31. var shot_number = 0
  32. var hit_probability = 0
  33. var max_hit_probability = 0.95
  34. var min_hit_probability = 0.05
  35. var hint
  36. var usingLadder = false
  37. var enemy_pos = Vector2(0, 0)
  38.  
  39. # initial crap
  40. func _ready():
  41. var node_pos = position
  42. if (node_pos != null):
  43. START_ENEMY_POS = node_pos
  44.  
  45. suspicionsScale.rect_size = Vector2(100, 10)
  46. suspicionsScale.percent_visible = false
  47. add_child(suspicionsScale)
  48. createHint()
  49.  
  50. # create button that you have to press to kill enemy
  51. func createHint():
  52. hint = Label.new()
  53. hint.text = "R"
  54. hint.rect_position = Vector2(30, -$EnemyCollisionShape.shape.extents.y * GLOBAL.sceneScaleCoef * 1)
  55. hint.rect_scale = Vector2(2 * GLOBAL.sceneScaleCoef, 2 * GLOBAL.sceneScaleCoef)
  56. add_child(hint)
  57.  
  58. # moving left and right
  59. func _move(direction, new_speed):
  60. if (direction == "right") && position.x < GLOBAL.rightMoveLimit:
  61. velocity.x = new_speed * GLOBAL.sceneScaleCoef
  62. $EnemySprite.flip_h = false
  63. $EnemySprite/AnimationEnemy.play("walking")
  64. elif (direction == "left" && position.x > GLOBAL.leftMoveLimit):
  65. velocity.x = -new_speed * GLOBAL.sceneScaleCoef
  66. $EnemySprite.flip_h = true
  67. $EnemySprite/AnimationEnemy.play("walking")
  68. else:
  69. velocity.x = 0
  70. $EnemySprite/AnimationEnemy.play("standing")
  71.  
  72. # falling down
  73. func gravity():
  74. if is_on_floor():
  75. velocity.y = 0
  76. else:
  77. velocity.y += GRAVITY * GLOBAL.sceneScaleCoef
  78.  
  79. # enemy dies
  80. func die():
  81. # get_tree().reload_current_scene()
  82. pass
  83.  
  84. # enemy shoots
  85. func shot():
  86. var dist = abs(GLOBAL.playerCoordinates.x - position.x)
  87. var k = shot_number
  88. hit_probability = 20 * k / dist
  89. if (hit_probability < min_hit_probability):
  90. hit_probability = min_hit_probability
  91. if (hit_probability > max_hit_probability):
  92. hit_probability = max_hit_probability
  93. var rand_number = randf()
  94. if (rand_number < hit_probability):
  95. print("hit ", hit_probability, " " , rand_number)
  96. # get_node("../Enemy").queue_free()
  97. die()
  98. else:
  99. print("miss ", hit_probability, " " , rand_number)
  100. pass
  101.  
  102. # player kills the enemy
  103. func kill_the_enemy():
  104. var enemy_dir = $EnemySprite.flip_h
  105. var player_dir = get_node("../Player/PlayerSprite").flip_h
  106.  
  107. if (abs(GLOBAL.playerCoordinates.x - position.x) < 100 &&\
  108. abs(GLOBAL.playerCoordinates.y - position.y) < HEIGHT_GAP && player_dir == enemy_dir) &&\
  109. !GLOBAL.playerIsHidden:
  110. get_child(5).visible = true
  111. if Input.is_key_pressed(KEY_R):
  112. queue_free()
  113. else:
  114. get_child(5).visible = false
  115.  
  116. # patrolling a territory around a supicious place
  117. func _patrol(pos, new_speed):
  118. var bump = false
  119.  
  120. if (position.x == enemy_pos.x):
  121. bump = true
  122.  
  123. enemy_pos = position
  124.  
  125. if (enemy_pos.x > pos.x - eps && patrol_direction == false &&\
  126. enemy_pos.x > GLOBAL.leftMoveLimit && !bump):
  127. _move("left", new_speed)
  128. elif enemy_pos.x < pos.x - eps || enemy_pos.x < GLOBAL.leftMoveLimit || bump:
  129. patrol_direction = true
  130. _move("right", new_speed)
  131.  
  132. if(enemy_pos.x < pos.x + eps && patrol_direction == true &&\
  133. enemy_pos.x < GLOBAL.rightMoveLimit && !bump):
  134. _move("right", new_speed)
  135. elif enemy_pos.x > pos.x + eps || enemy_pos.x > GLOBAL.rightMoveLimit || bump:
  136. patrol_direction = false
  137. _move("left", new_speed)
  138.  
  139. # checking if our player is visible to the enemy
  140. func lookForPlayer():
  141. if !seesPlayer:
  142. if !GLOBAL.playerIsHidden:
  143. enemyHeadCoordinates.x = position.x
  144. enemyHeadCoordinates.y = position.y - HALF_MAN_HEIGHT
  145. playerHeadCoordinates.x = GLOBAL.playerCoordinates.x
  146. playerHeadCoordinates.y = GLOBAL.playerCoordinates.y - HALF_MAN_HEIGHT
  147.  
  148. var k
  149. var b
  150.  
  151. k = (enemyHeadCoordinates.y - playerHeadCoordinates.y) /\
  152. (enemyHeadCoordinates.x - playerHeadCoordinates.x)
  153.  
  154. b = (playerHeadCoordinates.x * enemyHeadCoordinates.y -\
  155. enemyHeadCoordinates.x * playerHeadCoordinates.y) /\
  156. (playerHeadCoordinates.x - enemyHeadCoordinates.x)
  157.  
  158. var beg
  159. var end
  160.  
  161. if playerHeadCoordinates.x < enemyHeadCoordinates.x:
  162. beg = playerHeadCoordinates.x
  163. end = enemyHeadCoordinates.x
  164. else:
  165. beg = enemyHeadCoordinates.x
  166. end = playerHeadCoordinates.x
  167.  
  168. blockedPoints = 0
  169.  
  170. while beg < end:
  171. curPoint = Vector2(beg, k * beg + b)
  172.  
  173. for rect in GLOBAL.obstacleRects:
  174. if rect.has_point(curPoint):
  175. blockedPoints += 1
  176. break
  177.  
  178. beg += VISIBILITY_STEP
  179.  
  180. if blockedPoints == 0:
  181. seesPlayer = true
  182. else:
  183. seesPlayer = false
  184. else:
  185. seesPlayer = false
  186.  
  187. # FOLLOW THE DAMN PLAYER, ENEMY
  188. func playerVisibilityCheck():
  189. if (suspicions > 0):
  190. suspicions -= 0.01
  191. else:
  192. suspicions = 0
  193.  
  194. var shooting_radius = $body/CollisionShape2D.shape.radius
  195. var player_pos = GLOBAL.playerCoordinates
  196. var enemy_pos = position
  197.  
  198. var vision_sizes = $VisionShape.get_shape().extents
  199.  
  200. # true - left, false - right
  201. var direction = $EnemySprite.flip_h
  202.  
  203. if (((player_pos.x < enemy_pos.x && direction == true) ||\
  204. (player_pos.x > enemy_pos.x && direction == false)) &&\
  205. abs(player_pos.x - enemy_pos.x) < vision_sizes.x / 2 &&\
  206. abs(player_pos.y - enemy_pos.y) < vision_sizes.y / 2) &&\
  207. seesPlayer && !GLOBAL.playerIsHidden:
  208. if (suspicions < 100):
  209. suspicions += speed_of_suspicions / abs(player_pos.x - enemy_pos.x)
  210. elif (suspicions > 100):
  211. suspicions = 100
  212.  
  213. # if (abs(player_pos.x - enemy_pos.x) < shooting_radius):
  214. if_enemy_in_start_pos = false
  215. stay = true
  216. counter_timer_stay = 0
  217. counter_timer_looking_player = 0
  218. kind_of_patrol = 1
  219. lose_sight_of = enemy_pos
  220.  
  221. if (suspicions > 40 && suspicions < 90):
  222. shooting = false
  223. shot_number = 0
  224. elif (suspicions > 90):
  225. if (abs(player_pos.x - enemy_pos.x) < shooting_radius):
  226. shooting = true
  227. if (shooting_delay > 30):
  228. if (shot_number == 0):
  229. # first shoot
  230. # anim dostatt' stvol ept
  231. shot_number += 1
  232. shot()
  233. else:
  234. #other shoots
  235. shot_number += 1
  236. shot()
  237. pass
  238. shooting_delay = 0
  239. pass
  240. else:
  241. shooting_delay += 1
  242. else:
  243. shooting = false
  244. shot_number = 0
  245.  
  246. if abs(GLOBAL.playerCoordinates.y - position.y) < HEIGHT_GAP:
  247. if (player_pos.x < enemy_pos.x):
  248. _move("left", SPEED + 50)
  249. elif (player_pos.x > enemy_pos.x):
  250. _move("right", SPEED + 50)
  251. else:
  252. if (ladderCoordinate < enemy_pos.x):
  253. _move("left", SPEED + 50)
  254. elif (ladderCoordinate > enemy_pos.x):
  255. _move("right", SPEED + 50)
  256. else:
  257. # a player is on another level
  258. if abs(GLOBAL.playerCoordinates.y - position.y) > HEIGHT_GAP && suspicions > 40:
  259. if (abs(position.x - ladderCoordinate) > 10):
  260. if (ladderCoordinate < enemy_pos.x):
  261. _move("left", SPEED + 50)
  262. elif (ladderCoordinate > enemy_pos.x):
  263. _move("right", SPEED + 50)
  264. else:
  265. usingLadder = true
  266. # a player is on the same level
  267. else:
  268. if abs(position.x - ladderCoordinate) < 200:
  269. velocity.y = 0
  270.  
  271. # start position patrol
  272. if (if_enemy_in_start_pos == true):
  273. _patrol(START_ENEMY_POS, SPEED)
  274. else:
  275. # enemy stopped when he lost player
  276. if (stay == true && counter_timer_stay < 200):
  277. _move("stay", 0)
  278. counter_timer_stay += 1
  279. elif (stay == true):
  280. stay = false
  281. else:
  282. # enemy patrols (looking for a player)
  283. if (kind_of_patrol == 1 && lose_sight_of != null && counter_timer_looking_player < 1000):
  284. _patrol(lose_sight_of, SPEED)
  285. counter_timer_looking_player += 1
  286. # enemy goes to the start position
  287. # OK F*CK GO BACK sad :(
  288. elif (enemy_pos == START_ENEMY_POS):
  289. # STAY HERE MOTHERF*CKER
  290. _move("stay", SPEED)
  291. if_enemy_in_start_pos = true
  292. elif (enemy_pos.x < START_ENEMY_POS.x):
  293. _move("right", SPEED)
  294. else:
  295. _move("left", SPEED)
  296.  
  297. # a scale over an enemy's head
  298. func visualizeSuspicions():
  299. if $EnemySprite.flip_h:
  300. suspicionsScale.rect_position = Vector2(-35, -$EnemySprite.texture.get_size().y *\
  301. GLOBAL.sceneScaleCoef * 0.12)
  302. else:
  303. suspicionsScale.rect_position = Vector2(-60, -$EnemySprite.texture.get_size().y *\
  304. GLOBAL.sceneScaleCoef * 0.12)
  305.  
  306. suspicionsScale.value = suspicions
  307.  
  308. # remembering the ladder that a player used
  309. func checkForLadderUsing():
  310. if GLOBAL.ableToGoUp && abs(GLOBAL.playerCoordinates.y - position.y) < HEIGHT_GAP:
  311. ladderCoordinate = GLOBAL.playerCoordinates.x
  312.  
  313. func lift():
  314. var gap = GLOBAL.ladderSize * GLOBAL.sceneScaleCoef * 280
  315. $EnemySprite/AnimationEnemy.play("usingLadder")
  316.  
  317. print(abs(position.y - GLOBAL.ladderCoordinates.y), " ", gap)
  318.  
  319. velocity.x = 0
  320.  
  321. if GLOBAL.playerCoordinates.y < position.y:
  322. velocity.y = -SPEED * GLOBAL.sceneScaleCoef
  323. else:
  324. velocity.y = SPEED * GLOBAL.sceneScaleCoef
  325. # velocity.y = SPEED
  326.  
  327. if abs(position.y - GLOBAL.ladderCoordinates.y) > gap:
  328. usingLadder = false
  329. $EnemySprite/AnimationEnemy.play("standing")
  330.  
  331. # main func
  332. func _physics_process(delta):
  333. if !usingLadder:
  334. gravity()
  335.  
  336. if (GLOBAL.playerCoordinates.x < position.x && $EnemySprite.flip_h) ||\
  337. (GLOBAL.playerCoordinates.x > position.x && !$EnemySprite.flip_h):
  338. lookForPlayer()
  339. else:
  340. seesPlayer = false
  341.  
  342. playerVisibilityCheck()
  343. visualizeSuspicions()
  344. checkForLadderUsing()
  345.  
  346. kill_the_enemy()
  347. else:
  348. if (abs(GLOBAL.playerCoordinates.y - position.y) > 30):
  349. lift()
  350. else:
  351. if (GLOBAL.playerCoordinates.x < position.x):
  352. _move("left", SPEED * GLOBAL.sceneScaleCoef)
  353. else:
  354. _move("right", SPEED * GLOBAL.sceneScaleCoef)
  355.  
  356. velocity = move_and_slide(velocity, Vector2(0, -1))
  357.  
  358. func save():
  359. var save_dict = {
  360. "filename" : get_filename(),
  361. "parent" : get_parent().get_path(),
  362. "pos_x" : position.x,
  363. "pos_y" : position.y,
  364. # "attack" : attack,
  365. # "defense" : defense,
  366. # "current_health" : current_health,
  367. # "max_health" : max_health,
  368. # "damage" : damage,
  369. # "regen" : regen,
  370. # "experience" : experience,
  371. # "tnl" : tnl,
  372. # "level" : level,
  373. # "attack_growth" : attack_growth,
  374. # "defense_growth" : defense_growth,
  375. # "health_growth" : health_growth,
  376. # "is_alive" : is_alive,
  377. # "last_attack" : last_attack
  378. }
  379. return save_dict
  380.  
  381. func load_from_dict(save_dict):
  382. position.y = save_dict['pos_y']
  383. position.x = save_dict['pos_x']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement