Commit c8dfbb17 authored by dsxfbz's avatar dsxfbz
Browse files

Moved actors under new ActorManager node; added actor collision detection functions

parent 406c4df6
......@@ -48,6 +48,9 @@ func _process(delta):
1: #Status update phase
print("Status update phase: "+currActor)
print("Position: "
+String(get_node("../../ActorManager/"+currActor).get_position()[0])+", "
+String(get_node("../../ActorManager/"+currActor).get_position()[1]))
#Process status effects
#Determine how much delay has passed?
phase += 1
......@@ -62,32 +65,30 @@ func _process(delta):
#Wait for player input
#If legal action, process it and change phase
if (get_node("../../Player").action_taken == true):
if(isDead(currActor)):
phase = -1
if (get_node("../../ActorManager/Player").action_taken == true):
phase = -1
get_node("../../Player").action_taken = false
get_node("../../ActorManager/Player").action_taken = false
#AI turn
else:
#Trigger the mob's decision function
#Trigger the mob's decision/action function
# Should analyze the map and return a choice of what to do
pass
# get_node("../../"+currActor).choice()
get_node("../../ActorManager/"+currActor).choice()
#Process returned action
pass
get_node("../../"+currActor).move("right") #TEMPORARY
#Check if died during actions
if (get_node("../../"+currActor).action_taken == true):
if(isDead(currActor)):
phase = -1
elif (get_node("../../ActorManager/"+currActor).action_taken == true):
phase = -1
get_node("../../"+currActor).action_taken = false
get_node("../../ActorManager/"+currActor).action_taken = false
-1: #End phase - Cleanup, prepare for next turn
print("End phase: "+currActor)
#Reset turn-specific variables
#Check if still alive. If not, kill.
if (get_node("../../"+currActor).health <= 0):
get_node("../../"+currActor).kill()
#Reset turn-specific variables
#Add delay
#Handled by next_actor()
......@@ -99,3 +100,12 @@ func _process(delta):
phase = 0
pass
func isDead(actor: String):
var exists: bool = false
for i in get_node("../OrderManager").delay.keys():
if(i == currActor):
exists = true
break
return (not exists)
extends Node
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var dir_coords = {"right": [1*16, 0],
"left": [-1*16, 0],
"up": [0, -1*16],
"down": [0, 1*16]}
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func get_actor_id_coord(x: int, y: int) -> String:
for actor in get_children():
if(actor.get_position()[0] == x && actor.get_position()[1] == y):
return actor.name
return "NULL"
func get_actor_id_dir(currActor: String, dir: String) -> String:
for actor in get_children():
if(actor.get_position()[0] == get_node("./"+currActor).get_position()[0] + dir_coords[dir][0]
&& actor.get_position()[1] == get_node("./"+currActor).get_position()[1] + dir_coords[dir][1]):
return actor.name
return "NULL"
func is_actor_coord(x: int, y: int) -> bool:
for actor in get_children():
if(actor.get_position()[0] == x && actor.get_position()[1] == y):
return true
return false
func is_actor_dir(currActor: String, dir: String) -> bool:
print(currActor+" position: ("+String(get_node("./"+currActor).get_position()[0])+", "+String(get_node("./"+currActor).get_position()[0])+")")
for actor in get_children():
print(actor.name+
" position: ("+
String(get_node("./"+actor.name).get_position()[0])+
", "+
String(get_node("./"+actor.name).get_position()[0])+
")")
if(actor.get_position()[0] == get_node("./"+currActor).get_position()[0]+dir_coords[dir][0]
&& actor.get_position()[1] == get_node("./"+currActor).get_position()[1]+dir_coords[dir][1]):
return true
return false
func kill(actor: String):
print(">" + actor + " is dying.")
get_node("../TurnManager/OrderManager").remove_actor(actor)
remove_child(get_node(actor))
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=7 format=2]
[ext_resource path="res://src/Tilemap01.tscn" type="PackedScene" id=1]
[ext_resource path="res://actors/Player.tscn" type="PackedScene" id=2]
[ext_resource path="res://OrderManager.gd" type="Script" id=3]
[ext_resource path="res://ActionManager.gd" type="Script" id=4]
[ext_resource path="res://actors/Enemy.tscn" type="PackedScene" id=5]
[ext_resource path="res://ActorManager.gd" type="Script" id=6]
[node name="Main" type="Node2D"]
position = Vector2( 224, 112 )
[node name="TileMap" parent="." instance=ExtResource( 1 )]
[node name="Player" parent="." instance=ExtResource( 2 )]
position = Vector2( 288, 144 )
[node name="TestEnemy" parent="." instance=ExtResource( 5 )]
position = Vector2( 288, 160 )
[node name="TurnManager" type="Node" parent="."]
[node name="OrderManager" type="Node" parent="TurnManager"]
......@@ -24,3 +19,12 @@ script = ExtResource( 3 )
[node name="ActionManager" type="Node" parent="TurnManager"]
script = ExtResource( 4 )
[node name="ActorManager" type="Node" parent="."]
script = ExtResource( 6 )
[node name="Player" parent="ActorManager" instance=ExtResource( 2 )]
position = Vector2( 512, 256 )
[node name="TestEnemy" parent="ActorManager" instance=ExtResource( 5 )]
position = Vector2( 512, 272 )
......@@ -36,7 +36,7 @@ var delay = {}
var tickLen = 0.05
func add_actor(ID : String) -> void:
delay[ID] = get_node("../../"+ID).speed
delay[ID] = get_node("../../ActorManager/"+ID).speed
return
func remove_actor(ID : String) -> void:
......@@ -51,22 +51,22 @@ func curr_actor() -> String:
for i in delay:
if (delay[i] <= 0):
temp[i] = get_node("../../"+i).speed ####################
temp[i] = get_node("../../ActorManager/"+i).speed ####################
#Sort for highest speed/lowest delay?
#Finding entity with lowest delay left
if temp.size() > 0:
current = temp.keys()[0]
for i in temp.keys():
#if get_node("../../"+i).get_variable("speed")
#if get_node("../../ActorManager/"+i).get_variable("speed")
if (delay[i] < delay[current]):
current = i
else:
current = delay.keys()[0]
var maxSpeed: float = get_node("../../"+current).speed
var maxSpeed: float = get_node("../../ActorManager/"+current).speed
for i in delay.keys():
if maxSpeed < get_node("../../"+i).speed:
maxSpeed = get_node("../../"+i).speed
if maxSpeed < get_node("../../ActorManager/"+i).speed:
maxSpeed = get_node("../../ActorManager/"+i).speed
#Return ID with lowest delay??? Highest speed???
return current
......@@ -99,11 +99,11 @@ func tick() -> bool:
#Processing 1 tick
if (not action_triggered):
for i in delay:
# change_delay(i, get_node("../../"+i).get_variable("speed") * tickLen)
delay[i] -= get_node("../../"+i).get_variable("speed") * tickLen
# change_delay(i, get_node("../../ActorManager/"+i).get_variable("speed") * tickLen)
delay[i] -= get_node("../../ActorManager/"+i).get_variable("speed") * tickLen
#Status update?
#get_node("../../"+i).statusUpdate()
#get_node("../../ActorManager/"+i).statusUpdate()
if (delay[i] <= 0):
action_triggered = true
......@@ -124,13 +124,13 @@ func tick_loop() -> bool:
while (not action_triggered):
for i in delay:
print(i+": "+String(delay[i]))
print(" spd: "+String(get_node("../../"+i).speed))
# change_delay(i, get_node("../../"+i).get_variable("speed") * tickLen)
delay[i] -= get_node("../../"+i).speed * tickLen
print(" spd: "+String(get_node("../../ActorManager/"+i).speed))
# change_delay(i, get_node("../../ActorManager/"+i).get_variable("speed") * tickLen)
delay[i] -= get_node("../../ActorManager/"+i).speed * tickLen
print(i+": "+String(delay[i]))
#Status update?
#get_node("../../"+i).statusUpdate()
#get_node("../../ActorManager/"+i).statusUpdate()
if (delay[i] <= 0):
action_triggered = true
......
......@@ -18,29 +18,46 @@ func _ready():
# use this if you want to only move on keypress
func _unhandled_input(event):
for dir in inputs.keys():
if event.is_action_pressed(dir) and (get_node("../TurnManager/ActionManager").currActor == "Player"):
if event.is_action_pressed(dir) and (get_node("../../TurnManager/ActionManager").currActor == "Player"):
move(dir)
func move(dir: String):
ray.cast_to = inputs[dir] * tile_size
ray.force_raycast_update()
if !ray.is_colliding():
position += inputs[dir] * tile_size
action_taken = true
if(not get_node("..").is_actor_dir(name, dir)):
position += inputs[dir] * tile_size
action_taken = true
else:
print("-Actor Collision: "+get_node("..").get_actor_id_dir(name, dir))
else:
print("-Collision")
func harm(damage: float):
health -= damage
print(">" + self.name + " took " + String(damage) + " damage.")
life_update();
# if health <= 0:
# kill()
func kill():
print(">" + self.name + " is dying.")
get_node("../TurnManager/OrderManager").remove_actor(self.name)
get_parent().remove_child(self)
func life_update():
if health <= 0:
# kill()
get_node("..").kill(name)
#func kill():
# print(">" + self.name + " is dying.")
# get_node("../../TurnManager/OrderManager").remove_actor(self.name)
# get_parent().remove_child(self)
#Get name of actor-node being collided with
# +How to determine if something collided with is an actor?
#func get_actor_name():
# for()
# if:
# pass
# else:
# return "NULL"
#Stats
var speed: float = 1 #Controls how frequently the player can act; increases the rate at which delay is gone through
......
......@@ -12,7 +12,7 @@ func _ready():
position = position.snapped(Vector2.ONE * tile_size)
position += Vector2.ONE * tile_size/2
speed = 1
speed = 2
action_taken = false
# use this if you want to only move on keypress
......@@ -21,15 +21,48 @@ func _ready():
# if event.is_action_pressed(dir):
# move(dir)
#func choice():
# if(get_node("/Node2D").position < get_node("../Player/Node2D").position):
# move("up")
# elif(get_node("/Node2D").position > get_node("../Player/Node2D").position):
# move("Down")
# if(get_node("/Node2D").position > get_node("../Player/Node2D").position):
# move("Left")
# if(get_node("/Node2D").position < get_node("../Player/Node2D").position):
# move("Right")
func choice():
var max_sight_dist = 8 * 16
var playerPos = get_node("../Player").get_position()
var currPos = self.get_position()
print(">currPos: "+String(currPos[0])+", "+String(currPos[1]))
print(">playerPos: "+String(playerPos[0])+", "+String(playerPos[1]))
var xDist = playerPos[0]-currPos[0]
var yDist = playerPos[1]-currPos[1]
# print(">xDist: "+String(xDist))
# print(">yDist: "+String(yDist))
# if("""sqrt(xDist, yDist) < max sight""" && """Nothing_blocking_vision()""")
print(pow(pow(xDist, 2) + pow(yDist, 2), 0.5))
if(pow(pow(xDist, 2) + pow(yDist, 2), 0.5) < max_sight_dist): #Make is_visible() function?
if(abs(xDist) >= abs(yDist)):
if(xDist <= 0):
if(get_node("..").is_actor_dir(name, "left")):
print("ACTOR COLLISION: "+get_node("..").get_actor_id_dir(name, "left"))
else:
move("left")
else:
if(get_node("..").is_actor_dir(name, "right")):
print("ACTOR COLLISION: "+get_node("..").get_actor_id_dir(name, "right"))
else:
move("right")
else:
if(yDist <= 0):
if(get_node("..").is_actor_dir(name, "up")):
print("ACTOR COLLISION: "+get_node("..").get_actor_id_dir(name, "up"))
else:
move("up")
else:
if(get_node("..").is_actor_dir(name, "down")):
print("ACTOR COLLISION: "+get_node("..").get_actor_id_dir(name, "down"))
else:
move("down")
action_taken = true
func move(dir: String):
ray.cast_to = inputs[dir] * tile_size
......@@ -42,18 +75,23 @@ func move(dir: String):
else:
print(">TestEnemy ran into wall; inflicting 6 damage")
harm(6)
action_taken = true
func harm(damage: float):
health -= damage
print(">" + self.name + " took " + String(damage) + " damage.")
life_update()
# if health <= 0:
# kill()
func kill():
print(">" + self.name + " is dying.")
get_node("../TurnManager/OrderManager").remove_actor(self.name)
get_parent().remove_child(self)
func life_update():
if health <= 0:
# kill()
get_node("..").kill(name)
#func kill():
# print(">" + self.name + " is dying.")
# get_node("../../TurnManager/OrderManager").remove_actor(self.name)
# get_parent().remove_child(self)
#Stats
var speed: float = 1 #Controls how frequently the player can act; increases the rate at which delay is gone through
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment