monstre.gd (2823B)
1 extends Node2D 2 3 class_name Monstre 4 5 var speed = 90 6 var target_dist = 25 7 var path = null 8 var life = 50.0 9 var max_life = 50.0 10 var strength = 10.0 11 var orbe_type = 0 12 13 var respawn_chance_per_kill = .01 14 15 var dead = false 16 var attacking = false 17 18 var mage 19 var mage_head 20 var sprite 21 var lifebar 22 var attack_area 23 var path_provider 24 25 var orbe_scene 26 27 # Called when the node enters the scene tree for the first time. 28 func _ready(): 29 mage = get_node("../mage") 30 mage_head = mage.get_node("Sprite/HeadArea") 31 sprite = $Sprite 32 lifebar = $lifebar 33 attack_area = $Sprite/AttackArea 34 path_provider = get_parent() 35 36 orbe_scene = load("res://Orbe.tscn") 37 38 func right_angle(from, to): 39 return round((from.angle_to_point(to)+PI)/(PI/2))*PI/2 40 41 func _on_death(): 42 var orbe = orbe_scene.instance() 43 orbe.set_type(orbe_type) 44 orbe.position = position 45 get_node("..").add_child(orbe) 46 47 mage.kills += 1 48 49 if randf() < mage.kills * respawn_chance_per_kill: 50 var spawners = get_tree().get_nodes_in_group("spawner") 51 spawners[randi()%spawners.size()].spawn_monster() 52 53 54 queue_free() 55 56 func process_life(_delta): 57 if !dead: 58 lifebar.scale.x = life/max_life 59 if life <= 0: 60 $HitSound.play() 61 dead = true 62 sprite.play("die") 63 sprite.z_index = -2 64 lifebar.scale.x = 0 65 $lifebar_bg.scale.x = 0 66 sprite.connect("animation_finished", self, "_on_death") 67 if sprite.is_connected("animation_finished", self, "attack"): 68 sprite.disconnect("animation_finished", self, "attack") 69 70 func attack(): 71 if attack_area.overlaps_area(mage_head): 72 mage.life -= strength 73 if !mage.over: 74 mage.get_node("HitSound").play() 75 attacking = false 76 77 func move(delta): 78 var mage_pos = mage.position 79 80 sprite.rotation = right_angle(position, mage_pos) 81 82 if position.distance_to(mage_pos) > target_dist: 83 if mage.moving or path == null: 84 path = path_provider.get_simple_path( position, mage_pos) 85 path.remove(0) 86 var distance = delta*speed 87 var last_point = position 88 var old_pos = position 89 while path.size(): 90 var distance_between_points = last_point.distance_to(path[0]) 91 # The position to move to falls between two points. 92 if distance <= distance_between_points: 93 position = last_point.linear_interpolate(path[0], distance / distance_between_points) 94 break 95 # The position is past the end of the segment. 96 distance -= distance_between_points 97 last_point = path[0] 98 path.remove(0) 99 sprite.rotation = right_angle(old_pos, position) 100 sprite.play("walk") 101 else: 102 attacking = true 103 sprite.play("attack") 104 if !mage.over: 105 $AttackSound.play() 106 if !sprite.is_connected("animation_finished", self, "attack"): 107 sprite.connect("animation_finished", self, "attack") 108 109 110 # Called every frame. 'delta' is the elapsed time since the previous frame. 111 func _process(delta): 112 process_life(delta) 113 if !dead and !attacking: 114 move(delta)