This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monster.gd
59 lines (49 loc) · 1.83 KB
/
Monster.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
# Copyright (C) 2018 Jason Anderson
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
extends KinematicBody2D
const MAX_SPEED = 750 # Pixels per second
var speed = 0
var vel = Vector2()
var delay = 1
var waited = 0
func randomly_select_direction():
var direction = ['move_up','move_down','move_right','move_left']
var num = randi() % direction.size()
var random_choice = direction[num]
return random_choice
func _ready():
randomize()
func _physics_process(change_in_time):
var motion = Vector2()
if waited > delay:
var selection = randomly_select_direction()
if selection == "move_up":
# motion += Vector2(0, -1)
motion.y = -1
elif selection == "move_down":
# motion += Vector2(0, 1)
motion.y = 1
if selection == "move_left":
# motion += Vector2(-1, 0)
motion.x = -1
elif selection == "move_right":
# motion += Vector2(1, 0)
motion.x = 1
if motion != Vector2():
speed = MAX_SPEED
else:
speed = 0
vel = motion.normalized() * speed
move_and_slide(vel)
waited = 0
elif waited <= delay:
waited += change_in_time