Compare commits
	
		
			16 Commits
		
	
	
		
			f0ad3157f3
			...
			v20231212
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5680e2a204 | |||
| 8933597780 | |||
| 64e20f36f2 | |||
| 1542837738 | |||
| 5e587bc348 | |||
| ec8c3fb8a3 | |||
| d1af011acb | |||
| 134c5edef9 | |||
| d554bf73ef | |||
| 227364f79e | |||
| 4690e5b1f1 | |||
| 84a9ee5c52 | |||
| fa37a4740b | |||
| 85ca0b0f86 | |||
| adefe7bd20 | |||
| 2c7c17e727 | 
							
								
								
									
										39
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								README.md
									
									
									
									
									
								
							@@ -2,6 +2,33 @@
 | 
			
		||||
 | 
			
		||||
A clone of the video game Chase/Robots written in Go. Primary goal is to deepen my knowledge and understanding of the Go programming language.
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
╔══════════════════════════════════════════════════════════════════════════════╗
 | 
			
		||||
║                                                                              ║
 | 
			
		||||
║                                                                              ║
 | 
			
		||||
║                +                                                             ║
 | 
			
		||||
║                                                                              ║
 | 
			
		||||
║        +                                                                     ║
 | 
			
		||||
║                           +      +                                           ║
 | 
			
		||||
║           +                                                                  ║
 | 
			
		||||
║  +                                                                           ║
 | 
			
		||||
║                                                                              ║
 | 
			
		||||
║                                                                           +  ║
 | 
			
		||||
║                                                                           +  ║
 | 
			
		||||
║                         +             @                                      ║
 | 
			
		||||
║                                                                      +       ║
 | 
			
		||||
║                                                                              ║
 | 
			
		||||
║                                                                              ║
 | 
			
		||||
║                +                                                             ║
 | 
			
		||||
║     +                                                                        ║
 | 
			
		||||
║                                 +                                            ║
 | 
			
		||||
║                                                                           +  ║
 | 
			
		||||
║                    +                                                         ║
 | 
			
		||||
║                                                                         +    ║
 | 
			
		||||
║                                                                              ║
 | 
			
		||||
╚════[ Score: 0 Level: 1 ]═════════════════════════════════════════════════════╝
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Gameplay
 | 
			
		||||
* As described here: https://en.wikipedia.org/wiki/Chase_(video_game)
 | 
			
		||||
 | 
			
		||||
@@ -11,4 +38,14 @@ The tool will be written in Go exploring the following:
 | 
			
		||||
* Usage of channels
 | 
			
		||||
* Localities
 | 
			
		||||
* Interfaces
 | 
			
		||||
* Generics (if possible)
 | 
			
		||||
* Generics (if possible)
 | 
			
		||||
 | 
			
		||||
## Phases
 | 
			
		||||
### Step 1: Finish the game
 | 
			
		||||
* ~~Make the game in a functioning state~~
 | 
			
		||||
### Step 2: Split into modules
 | 
			
		||||
* ~~Split the game in separate module~~
 | 
			
		||||
### Step 3: Go routines
 | 
			
		||||
* Put robots in separate go routines and experiment with localities
 | 
			
		||||
### Step 4: Beautify the game
 | 
			
		||||
