Attempt to start using Go Routines
This commit is contained in:
		| @@ -65,7 +65,7 @@ func initialize() (Game, error) { | ||||
| 		gameover: 0, | ||||
| 	} | ||||
|  | ||||
| 	// game.initRobots() | ||||
| 	game.initRobots() | ||||
|  | ||||
| 	return game, err | ||||
| } | ||||
| @@ -73,19 +73,18 @@ func initialize() (Game, error) { | ||||
| func (game *Game) gameDirector() { | ||||
| 	defer quit(game) | ||||
|  | ||||
| 	// TODO reorder this | ||||
| 	for game.gameover == 0 { | ||||
|  | ||||
| 		// Clean the screen | ||||
| 		game.screen.Clear() | ||||
|  | ||||
| 		// Draw robots?? | ||||
| 		// if len(game.robots) == 0 { | ||||
| 		// 	game.level++ | ||||
| 		// 	game.cleanTrash() | ||||
| 		// 	game.resetPlayer() | ||||
| 		// 	game.initRobots() | ||||
| 		// } | ||||
| 		if len(game.robots) == 0 { | ||||
| 			game.level++ | ||||
| 			game.cleanTrash() | ||||
| 			game.resetPlayer() | ||||
| 			game.initRobots() | ||||
| 		} | ||||
|  | ||||
| 		// Draw starter conditions | ||||
| 		game.drawBox() | ||||
|   | ||||
| @@ -3,6 +3,12 @@ package robots | ||||
| func (game *Game) initRobots() { | ||||
| 	var fabricate int = game.level * 16 | ||||
| 	for i := 0; i < fabricate; i++ { | ||||
|  | ||||
| 		var robot Robot | ||||
|  | ||||
| 		robot.id = i | ||||
| 		robot.comms = make(chan Position) | ||||
|  | ||||
| 		var found bool | ||||
| 		var rndPos Position | ||||
| 		for !found { | ||||
| @@ -11,11 +17,10 @@ func (game *Game) initRobots() { | ||||
| 				found = true | ||||
| 			} | ||||
| 		} | ||||
| 		rndRobot := Robot{ | ||||
| 			id:       i, | ||||
| 			position: rndPos, | ||||
| 		} | ||||
| 		game.robots = append(game.robots, rndRobot) | ||||
|  | ||||
| 		robot.position = rndPos | ||||
| 		game.robots = append(game.robots, robot) | ||||
| 		go game.Robot(robot) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -26,21 +31,15 @@ func (game *Game) drawRobots() { | ||||
| } | ||||
|  | ||||
| func (game *Game) moveRobots() { | ||||
|  | ||||
| 	var robotPos Position | ||||
|  | ||||
| 	// Iterate through the robots | ||||
| 	for i, r := range game.robots { | ||||
| 	for i, _ := range game.robots { | ||||
|  | ||||
| 		// Determine in which direction to go | ||||
| 		if game.player.position.x < r.position.x { | ||||
| 			game.robots[i].position.x-- | ||||
| 		} else if game.player.position.x > r.position.x { | ||||
| 			game.robots[i].position.x++ | ||||
| 		} | ||||
|  | ||||
| 		if game.player.position.y < r.position.y { | ||||
| 			game.robots[i].position.y-- | ||||
| 		} else if game.player.position.y > r.position.y { | ||||
| 			game.robots[i].position.y++ | ||||
| 		} | ||||
| 		game.robots[i].comms <- game.player.position | ||||
| 		robotPos = <-game.robots[i].comms | ||||
| 		game.robots[i].position = robotPos | ||||
|  | ||||
| 	} | ||||
|  | ||||
| @@ -89,14 +88,66 @@ func (game *Game) deleteRobot(robot Robot) { | ||||
|  | ||||
| 	var rowboz []Robot | ||||
|  | ||||
| 	// Delete from Array | ||||
| 	for _, r := range game.robots { | ||||
| 		if robot != r { | ||||
| 		if robot.id != r.id { | ||||
| 			rowboz = append(rowboz, r) | ||||
| 		} | ||||
| 	} | ||||
| 	game.robots = nil | ||||
| 	game.robots = rowboz | ||||
|  | ||||
| 	// Close the channel | ||||
| 	close(robot.comms) | ||||
|  | ||||
| 	game.player.score++ | ||||
|  | ||||
| } | ||||
|  | ||||
| func (game *Game) Robot(robot Robot) { | ||||
| 	var player Position | ||||
| 	var robotPos Position = robot.position | ||||
| 	var emptyPos Position | ||||
|  | ||||
| 	for { | ||||
| 		// Wait for input from the channel | ||||
| 		player = <-robot.comms | ||||
|  | ||||
| 		if player == emptyPos { | ||||
| 			close(robot.comms) | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 		//fmt.Println(player, robotPos) | ||||
|  | ||||
| 		// Determine direction to move in | ||||
| 		robotPos = game.moveRobot(player, robot.position) | ||||
| 		robot.position = robotPos | ||||
|  | ||||
| 		// Return new position of robot | ||||
| 		robot.comms <- robotPos | ||||
| 	} | ||||
|  | ||||
| } | ||||
|  | ||||
| func (game *Game) moveRobot(player Position, robot Position) Position { | ||||
|  | ||||
| 	var newPosition Position | ||||
|  | ||||
| 	// Determine in which direction to go | ||||
| 	if player.x < robot.x { | ||||
| 		newPosition.x = robot.x - 1 | ||||
| 	} else if player.x > robot.x { | ||||
| 		newPosition.x = robot.x + 1 | ||||
| 	} | ||||
|  | ||||
| 	if player.y < robot.y { | ||||
| 		newPosition.y = robot.y - 1 | ||||
| 	} else if player.y > robot.y { | ||||
| 		newPosition.y = robot.y + 1 | ||||
| 	} | ||||
|  | ||||
| 	//fmt.Println(player, robot, newPosition) | ||||
|  | ||||
| 	return newPosition | ||||
| } | ||||
|   | ||||
| @@ -41,5 +41,6 @@ type Game struct { | ||||
|  | ||||
| type Robot struct { | ||||
| 	id       int | ||||
| 	comms    chan Position | ||||
| 	position Position | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user