tomato-dungeon

a game made in 1 day
git clone git://git.vgx.fr/tomato-dungeon
Log | Files | Refs | README

mage.gd (4685B)


      1 extends Node2D
      2 
      3 export var speed = 100.0
      4 export var max_life = 50.0
      5 export var strength = 10.0
      6 export var spell_level = 0
      7 
      8 export var max_speed = 200.0
      9 export var min_speed = 50.0
     10 export var max_max_life = 150.0
     11 export var min_max_life = 15.0
     12 export var max_strength = 100.0
     13 export var min_strength = 2.0
     14 export var max_spell_level = 10
     15 
     16 var life = max_life
     17 var life_full_size = 100.0
     18 
     19 var attack_time = 0
     20 var attack_duration = .5
     21 var attack_dist = 30
     22 
     23 
     24 var kills = 0
     25 
     26 var moving = true
     27 
     28 var over = false
     29 
     30 var sprite
     31 var attbar
     32 var lifebar
     33 var lifebar_bg
     34 var head_area
     35 var spell_scene
     36 var label
     37 
     38 	#life -= delta*5
     39 
     40 # Called when the node enters the scene tree for the first time.
     41 func _ready():
     42 	sprite = $Sprite
     43 	lifebar = $lifebar
     44 	lifebar_bg = $lifebar_bg
     45 	attbar = $attbar
     46 	head_area = $Sprite/HeadArea
     47 	
     48 	label = get_node("../../CanvasLayer/stats")
     49 	display_stats()
     50 	
     51 	spell_scene = load("res://Spell.tscn")
     52 	
     53 
     54 func process_life(_delta):
     55 	if !over and life <= 0:
     56 		over = true
     57 		game_over()
     58 	elif over:
     59 		life = 0
     60 	
     61 	lifebar.scale.x = life/life_full_size
     62 	
     63 
     64 func move(delta):
     65 	var pos = position
     66 	
     67 	#sprite.rotation = PI/2
     68 	
     69 	var mov = Vector2(0,0)
     70 	
     71 	if Input.is_action_pressed("ui_right"):
     72 		mov.x += 1
     73 		sprite.rotation = 0
     74 	if Input.is_action_pressed("ui_left"):
     75 		mov.x -= 1
     76 		#sprite.rotation += PI/2
     77 		sprite.rotation = PI
     78 	if Input.is_action_pressed("ui_up"):
     79 		mov.y -= 1
     80 		sprite.rotation = -PI/2
     81 	if Input.is_action_pressed("ui_down"):
     82 		mov.y += 1
     83 		sprite.rotation = PI/2
     84 	
     85 	if mov != Vector2(0, 0):
     86 		pos += mov.normalized()*delta*speed
     87 		pos = get_parent().get_closest_point(pos)
     88 	
     89 	if pos != position:
     90 		moving = true
     91 		position = pos
     92 		sprite.play("walk")
     93 	else:
     94 		moving = false
     95 		sprite.stop()
     96 
     97 func attack():
     98 	for monstre in get_tree().get_nodes_in_group("monstres"):
     99 		#if position.distance_to(monstre.position) < attack_dist:
    100 		if get_node("Sprite/AttackArea").overlaps_area(monstre.get_node("Sprite/HitArea")):
    101 			monstre.life -= strength
    102 
    103 func spell():
    104 	var newspell = spell_scene.instance()
    105 	newspell.position = $Sprite/SpellSpawn.global_position
    106 	var mov = Vector2(0,0)
    107 	if Input.is_action_pressed("ui_right"):
    108 		mov.x += 1
    109 	if Input.is_action_pressed("ui_left"):
    110 		mov.x -= 1
    111 	if Input.is_action_pressed("ui_up"):
    112 		mov.y -= 1
    113 	if Input.is_action_pressed("ui_down"):
    114 		mov.y += 1
    115 	
    116 	if mov == Vector2(0,0):
    117 		mov = newspell.movement.rotated(sprite.rotation)
    118 	
    119 	newspell.speed *= 0.5 + 0.3*spell_level
    120 	
    121 	newspell.movement = mov.normalized() * newspell.speed
    122 	newspell.lvl = spell_level
    123 	
    124 	get_node("..").add_child(newspell)
    125 
    126 func process_attack(delta):
    127 	if Input.is_action_pressed("ui_accept") and attack_time == 0:
    128 		attack_time = attack_duration
    129 		$AttackSound.play()
    130 	elif attack_time > 0:
    131 		attack_time -= delta
    132 		attbar.scale.x = (attack_duration - attack_time)/attack_duration
    133 		sprite.play("attack")
    134 		if attack_time < 0:
    135 			attack_time = 0
    136 			attbar.scale.x = 0
    137 			sprite.frame = 0
    138 			attack()
    139 			if spell_level > 0:
    140 				spell()
    141 
    142 func game_over():
    143 	var niveau = get_node("../..")
    144 	niveau.get_node("CanvasLayer/GameOver").visible = true
    145 	niveau.get_node("CanvasModulate").visible = true
    146 	niveau.get_node("CanvasModulate/AnimationPlayer").play("fade")
    147 	niveau.get_node("CanvasLayer/killcount").text = "Kills: " + String(kills)
    148 	niveau.get_node("CanvasLayer/killcount").visible = true
    149 	
    150 	$Music.stop()
    151 	$GameOverMusic.play()
    152 
    153 func process_orbs():
    154 	for orbe in get_tree().get_nodes_in_group("orbes"):
    155 		if orbe.overlaps_area(head_area):
    156 			match orbe.type:
    157 				orbe.TYPE_DIABLE:
    158 					strength += 5
    159 					
    160 					max_life -= 3
    161 					speed -= 2
    162 				orbe.TYPE_GOLEM:
    163 					max_life += 10
    164 					
    165 					speed -= 3
    166 					strength -= 1
    167 					
    168 				orbe.TYPE_LANCEUR:
    169 					if spell_level < max_spell_level:
    170 						spell_level += 1
    171 					
    172 					speed -= 1
    173 					strength -= 1
    174 					max_life -= 1
    175 				orbe.TYPE_SERPENT:
    176 					speed += 10
    177 					
    178 					strength -= 2
    179 					max_life -= 2
    180 			
    181 			speed = clamp(speed, min_speed, max_speed)
    182 			max_life = clamp(max_life, min_max_life, max_max_life)
    183 			strength = clamp(strength, min_strength, max_strength)
    184 			
    185 			
    186 			lifebar_bg.scale.x = max_life/life_full_size
    187 			life = min(life+10, max_life)
    188 			
    189 			display_stats()
    190 			
    191 			$OrbeSound.play()
    192 			
    193 			orbe.queue_free()
    194 
    195 func display_stats():
    196 	label.text = "CON: " + String(max_life)
    197 	label.text += "\nSTR: " + String(strength)
    198 	label.text += "\nSPD: " + String(speed)
    199 	label.text += "\nINT: " + String(spell_level)
    200 
    201 func _process(delta):
    202 	if over:
    203 		return
    204 	
    205 	process_attack(delta)
    206 	if attack_time == 0:
    207 		move(delta)
    208 	process_orbs()
    209 	process_life(delta)
    210 
    211 
    212 func _on_AnimationPlayer_animation_finished(anim_name):
    213 	if anim_name == "fade":
    214 		get_tree().change_scene("res://titre.tscn")