@@ -8,15 +8,24 @@ import (
88 "github.com/faiface/pixel/imdraw"
99)
1010
11+ // Vehicle a structure which represents a vehicle in the simulation
12+ // Quite self-explanatory
1113type Vehicle struct {
14+ // current position of the vehicle
1215 CurrentPosition Coordinates
1316
14- CurrentRide int
15- OnRide bool
16- Trips []int32
17- Enabled bool
17+ // Trips an array of trips' indexes
18+ // (and not their ids)
19+ Trips []int32
20+ // index of the current trip in Trips
21+ CurrentTrip int
22+ // if he is currently assigned on a trip
23+ OnRide bool
24+ Enabled bool
1825}
1926
27+ // AddToImd adds to the imd batch the graphic point of the vehicle
28+ // We only call .draw once for all the vehicles
2029func (v * Vehicle ) AddToImd (imd * imdraw.IMDraw ) {
2130 imd .Color = config .Config .UI .VehicleDefaultColor
2231 imd .EndShape = imdraw .RoundEndShape
@@ -34,18 +43,13 @@ func (v *Vehicle) AddToImd(imd *imdraw.IMDraw) {
3443 imd .Line (config .Config .UI .VehicleSize )
3544}
3645
37- func (v * Vehicle ) SetPosition (x , y int32 ) {
38- v .CurrentPosition .X = x
39- v .CurrentPosition .Y = y
40- }
41-
4246func (v * Vehicle ) Drive (allTrips []* Trip , step int ) (score int ) {
4347 // it has done every of his trips
44- if v .CurrentRide >= len (v .Trips ) {
48+ if v .CurrentTrip >= len (v .Trips ) {
4549 return
4650 }
4751
48- tripToGoTo := allTrips [v.Trips [v.CurrentRide ]]
52+ tripToGoTo := allTrips [v.Trips [v.CurrentTrip ]]
4953
5054 if v .OnRide {
5155 tripToGoTo .StartTrip (step )
@@ -54,8 +58,8 @@ func (v *Vehicle) Drive(allTrips []*Trip, step int) (score int) {
5458 return
5559 }
5660 v .DriveOnTrip (tripToGoTo .End .X , tripToGoTo .End .Y )
57- cx , cy := v .GetPosition ()
58- if cx == tripToGoTo .End .X && cy == tripToGoTo .End .Y {
61+ currentX , currentY := v .GetPosition ()
62+ if currentX == tripToGoTo .End .X && currentY == tripToGoTo .End .Y {
5963 score += tripToGoTo .Finish (int32 (step ))
6064 v .NextTrip ()
6165 return
@@ -68,71 +72,103 @@ func (v *Vehicle) Drive(allTrips []*Trip, step int) (score int) {
6872 return
6973}
7074
71- func (v * Vehicle ) GetPosition () (int32 , int32 ) {
72- return v .CurrentPosition .X , v .CurrentPosition .Y
73- }
74-
75+ // NextTrip move forward next trip or disable the vehicle
76+ // if there isnt any trips left
7577func (v * Vehicle ) NextTrip () {
76- // move forward next trip
77- v .CurrentRide ++
78+ v .CurrentTrip ++
7879 v .OnRide = false
79- if v .CurrentRide >= len (v .Trips ) {
80- // no trip ? hide the car
80+
81+ // no trip ? hide the car
82+ if v .CurrentTrip >= len (v .Trips ) {
8183 v .Enabled = false
8284 }
8385}
8486
85- func (v * Vehicle ) DriveOnTrip (x , y int32 ) {
86- cx , cy := v .GetPosition ()
87- if cx > x {
88- cx --
89- } else if cx < x {
90- cx ++
91- } else if cy > y {
92- cy --
93- } else if cy < y {
94- cy ++
87+ // DriveOnTrip it will make the vehicle
88+ // follow the line drawn on the screen by the trip
89+ func (v * Vehicle ) DriveOnTrip (destinationX , destinationY int32 ) {
90+ currentX , currentY := v .GetPosition ()
91+ if currentX > destinationX {
92+ currentX --
93+ } else if currentX < destinationX {
94+ currentX ++
95+ } else if currentY > destinationY {
96+ currentY --
97+ } else if currentY < destinationY {
98+ currentY ++
9599 }
96- v .SetPosition (cx , cy )
100+
101+ // update vehicle position
102+ v .SetPosition (currentX , currentY )
97103}
98104
99- func (v * Vehicle ) DriveTo (x , y int32 ) {
100- cx , cy := v .GetPosition ()
101- dx := math .Abs (float64 (cx - x ))
102- dy := math .Abs (float64 (cy - y ))
105+ // DriveTo destinationX, destinationY coordinates
106+ // It will make the vehicle move in a straight line to a point
107+ func (v * Vehicle ) DriveTo (destinationX , destinationY int32 ) {
108+ // retrieve current position
109+ currentX , currentY := v .GetPosition ()
103110
104- // fmt.Printf("goto %d %d\n", x, y)
105- nx := cx
106- ny := cy
111+ // calculate the absolute difference between the positions
112+ dx := math .Abs (float64 (currentX - destinationX ))
113+ dy := math .Abs (float64 (currentY - destinationY ))
114+
115+ // newX and newY
116+ nx := currentX
117+ ny := currentY
107118 if dx < dy {
108- if cy < y {
119+ if currentY < destinationY {
109120 ny ++
110- } else if cy > y {
121+ } else if currentY > destinationY {
111122 ny --
112123 }
113124 } else if dx > dy {
114- if cx < x {
125+ if currentX < destinationX {
115126 nx ++
116- } else if cx > x {
127+ } else if currentX > destinationX {
117128 nx --
118129 }
119130 } else if dx == dy {
120- if cx < x {
131+ if currentX < destinationX {
121132 nx ++
122- } else if cx > x {
133+ } else if currentX > destinationX {
123134 nx --
124- } else if cy < y {
135+ } else if currentY < destinationY {
125136 ny ++
126- } else if cy > y {
137+ } else if currentY > destinationX {
127138 ny --
128139 }
129140 }
130- // fmt.Printf("driving to %d, %d\n", x, y)
131- // fmt.Printf("current pos [%d, %d]", v.CurrentPosition.X, v.CurrentPosition.Y)
132- // fmt.Printf("-\n")
133- // fmt.Printf("moving from (%d, %d) to (%d, %d)\n", cx, cy, nx, ny)
141+
142+ // update the vehicle position
134143 v .SetPosition (nx , ny )
135- if x == nx && y == ny {
144+
145+ // it reached his destination so we can consider him ready to start his ride
146+ if destinationX == nx && destinationY == ny {
136147 v .OnRide = true
137148 }
138149}
150+
151+ // NewVehicle a constructor for Trip
152+ // takes its trips as parameters
153+ func NewVehicle (trips []int32 ) * Vehicle {
154+ v := new (Vehicle )
155+
156+ // default values
157+ v .SetPosition (0 , 0 )
158+ v .Enabled = true
159+ v .CurrentTrip = 0
160+ v .Trips = trips
161+
162+ return v
163+ }
164+
165+ // SetPosition setter for CurrentPosition
166+ func (v * Vehicle ) SetPosition (x , y int32 ) {
167+ v .CurrentPosition .X = x
168+ v .CurrentPosition .Y = y
169+ }
170+
171+ // GetPosition getter for CurrentPosition
172+ func (v * Vehicle ) GetPosition () (int32 , int32 ) {
173+ return v .CurrentPosition .X , v .CurrentPosition .Y
174+ }
0 commit comments