001package myproject.model;
002
003/**
004 * A car remembers its position from the beginning of its road.
005 * Cars have random velocity and random movement pattern:
006 * when reaching the end of a road, the dot either resets its position
007 * to the beginning of the road, or reverses its direction.
008 */
009public class Car implements Agent {
010        Car() { } // Created only by this package
011
012        private boolean backAndForth = Math.round(Math.random())==1 ? true : false;
013        private double position = (MP.roadLength-MP.carLength) * Math.random ();
014        private double velocity = (int) Math.ceil(Math.random() * MP.maxVelocity);
015        private java.awt.Color color = new java.awt.Color((int)Math.ceil(128 + Math.random()*127),(int)Math.ceil(128 + Math.random()*127),(int)Math.ceil(Math.random()*255));
016
017        public double getPosition() {
018                return position;
019        }
020        public java.awt.Color getColor() {
021                return color;
022        }
023        public void run(double time) {
024                if (backAndForth) {
025                        if (((position + velocity) < 0) || ((position + velocity) > (MP.roadLength-MP.carLength)))
026                                velocity *= -1;
027                } else {
028                        if ((position + velocity) > (MP.roadLength-MP.carLength))
029                                position = 0;
030                }
031                position += velocity;
032        }
033}