RMVA 设置移动直线

沿直线向指定坐标移动。图形学作业……使用Bresenham直线算法计算直线并按其移动。

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 沿直线向指定位置移动

# 设置移动路线
# 脚本:line_move(x, y)


LINE_MOVE_DEBUG = false # 开启显示调试路径

class Game_Character < Game_CharacterBase
# 计算直线坐标点
def collect_points(startX, startY, endX, endY)
points = []

dx = endX - startX
dy = endY - startY

if (endX > startX && endY < startY && dy.abs > dx.abs) ||
(endX <= startX && endY <= startY) ||
(endX < startX && endY > startY && dy.abs <= dx.abs) then
tx, ty = startX, startY
startX, startY = endX, endY
endX, endY = tx, ty
end

dx = endX - startX
dy = endY - startY

stepX = dx < 0 ? -1 : 1
stepY = dy < 0 ? -1 : 1

d2x = (dx * 2).abs
d2y = (dy * 2).abs
x = startX
y = startY
points << [x, y]
if d2y > d2x
p0 = d2x * 2 - d2y # *2不影响判定正负
pt = p0
while y < endY do
if pt >= 0
x = x + stepX
pt = pt - d2y
end
y = y + stepY
pt = pt + d2x
points << [x, y]
end
else
p0 = d2y * 2 - d2x # *2不影响判定正负
pt = p0
while x < endX do
if pt >= 0
y = y + stepY
pt = pt - d2x
end
x = x + stepX
pt = pt + d2y
points << [x, y]
end
end
# 调试
if LINE_MOVE_DEBUG
s = SceneManager.scene
def s.spriteset;@spriteset;end
sp = s.spriteset
def sp.tilemap;@tilemap;end
tilemap = sp.tilemap
w,h = $game_map.width,$game_map.height
tilemap.flash_data = Table.new(w,h)
points.each{|data|
x = data[0];y = data[1]
tilemap.flash_data[x, y] = 0xf84
}
end
return points
end
def route_up
route = RPG::MoveCommand.new
route.code = ROUTE_MOVE_UP
route.parameters = []
return route
end
def route_down
route = RPG::MoveCommand.new
route.code = ROUTE_MOVE_DOWN
route.parameters = []
return route
end
def route_left
route = RPG::MoveCommand.new
route.code = ROUTE_MOVE_LEFT
route.parameters = []
return route
end
def route_right
route = RPG::MoveCommand.new
route.code = ROUTE_MOVE_RIGHT
route.parameters = []
return route
end
def route_left_up
route = RPG::MoveCommand.new
route.code = ROUTE_MOVE_UPPER_L
route.parameters = []
return route
end
def route_right_up
route = RPG::MoveCommand.new
route.code = ROUTE_MOVE_UPPER_R
route.parameters = []
return route
end
def route_left_down
route = RPG::MoveCommand.new
route.code = ROUTE_MOVE_LOWER_L
route.parameters = []
return route
end
def route_right_down
route = RPG::MoveCommand.new
route.code = ROUTE_MOVE_LOWER_R
route.parameters = []
return route
end
# 路径结束
def route_end
route = RPG::MoveCommand.new
route.code = ROUTE_END
route.parameters = []
return route
end
# 计算移动方式
def calc_route(points, index = 0)
last_point = points[index]
next_point = points[index + 1]
if index == 0
sx, sy = points[0]
px, py = self.x, self.y
if sx != px || sy != py
points.reverse!
last_point = points[index]
next_point = points[index + 1]
end
end
#~ p [last_point, next_point]
return [] unless next_point
lx, ly = last_point
nx, ny = next_point
if lx < nx && ly == ny
# 向右
return [route_right] + calc_route(points, index + 1)
elsif lx < nx && ly < ny
# 右下
return [route_right_down] + calc_route(points, index + 1)
elsif lx < nx && ly > ny
# 右上
return [route_right_up] + calc_route(points, index + 1)
elsif lx == nx && ly == ny
# 不动..
return [] + calc_route(points, index + 1)
elsif lx == nx && ly < ny
# 向下
return [route_down] + calc_route(points, index + 1)
elsif lx == nx && ly > ny
# 向上
return [route_up] + calc_route(points, index + 1)
elsif lx > nx && ly == ny
# 向左
return [route_left] + calc_route(points, index + 1)
elsif lx > nx && ly < ny
# 左下
return [route_left_down] + calc_route(points, index + 1)
elsif lx > nx && ly > ny
# 左上
return [route_left_up] + calc_route(points, index + 1)
end
p "-_-?"
return [] + calc_route(points, index + 1)
end
def line_move(ex, ey)
sx = self.x
sy = self.y
points = collect_points(sx, sy, ex, ey)
#~ p points
list = calc_route(points)
#~ p list
list += [route_end]
move_route = RPG::MoveRoute.new
move_route.list = [nil] + list # nil占位
move_route.wait = true
move_route.repeat = false
force_move_route(move_route)
end
end