希望做一个小的开放世界,为了使得这个世界不那么单调,需要有路上的行人和路上行走的汽车🚕。so,guys,let’s go。
红绿灯
一组红绿灯由三个圆形part构成,此外我们还需要一个带有gui的方形part通过intvalue记录时间,并显示在方块上,来显示等待的时间。由于时间是相同的,于是我们可以将这两个模块分开。
为了记录通行时间,我们引入一个名为time(intvalue)的值来记录。
如果我们搭建的是一个十字路口,那么就需要给出现在可以同行的道路,假定以坐标系正值的道路为前后,那么引入一个boolvalue(straight)来记录。绿灯时为straight值为true,另一边的道路为左右,该路绿灯时为straight值为false。
对于行人来说,在黄灯时,应该行走完,现在的路程,而不开始新的横穿操作,于是我设置了第三个值wait(boolvalue),绿灯时wait为false,黄灯时为true。
对于车辆来说,逻辑简化为车辆黄灯转弯,绿灯直行,红灯停止。
代码实现如下
//light system
local system = script.Parent
local light = {
system.light1,system.light2,system.light3,system.light4}
local desk = {
system.showtime.desk1,system.showtime.desk2,system.showtime.desk3,system.showtime.desk4}
local willchange={
1,2}
local RunService = game:GetService("RunService")
local currenttime = 0
system.time.Value=0
RunService.Stepped:Connect(function(all, step)
system.time.Value = system.time.Value - step
if currenttime-system.time.Value>1 then
currenttime = currenttime-1
end
for index = 1, #desk, 1 do
desk[index].SurfaceGui.num.Text= currenttime
end
--system.desk.SurfaceGui.num.Text= currenttime
--黄灯
if system.time.Value <= 5 and system.time.Value>0 then
system.wait.Value=true
light[willchange[1]].green.Color = Color3.fromRGB(0, 0, 0)
light[willchang