-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdirectors.js
123 lines (111 loc) · 4.65 KB
/
directors.js
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
function MovementDirectionsFromUserInput(user_input, facing_left) {
let directions = []
if (user_input.a_key) {
directions.push("punch")
}
if (user_input.s_key) {
directions.push("kick")
}
if (user_input.left) {
if (facing_left == true) {
if (user_input.down) {
directions.push("forward_down")
} else if (user_input.up) {
directions.push("forward_up")
} else {
directions.push("forward")
}
} else {
directions.push("backward")
}
} else if (user_input.right) {
if (facing_left == true) {
directions.push("backward")
} else {
if (user_input.down) {
directions.push("forward_down")
} else if (user_input.up) {
directions.push("forward_up")
} else {
directions.push("forward")
}
}
} else if (user_input.down) {
// Just hitting down
directions.push("down")
} else if (user_input.up) {
// Just hitting up
directions.push("up")
}
return directions
}
function f_Directors(frame_num, world_map, user_input, actors, directors) {
let new_directors = directors.map( director => {
// TODO: Update each director's state. For now, they're really just stateless.
return {...director}
})
return new_directors
}
function f_Directions(frame_num, world_map, user_input, actors, directors) {
let all_directions = {}
// Update directors based on world state and type of director...
directors.forEach( (director) => {
let actor = actors.find( actor => { return actor.id == director.id } )
let facing_left = actor.facing_left
if (director.director_type == "user_1") {
let directions = MovementDirectionsFromUserInput(user_input, facing_left)
all_directions[director.id] = directions
} else {
// TODO: Make this more sophisticated. For now, assume NPC.
// TODO: Find nearest PC
// TODO: Handle coordinating directors as well. Use the director state...
let target_actor = actors.find( actor => { return actor.actor_type == "player" && actor.enabled } )
if (target_actor != null) {
let target = {y: target_actor.position.y}
// if target x < 160, then target is to his right
if (target_actor.position.x < 160) {
target.x = target_actor.position.x + 24
}
// else target is to his left
else {
target.x = target_actor.position.x - 24
}
// Attempt to walk to the target
let is_below = target.y > actor.position.y
let is_at_same_level = target.y == actor.position.y
let user_input = {}
let abs_x_distance = Math.abs(actor.position.x - target.x)
if (actor.position.x < target.x && abs_x_distance > 8) {
user_input.right = true
} else if (actor.position.x > target.x && abs_x_distance > 8) {
user_input.left = true
} else if ( actor.position.x < target_actor.position.x && actor.facing_left ) {
user_input.right = true
} else if ( actor.position.x > target_actor.position.x && actor.facing_right ) {
user_input.left = true
}
let abs_y_distance = Math.abs(actor.position.y - target.y)
if (actor.position.y < target.y && abs_y_distance > 8) {
user_input.down = true
} else if (actor.position.y > target.y && abs_y_distance > 8) {
user_input.up = true
}
let directions = MovementDirectionsFromUserInput(user_input, actor.facing_left)
// If close to target, then randomly attack
let x_squared = Math.pow((target_actor.position.x - actor.position.x),2)
let y_squared = Math.pow((target_actor.position.y - actor.position.y),2)
let distance = Math.sqrt(x_squared + y_squared)
if (distance <= 32.0) {
// CPU player
if (Math.random() < 0.05) {
directions.push("punch")
} else if (Math.random() < 0.05) {
directions.push("kick")
}
}
all_directions[director.id] = directions
}
}
})
return all_directions
}