* Work on [#8](https://gitea.ligthert.net/golang/rowboz/issues/8), [#9](https://gitea.ligthert.net/golang/rowboz/issues/9), [#10](https://gitea.ligthert.net/golang/rowboz/issues/10) and make it something nice to look at.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								design.md
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								design.md
									
									
									
									
									
								
							@@ -1,26 +0,0 @@
 | 
			
		||||
# Design
 | 
			
		||||
 | 
			
		||||
Gameplay: See https://en.wikipedia.org/wiki/Chase_%28video_game%29
 | 
			
		||||
 | 
			
		||||
controls: wasd + qezc
 | 
			
		||||
Teleport key: t
 | 
			
		||||
 | 
			
		||||
`@`: player
 | 
			
		||||
`+`: drone
 | 
			
		||||
`*`: wrecks
 | 
			
		||||
 | 
			
		||||
Playfield: 80x24 - Display message when windows is too small
 | 
			
		||||
 | 
			
		||||
Interfaces:
 | 
			
		||||
Game
 | 
			
		||||
 - Screen
 | 
			
		||||
 - Gamestate
 | 
			
		||||
 - Styles?
 | 
			
		||||
 - Player
 | 
			
		||||
    - Score
 | 
			
		||||
    - Position
 | 
			
		||||
    - Moves
 | 
			
		||||
 - []Robots
 | 
			
		||||
    - Channel name
 | 
			
		||||
    - Go routine reference?
 | 
			
		||||
    
 | 
			
		||||
							
								
								
									
										12
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,15 +1,15 @@
 | 
			
		||||
module gitea.ligthert.net/golang/rowboz
 | 
			
		||||
 | 
			
		||||
go 1.21.4
 | 
			
		||||
go 1.21.5
 | 
			
		||||
 | 
			
		||||
require github.com/gdamore/tcell/v2 v2.6.0
 | 
			
		||||
require github.com/gdamore/tcell/v2 v2.7.0
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/gdamore/encoding v1.0.0 // indirect
 | 
			
		||||
	github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
 | 
			
		||||
	github.com/mattn/go-runewidth v0.0.14 // indirect
 | 
			
		||||
	github.com/mattn/go-runewidth v0.0.15 // indirect
 | 
			
		||||
	github.com/rivo/uniseg v0.4.3 // indirect
 | 
			
		||||
	golang.org/x/sys v0.5.0 // indirect
 | 
			
		||||
	golang.org/x/term v0.5.0 // indirect
 | 
			
		||||
	golang.org/x/text v0.7.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.15.0 // indirect
 | 
			
		||||
	golang.org/x/term v0.15.0 // indirect
 | 
			
		||||
	golang.org/x/text v0.14.0 // indirect
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,11 +1,11 @@
 | 
			
		||||
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
 | 
			
		||||
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
 | 
			
		||||
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
 | 
			
		||||
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
 | 
			
		||||
github.com/gdamore/tcell/v2 v2.7.0 h1:I5LiGTQuwrysAt1KS9wg1yFfOI3arI3ucFrxtd/xqaA=
 | 
			
		||||
github.com/gdamore/tcell/v2 v2.7.0/go.mod h1:hl/KtAANGBecfIPxk+FzKvThTqI84oplgbPEmVX60b8=
 | 
			
		||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
 | 
			
		||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
 | 
			
		||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
 | 
			
		||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
 | 
			
		||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
 | 
			
		||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
 | 
			
		||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
 | 
			
		||||
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
 | 
			
		||||
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
 | 
			
		||||
@@ -13,28 +13,35 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
 | 
			
		||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
 | 
			
		||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
 | 
			
		||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
 | 
			
		||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 | 
			
		||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
 | 
			
		||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 | 
			
		||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
 | 
			
		||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
			
		||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
			
		||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
			
		||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 | 
			
		||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
 | 
			
		||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
 | 
			
		||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 | 
			
		||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 | 
			
		||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 | 
			
		||||
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
 | 
			
		||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
 | 
			
		||||
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
 | 
			
		||||
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
 | 
			
		||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
			
		||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 | 
			
		||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 | 
			
		||||
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
 | 
			
		||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 | 
			
		||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
 | 
			
		||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
 | 
			
		||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 | 
			
		||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 | 
			
		||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
 | 
			
		||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
 | 
			
		||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								main.go
									
									
									
									
									
								
							@@ -1,5 +1,7 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "gitea.ligthert.net/golang/rowboz/robots"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	gameinit()
 | 
			
		||||
	robots.GameInit()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										93
									
								
								robots.go
									
									
									
									
									
								
							
							
						
						
									
										93
									
								
								robots.go
									
									
									
									
									
								
							@@ -1,93 +0,0 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
func (game *Game) initRobots() {
 | 
			
		||||
	var fabricate int = game.level * 16
 | 
			
		||||
	for i := 0; i < fabricate; i++ {
 | 
			
		||||
		var found bool
 | 
			
		||||
		var rndPos Position
 | 
			
		||||
		for !found {
 | 
			
		||||
			rndPos = game.randPos()
 | 
			
		||||
			if !game.onPlayer(rndPos) {
 | 
			
		||||
				found = true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		game.robots = append(game.robots, rndPos)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) drawRobots() {
 | 
			
		||||
	for _, r := range game.robots {
 | 
			
		||||
		game.screen.SetContent(r.x, r.y, '+', nil, game.style)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) moveRobots() {
 | 
			
		||||
	// Iterate through the robots
 | 
			
		||||
	for i, r := range game.robots {
 | 
			
		||||
 | 
			
		||||
		// Determine in which direction to go
 | 
			
		||||
		if game.player.position.x < r.x {
 | 
			
		||||
			game.robots[i].x--
 | 
			
		||||
		} else if game.player.position.x > r.x {
 | 
			
		||||
			game.robots[i].x++
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if game.player.position.y < r.y {
 | 
			
		||||
			game.robots[i].y--
 | 
			
		||||
		} else if game.player.position.y > r.y {
 | 
			
		||||
			game.robots[i].y++
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// After all the moves, lets check if any of the rowboz collided with anything
 | 
			
		||||
	for i, r := range game.robots {
 | 
			
		||||
 | 
			
		||||
		// Check if it collides with anything.
 | 
			
		||||
		if game.onPlayer(r) {
 | 
			
		||||
			game.gameover = 1
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if game.onRobot(i, r) || game.onTrash(r) {
 | 
			
		||||
 | 
			
		||||
			trash := r
 | 
			
		||||
 | 
			
		||||
			// delete robot
 | 
			
		||||
			game.deleteRobot(i)
 | 
			
		||||
 | 
			
		||||
			// create trash
 | 
			
		||||
			game.addTrash(trash)
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) onRobot(index int, pos Position) bool {
 | 
			
		||||
	var found bool
 | 
			
		||||
	for i, r := range game.robots {
 | 
			
		||||
		if index != i && pos.x == r.x && pos.y == r.y {
 | 
			
		||||
			found = true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return found
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO: improve this
 | 
			
		||||
func (game *Game) deleteRobot(robot int) {
 | 
			
		||||
 | 
			
		||||
	var rowboz []Position
 | 
			
		||||
 | 
			
		||||
	for i, r := range game.robots {
 | 
			
		||||
		if robot != i {
 | 
			
		||||
			rowboz = append(rowboz, r)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	game.robots = nil
 | 
			
		||||
	game.robots = rowboz
 | 
			
		||||
 | 
			
		||||
	game.player.score++
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package main
 | 
			
		||||
package robots
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
@@ -8,7 +8,7 @@ import (
 | 
			
		||||
	"github.com/gdamore/tcell/v2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func gameinit() {
 | 
			
		||||
func GameInit() {
 | 
			
		||||
	game, err := initialize()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln("Initilization error: ", err)
 | 
			
		||||
@@ -19,19 +19,15 @@ func gameinit() {
 | 
			
		||||
	// Quit the screen
 | 
			
		||||
	quit(&game)
 | 
			
		||||
 | 
			
		||||
	fmt.Println("\nDebug time: Trash")
 | 
			
		||||
	for i, t := range game.trash {
 | 
			
		||||
		fmt.Println("Trash: ", i, t)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Give closure
 | 
			
		||||
	if game.gameover == 1 {
 | 
			
		||||
		fmt.Println("\nYou got rekt by a robot")
 | 
			
		||||
		fmt.Println("You got rekt by a robot")
 | 
			
		||||
	} else if game.gameover == 2 {
 | 
			
		||||
		fmt.Println("\nYou took the easy way out")
 | 
			
		||||
		fmt.Println("You took the easy way out")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Score:", game.player.score)
 | 
			
		||||
	fmt.Println("Level: ", game.level)
 | 
			
		||||
	fmt.Println("Moves:", game.player.moves)
 | 
			
		||||
	fmt.Println("Teleports:", game.player.teleports)
 | 
			
		||||
 | 
			
		||||
@@ -65,9 +61,7 @@ func initialize() (Game, error) {
 | 
			
		||||
			moves:     0,
 | 
			
		||||
			teleports: 0,
 | 
			
		||||
		},
 | 
			
		||||
		level: 1,
 | 
			
		||||
		// robots:   []Robot, // I need this maybe
 | 
			
		||||
		// trash: []Position, // I will need this
 | 
			
		||||
		level:    1,
 | 
			
		||||
		gameover: 0,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -85,17 +79,19 @@ func (game *Game) gameDirector() {
 | 
			
		||||
		// Clean the screen
 | 
			
		||||
		game.screen.Clear()
 | 
			
		||||
 | 
			
		||||
		// Draw robots??
 | 
			
		||||
		if len(game.robots) == 0 {
 | 
			
		||||
			game.level++
 | 
			
		||||
			game.cleanTrash()
 | 
			
		||||
			game.resetPlayer()
 | 
			
		||||
			game.initRobots()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Draw starter conditions
 | 
			
		||||
		game.drawBox()
 | 
			
		||||
		game.drawPlayer()
 | 
			
		||||
		//game.drawCoords()
 | 
			
		||||
 | 
			
		||||
		// Draw robots??
 | 
			
		||||
		if len(game.robots) == 0 {
 | 
			
		||||
			game.level++
 | 
			
		||||
			game.cleanTrash()
 | 
			
		||||
			game.initRobots()
 | 
			
		||||
		}
 | 
			
		||||
		game.drawRobots()
 | 
			
		||||
 | 
			
		||||
		// Draw my ex-wife
 | 
			
		||||
@@ -165,7 +161,6 @@ func (game *Game) keyboardProcessor() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func quit(game *Game) {
 | 
			
		||||
	game.screen.Clear()
 | 
			
		||||
	maybePanic := recover()
 | 
			
		||||
	game.screen.Fini()
 | 
			
		||||
	if maybePanic != nil {
 | 
			
		||||
@@ -176,8 +171,8 @@ func quit(game *Game) {
 | 
			
		||||
func (game *Game) randPos() Position {
 | 
			
		||||
	var pos Position
 | 
			
		||||
 | 
			
		||||
	x := 1 + rand.Intn(77)
 | 
			
		||||
	y := 1 + rand.Intn(22)
 | 
			
		||||
	x := 2 + rand.Intn(76)
 | 
			
		||||
	y := 2 + rand.Intn(21)
 | 
			
		||||
 | 
			
		||||
	pos.x = x
 | 
			
		||||
	pos.y = y
 | 
			
		||||
@@ -1,10 +1,16 @@
 | 
			
		||||
package main
 | 
			
		||||
package robots
 | 
			
		||||
 | 
			
		||||
func (game *Game) drawPlayer() {
 | 
			
		||||
	game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) movePlayer(press int) {
 | 
			
		||||
 | 
			
		||||
	if game.blockedByTrash(press) {
 | 
			
		||||
		game.player.moves++
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if press == Left {
 | 
			
		||||
		if game.player.position.x != 2 {
 | 
			
		||||
			game.player.position.x--
 | 
			
		||||
@@ -55,10 +61,53 @@ func (game *Game) movePlayer(press int) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// blockedByTrash() Checks if player can move into a direction and if trash isn't blocking
 | 
			
		||||
func (game *Game) blockedByTrash(press int) bool {
 | 
			
		||||
	var checkPos Position
 | 
			
		||||
 | 
			
		||||
	if press == Left {
 | 
			
		||||
		checkPos.x = game.player.position.x - 1
 | 
			
		||||
		checkPos.y = game.player.position.y
 | 
			
		||||
	} else if press == Right {
 | 
			
		||||
		checkPos.x = game.player.position.x + 1
 | 
			
		||||
		checkPos.y = game.player.position.y
 | 
			
		||||
	} else if press == Up {
 | 
			
		||||
		checkPos.x = game.player.position.x
 | 
			
		||||
		checkPos.y = game.player.position.y - 1
 | 
			
		||||
	} else if press == Down {
 | 
			
		||||
		checkPos.x = game.player.position.x
 | 
			
		||||
		checkPos.y = game.player.position.y + 1
 | 
			
		||||
	} else if press == upleft {
 | 
			
		||||
		checkPos.x = game.player.position.x - 1
 | 
			
		||||
		checkPos.y = game.player.position.y - 1
 | 
			
		||||
	} else if press == upright {
 | 
			
		||||
		checkPos.x = game.player.position.x + 1
 | 
			
		||||
		checkPos.y = game.player.position.y - 1
 | 
			
		||||
	} else if press == downright {
 | 
			
		||||
		checkPos.x = game.player.position.x + 1
 | 
			
		||||
		checkPos.y = game.player.position.y + 1
 | 
			
		||||
	} else if press == downleft {
 | 
			
		||||
		checkPos.x = game.player.position.x - 1
 | 
			
		||||
		checkPos.y = game.player.position.y + 1
 | 
			
		||||
	}
 | 
			
		||||
	return game.onTrash(checkPos)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) teleport() {
 | 
			
		||||
	pos := game.randPos()
 | 
			
		||||
 | 
			
		||||
	var pos Position
 | 
			
		||||
	var safe bool = false
 | 
			
		||||
 | 
			
		||||
	for !safe {
 | 
			
		||||
		pos = game.randPos()
 | 
			
		||||
		if !game.onTrash(pos) {
 | 
			
		||||
			safe = true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	game.player.position.x = pos.x
 | 
			
		||||
	game.player.position.y = pos.y
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) onPlayer(pos Position) bool {
 | 
			
		||||
@@ -70,3 +119,8 @@ func (game *Game) onPlayer(pos Position) bool {
 | 
			
		||||
	}
 | 
			
		||||
	return onPlayer
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) resetPlayer() {
 | 
			
		||||
	game.player.position.x = 40
 | 
			
		||||
	game.player.position.y = 12
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package main
 | 
			
		||||
package robots
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"runtime"
 | 
			
		||||
							
								
								
									
										102
									
								
								robots/robots.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										102
									
								
								robots/robots.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,102 @@
 | 
			
		||||
package robots
 | 
			
		||||
 | 
			
		||||
func (game *Game) initRobots() {
 | 
			
		||||
	var fabricate int = game.level * 16
 | 
			
		||||
	for i := 0; i < fabricate; i++ {
 | 
			
		||||
		var found bool
 | 
			
		||||
		var rndPos Position
 | 
			
		||||
		for !found {
 | 
			
		||||
			rndPos = game.randPos()
 | 
			
		||||
			if !game.onPlayer(rndPos) {
 | 
			
		||||
				found = true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		rndRobot := Robot{
 | 
			
		||||
			id:       i,
 | 
			
		||||
			position: rndPos,
 | 
			
		||||
		}
 | 
			
		||||
		game.robots = append(game.robots, rndRobot)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) drawRobots() {
 | 
			
		||||
	for _, r := range game.robots {
 | 
			
		||||
		game.screen.SetContent(r.position.x, r.position.y, '+', nil, game.style)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) moveRobots() {
 | 
			
		||||
	// Iterate through the robots
 | 
			
		||||
	for i, r := 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++
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// After all the moves, lets check if any of the rowboz collided with anything
 | 
			
		||||
	for _, r := range game.robots {
 | 
			
		||||
 | 
			
		||||
		// Hit a player? Game over!
 | 
			
		||||
		if game.onPlayer(r.position) {
 | 
			
		||||
			game.gameover = 1
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Robots mingling? Trash!
 | 
			
		||||
		if game.onRobot(r) {
 | 
			
		||||
 | 
			
		||||
			// Delete robot
 | 
			
		||||
			game.deleteRobot(r)
 | 
			
		||||
 | 
			
		||||
			// Create trash
 | 
			
		||||
			game.addTrash(r)
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Hugging Trash? More trash!
 | 
			
		||||
		if game.onTrash(r.position) {
 | 
			
		||||
 | 
			
		||||
			// Delete robot
 | 
			
		||||
			game.deleteRobot(r)
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) onRobot(robot Robot) bool {
 | 
			
		||||
	var found bool = false
 | 
			
		||||
	for _, r := range game.robots {
 | 
			
		||||
		if robot.id != r.id && robot.position == r.position {
 | 
			
		||||
			found = true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return found
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) deleteRobot(robot Robot) {
 | 
			
		||||
 | 
			
		||||
	var rowboz []Robot
 | 
			
		||||
 | 
			
		||||
	for _, r := range game.robots {
 | 
			
		||||
		if robot != r {
 | 
			
		||||
			rowboz = append(rowboz, r)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	game.robots = nil
 | 
			
		||||
	game.robots = rowboz
 | 
			
		||||
 | 
			
		||||
	game.player.score++
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package main
 | 
			
		||||
package robots
 | 
			
		||||
 | 
			
		||||
func (game *Game) drawTrash() {
 | 
			
		||||
	for _, t := range game.trash {
 | 
			
		||||
@@ -9,15 +9,15 @@ func (game *Game) drawTrash() {
 | 
			
		||||
func (game *Game) onTrash(pos Position) bool {
 | 
			
		||||
	var found bool
 | 
			
		||||
	for _, t := range game.trash {
 | 
			
		||||
		if t.x == pos.x && t.y == pos.y {
 | 
			
		||||
		if t == pos {
 | 
			
		||||
			found = true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return found
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) addTrash(pos Position) {
 | 
			
		||||
	game.trash = append(game.trash, pos)
 | 
			
		||||
func (game *Game) addTrash(robot Robot) {
 | 
			
		||||
	game.trash = append(game.trash, robot.position)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (game *Game) cleanTrash() {
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
package main
 | 
			
		||||
package robots
 | 
			
		||||
 | 
			
		||||
import "github.com/gdamore/tcell/v2"
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/gdamore/tcell/v2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	Up = iota
 | 
			
		||||
@@ -32,7 +34,12 @@ type Game struct {
 | 
			
		||||
	keypresses chan int
 | 
			
		||||
	player     Player
 | 
			
		||||
	level      int
 | 
			
		||||
	robots     []Position
 | 
			
		||||
	robots     []Robot
 | 
			
		||||
	trash      []Position
 | 
			
		||||
	gameover   int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Robot struct {
 | 
			
		||||
	id       int
 | 
			
		||||
	position Position
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user