did a lot of refactoring on player/room api

player position is now vector2
added id to player
added in_inventory func to player
added add_to_inventory func to player
moved 'move' logic into player
added setget to player position
removed custom struct for player position (wtf me)
renamed Room.Connections to Room.Exits
renamed Room.Items to Room.Objects
removed room_exists_with_exit function (seemed a bit over the top)
added get_room_exits
added room_exit_exists
all room api functions now take vector2 instead of 2 ints
changed how ExitsDisplay is set
changed movement commands to match new api
added comments
master
Zac 4 years ago
parent 248eec59cc
commit 835be4dff3

@ -165,6 +165,7 @@ __meta__ = {
}
[node name="ColorRect" type="ColorRect" parent="TextDisplay"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
margin_right = -156.0

@ -5,6 +5,7 @@ onready var Display = $TextDisplay/RichTextLabel
onready var PlayerHPBar = $Information/HP/HealthBar
onready var ExitsDisplay = $Information/Exits/Exits
# TODO: move roomcontroller to a global
var RoomController = preload("res://scripts/RoomController.gd").new()
var LocalPlayer = preload("res://scripts/PlayerInfo.gd").new()
@ -35,11 +36,10 @@ func _ready():
RoomController.generate(1)
# test data
LocalPlayer.pos.X = 0
LocalPlayer.pos.Y = 0
LocalPlayer.set_position(Vector2(0,0))
# call this *after* generating the rooms duh
update_display_info(LocalPlayer.pos)
update_display_info(LocalPlayer.position)
#!! make sure to call player functions every frame !!
func _process(delta):
@ -67,20 +67,18 @@ func _on_CommandEntry_entered(cmdstr):
add_system_text("Command \"" + input[0] + "\" not recognized", true)
# updates our own display when we move (this shouldn't fire when remote players move...hopefully)
# pretty sure i dont need that if check or the argument. just gotta set this function to
# only be local (on second though, i probably should keep the argument and just provide the player ID with it)
func update_display_info(pos):
if pos == LocalPlayer.pos:
if pos == LocalPlayer.position:
ExitsDisplay.text = ""
var x = LocalPlayer.pos.X
var y = LocalPlayer.pos.Y
# iterate over every direction and check if our current room has an exit in that direction
for e in Directions.values():
if RoomController.room_exists_with_exit(x, y, e):
ExitsDisplay.text += "N " if e == Directions.N else \
"S " if e == Directions.S else \
"E " if e == Directions.E else \
"W "
var exits = RoomController.get_room_exits(pos)
ExitsDisplay.text += "N " if exits & Globals.Directions.N else ''
ExitsDisplay.text += "S " if exits & Globals.Directions.S else ''
ExitsDisplay.text += "E " if exits & Globals.Directions.E else ''
ExitsDisplay.text += "W " if exits & Globals.Directions.W else ''
func add_system_text(txt, err=false):
if err:
@ -106,33 +104,25 @@ func cmd_clear():
# direction commands
##
func cmd_south():
var x = LocalPlayer.pos.X
var y = LocalPlayer.pos.Y
if RoomController.room_exists_with_exit(x - 1, y, Directions.N):
LocalPlayer.update_position(x - 1, y)
if RoomController.room_exit_exists(LocalPlayer.position, Globals.Directions.S):
LocalPlayer.move(Globals.Directions.S)
else:
add_system_text("An exit does not exist towards the south", true)
func cmd_east():
var x = LocalPlayer.pos.X
var y = LocalPlayer.pos.Y
if RoomController.room_exists_with_exit(x, y + 1, Directions.W):
LocalPlayer.update_position(x, y + 1)
if RoomController.room_exit_exists(LocalPlayer.position, Globals.Directions.E):
LocalPlayer.move(Globals.Directions.W)
else:
add_system_text("An exit does not exist towards the east", true)
func cmd_west():
var x = LocalPlayer.pos.X
var y = LocalPlayer.pos.Y
if RoomController.room_exists_with_exit(x, y - 1, Directions.E):
LocalPlayer.update_position(x, y - 1)
if RoomController.room_exit_exists(LocalPlayer.position, Globals.Directions.W):
LocalPlayer.move(Globals.Directions.E)
else:
add_system_text("An exit does not exist towards the west", true)
func cmd_north():
var x = LocalPlayer.pos.X
var y = LocalPlayer.pos.Y
if RoomController.room_exists_with_exit(x + 1, y, Directions.S):
LocalPlayer.update_position(x + 1, y)
if RoomController.room_exit_exists(LocalPlayer.position, Globals.Directions.N):
LocalPlayer.move(Globals.Directions.N)
else:
add_system_text("An exit does not exist towards the north", true)

@ -1,13 +1,10 @@
class Position:
var X = -1
var Y = -1
var low_health = preload("res://textures/red_fill.png")
var normal_health = preload("res://textures/white_fill.png")
var id = -1
var HP = 100
var Inventory = []
var pos = Position.new()
var position = Vector2() setget set_position, get_position
signal player_moved
@ -20,8 +17,28 @@ func update_player_health(HealthBar):
elif HP > 20 and HealthBar.texture_progress != normal_health:
HealthBar.texture_progress = normal_health
func update_position(x, y):
pos.X = x
pos.Y = y
func in_inventory(item):
return Inventory.has(item)
func add_to_inventory(item):
Inventory.append(item)
# moves the player in a direction
func move(direction):
if direction == Globals.Directions.S:
position.x -= 1
elif direction == Globals.Directions.N:
position.x += 1
elif direction == Globals.Directions.W:
position.y += 1
else:
position.y -= 1
emit_signal("player_moved", pos)
emit_signal("player_moved", position)
func set_position(pos):
position = pos
# returns the player's position
func get_position():
return position

@ -6,12 +6,12 @@ class Room:
}
var Description setget ,generate_desc # this way we can call a function to generate the description on the fly
var Items = [] # an array of items inside the room
var Connections = 0 # this needs to be set as a bit flag, oring together directions
var Objects = [] # an array of objects in the room. these objects can be interacted with
var Exits = 0 # this needs to be set as a bit flag, oring together directions
var CurrentStatus = Status.GREEN # every room starts off with a "green" status
func has_exit(e):
return Connections & e
return Exits & e
func generate_desc():
# this should combine what we already have in the description
@ -20,6 +20,9 @@ class Room:
var Rooms = null # a 2D array
## only needs to be called when you're the host.
## the host needs to pass the room structure to the other clients.
## JSON??
func generate(players):
randomize()
var num_rooms = (players * 3) + (randi() % 10 + 5)
@ -40,10 +43,10 @@ func generate(players):
]
]
Rooms[0][0].Connections |= 1 | 4
Rooms[0][1].Connections |= 8
Rooms[1][0].Connections |= 2 | 4
Rooms[1][1].Connections |= 8
Rooms[0][0].Exits |= 1 | 4
Rooms[0][1].Exits |= 8
Rooms[1][0].Exits |= 2 | 4
Rooms[1][1].Exits |= 8
# end test data
@ -55,5 +58,15 @@ func room_exists(x, y):
else:
return false
func room_exists_with_exit(x, y, e):
return room_exists(x, y) && Rooms[x][y].has_exit(e)
func get_room_exits(pos):
var x = pos.x
var y = pos.y
return Rooms[x][y].Exits
# returns whether or not a room has an exit in a given direction
func room_exit_exists(pos, exit):
var x = pos.x
var y = pos.y
return Rooms[x][y].has_exit(exit)
Loading…
Cancel
Save