-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.gd
132 lines (109 loc) · 3.38 KB
/
Player.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
extends KinematicBody2D
var moveSpeed : int = 250
var vel = Vector2()
var facingDir = Vector2()
# onready var rayCast = $RayCast2D
onready var anim = $PlayerSprite
# TODO: move this to its own object/script?
var carrying_books = []
var GameManager
var BookManager
var InventoryManager
var object_type = "Player"
# Called when the node enters the scene tree for the first time.
func _ready():
GameManager = get_parent()
BookManager = get_parent().get_node("BookManager")
InventoryManager = get_parent().get_node("InventoryManager")
func set_description(text):
get_parent().get_node("DescriptionNode/CurrentDescription").text = text
func interact_with_nearby_objects():
for body in $PickupArea.get_overlapping_bodies():
if ("object_type" in body && body.object_type == "Bookcase"):
GameManager.view_bookcase(body)
return
elif ("object_type" in body && body.object_type == "Book"):
InventoryManager.add_book_to_inventory(body.attributes["id"])
body.free()
return
elif ("object_type" in body && body.object_type == "NPC"):
body.talk()
return
elif ("object_type" in body && body.object_type == "desk"):
set_description(body.get_description())
func view_nearby_objects():
for body in $PickupArea.get_overlapping_bodies():
if ("object_type" in body && body.object_type == "Bookcase"):
set_description(body.description)
return
elif ("object_type" in body && body.object_type == "Book"):
set_description(body.attributes["description"])
return
elif ("object_type" in body && body.object_type == "desk"):
set_description(body.get_description())
return
else:
set_description("")
func _input(event)->void:
if event.is_action_pressed("ui_accept"):
if GameManager.is_player_focus():
interact_with_nearby_objects()
get_tree().set_input_as_handled()
func _physics_process (delta):
vel = Vector2()
view_nearby_objects()
# inputs
if (GameManager.is_player_focus()):
move_player()
func move_player():
if Input.is_action_pressed("ui_up"):
$PickupArea.position.x = 0
$PickupArea.position.y = -4
vel.y -= 1
facingDir = Vector2(0, -1)
if Input.is_action_pressed("ui_down"):
$PickupArea.position.x = 0
$PickupArea.position.y = 4
vel.y += 1
facingDir = Vector2(0, 1)
if Input.is_action_pressed("ui_left"):
$PickupArea.position.x = -8
$PickupArea.position.y = 0
vel.x -= 1
facingDir = Vector2(-1, 0)
if Input.is_action_pressed("ui_right"):
$PickupArea.position.x = 8
$PickupArea.position.y = 0
vel.x += 1
facingDir = Vector2(1, 0)
# normalize the velocity to prevent faster diagonal movement
vel = vel.normalized()
# move the player
move_and_slide(vel * moveSpeed, Vector2.ZERO)
# for i in get_slide_count():
# var collision = get_slide_collision(i)
# print("I collided with ", collision.collider.name)
manage_animations()
func manage_animations ():
if vel.x > 0:
play_animation("east")
elif vel.x < 0:
play_animation("west")
elif vel.y < 0:
play_animation("north")
elif vel.y > 0:
play_animation("south")
elif facingDir.x == 1:
play_animation("idle_east")
elif facingDir.x == -1:
play_animation("idle_west")
elif facingDir.y == -1:
play_animation("idle_north")
elif facingDir.y == 1:
play_animation("idle_south")
func play_animation (anim_name):
if anim.animation != anim_name:
anim.play(anim_name)
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass