public event EventHandler FullOfFuelEvent; // can just use EventHandler // Constructor public Ship(int shipSize, Rectangle boundsRectangle, Graphics shipCanvas, Random rGen) { this.shipSize = shipSize; this.boundsRectangle = boundsRectangle; this.shipCanvas = shipCanvas; shipColor = Color.Red; // could perhaps change this to be passed in -- leave for now int velX = rGen.Next(MAX_VELOCITY); // randomly generate our velocity int velY = rGen.Next(MAX_VELOCITY); shipVelocity = new Point(velX, velY); petrol = rGen.Next(MAX_PETROL); // randomly generate petrol // We will randomly generate the ship's starting location int x = rGen.Next(boundsRectangle.Width - shipSize); // x needs to be within the width of our bounds rectangle int y = rGen.Next(boundsRectangle.Height - shipSize); // and y needs to be within the height of it. We will // then minus shipSize so that it cannot overlap bounds // Then, we generate a point based on our two random numbers shipLocation = new Point(x, y); petrolBotState = EPetrolBotState.docked; }
private void refuel() { OnOutOfFuelEvent(shipLocation); if (petrolBotState == EPetrolBotState.refuelling) { petrolBot.botCurrentLocation = shipLocation; if (petrol < 300) { shipState = EShipState.refueling; //shipVelocity = new Point(0, 0); petrol += 10; } else // we have finished refueling { //shipVelocity = oldVelocity; shipState = EShipState.wandering; petrolBotState = EPetrolBotState.docked; OnFullOfFuelEvent(); } } else { checkIfPetrolBotHasArrived(); } }
private void checkIfPetrolBotHasArrived() { Rectangle rect = new Rectangle(shipLocation.X, shipLocation.Y, shipSize, shipSize); bool doesContain = rect.Contains(petrolBot.botCurrentLocation); if (doesContain) { petrolBotState = EPetrolBotState.refuelling; } //else //petrolBot. //if (petrolBot.botCurrentLocation) }
// Constructor public PetrolBot(Ship botsShip, Graphics botsCanvas, int xPos, Random rGen) { this.botsShip = botsShip; this.botsCanvas = botsCanvas; shipLocation = botsShip.ShipLocation; botState = EPetrolBotState.docked; xPos = xPos * 30; botStartingLocation = new Point(xPos, 520); botCurrentLocation = botStartingLocation; int r = rGen.Next(255); int g = rGen.Next(255); int b = rGen.Next(255); botColor = Color.FromArgb(r, g, b); Ship.OutOfFuelEventHandler outOfFuelHandler = new Ship.OutOfFuelEventHandler(OutOfFuelEventHandler); this.botsShip.OutOfFuelEvent += outOfFuelHandler; EventHandler fullOfFuelHandler = new EventHandler(FullOfFuelEventHandler); this.botsShip.FullOfFuelEvent += fullOfFuelHandler; }
// Constructor public Ship(int shipSize, Rectangle boundsRectangle, Graphics shipCanvas, Random rGen) { this.shipSize = shipSize; this.boundsRectangle = boundsRectangle; this.shipCanvas = shipCanvas; shipColor = Color.Red; // could perhaps change this to be passed in -- leave for now int velX = rGen.Next(MAX_VELOCITY); // randomly generate our velocity int velY = rGen.Next(MAX_VELOCITY); shipVelocity = new Point(velX,velY); petrol = rGen.Next(MAX_PETROL); // randomly generate petrol // We will randomly generate the ship's starting location int x = rGen.Next(boundsRectangle.Width - shipSize); // x needs to be within the width of our bounds rectangle int y = rGen.Next(boundsRectangle.Height - shipSize); // and y needs to be within the height of it. We will // then minus shipSize so that it cannot overlap bounds // Then, we generate a point based on our two random numbers shipLocation = new Point(x, y); petrolBotState = EPetrolBotState.docked; }
private void refuel() { OnOutOfFuelEvent(shipLocation); if (petrolBotState == EPetrolBotState.refuelling) { petrolBot.botCurrentLocation = shipLocation; if (petrol < 300) { shipState = EShipState.refueling; //shipVelocity = new Point(0, 0); petrol += 10; } else // we have finished refueling { //shipVelocity = oldVelocity; shipState = EShipState.wandering; petrolBotState = EPetrolBotState.docked; OnFullOfFuelEvent(); } } else checkIfPetrolBotHasArrived(); }