Compare commits

..

19 Commits

Author SHA1 Message Date
06aa9b54c7 Attempt to start using Go Routines 2024-01-08 11:12:13 +01:00
76e745801a Bring players closer to the borders. 2023-12-15 00:21:26 +01:00
e1ce42614c Improve build script, ignore the builds/ folder. 2023-12-14 23:42:15 +01:00
5680e2a204 Update the README.md! 🥳 2023-12-11 21:54:50 +01:00
8933597780 Split game into a different module. Closes #1 2023-12-11 21:51:50 +01:00
64e20f36f2 Remove (outdated) comments 2023-12-11 20:49:07 +01:00
1542837738 Resets player position at the start of a new level. Closes #12 2023-12-11 01:04:38 +01:00
5e587bc348 Added a "screenshot". 2023-12-11 00:36:54 +01:00
ec8c3fb8a3 Checks of players are blocked by heaps of trash. Closes #11 2023-12-11 00:33:02 +01:00
d1af011acb Update README.md 2023-12-07 01:25:43 +01:00
134c5edef9 Update the readme (1/2) 2023-12-07 01:18:39 +01:00
d554bf73ef Prevent from TPing into a wall. 2023-12-06 22:30:22 +01:00
227364f79e Prevent users from TPing into trash. 2023-12-06 22:23:47 +01:00
4690e5b1f1 Enable bragging rights. 2023-12-06 20:35:31 +01:00
84a9ee5c52 Refactored how robots are handled. Closes #3 2023-12-06 20:32:08 +01:00
fa37a4740b Cleaning up, followed by start of a rewrite of the deleteRobot() function. 2023-12-06 19:26:06 +01:00
85ca0b0f86 Better method comparing Position's. 2023-12-06 19:25:16 +01:00
adefe7bd20 Disable trash debug for now. 2023-12-06 19:24:46 +01:00
2c7c17e727 Change game-logic a bit. Reduces trash. Addresses #3 and #11. 2023-12-06 19:23:47 +01:00
14 changed files with 313 additions and 189 deletions

24
.gitignore vendored
View File

@ -1,23 +1 @@
# ---> Go builds
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work

View File

@ -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. 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 ## Gameplay
* As described here: https://en.wikipedia.org/wiki/Chase_(video_game) * As described here: https://en.wikipedia.org/wiki/Chase_(video_game)
@ -12,3 +39,13 @@ The tool will be written in Go exploring the following:
* Localities * Localities
* Interfaces * 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.

10
build.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
mkdir -p builds
rm builds/*
go tool dist list | grep -v android | grep -v ios | grep -v wasip1 | awk -F '/' '{printf "echo Compiling %s/%s; env CGO_ENABLED=1 GOOS=%s GOARCH=%s go build -o builds/rowboz.%s-%s\n",$1,$2,$1,$2,$1,$2 }' | sh
cd builds
for i in `ls *windows*`; do mv -v $i $i.exe; done

View File

@ -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
View File

@ -1,15 +1,15 @@
module gitea.ligthert.net/golang/rowboz 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 ( require (
github.com/gdamore/encoding v1.0.0 // indirect github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.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 github.com/rivo/uniseg v0.4.3 // indirect
golang.org/x/sys v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.5.0 // indirect golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.7.0 // indirect golang.org/x/text v0.14.0 // indirect
) )

21
go.sum
View File

@ -1,11 +1,11 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= 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/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.7.0 h1:I5LiGTQuwrysAt1KS9wg1yFfOI3arI3ucFrxtd/xqaA=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y= 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 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= 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.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 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.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= 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-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/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.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-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-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.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-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.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-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-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-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-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.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.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-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.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.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.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.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.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.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-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.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.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= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -1,5 +1,7 @@
package main package main
import "gitea.ligthert.net/golang/rowboz/robots"
func main() { func main() {
gameinit() robots.GameInit()
} }

View File

@ -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++
}

View File

@ -1,4 +1,4 @@
package main package robots
import ( import (
"fmt" "fmt"
@ -8,7 +8,7 @@ import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
) )
func gameinit() { func GameInit() {
game, err := initialize() game, err := initialize()
if err != nil { if err != nil {
log.Fatalln("Initilization error: ", err) log.Fatalln("Initilization error: ", err)
@ -19,19 +19,15 @@ func gameinit() {
// Quit the screen // Quit the screen
quit(&game) quit(&game)
fmt.Println("\nDebug time: Trash")
for i, t := range game.trash {
fmt.Println("Trash: ", i, t)
}
// Give closure // Give closure
if game.gameover == 1 { 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 { } 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("Score:", game.player.score)
fmt.Println("Level: ", game.level)
fmt.Println("Moves:", game.player.moves) fmt.Println("Moves:", game.player.moves)
fmt.Println("Teleports:", game.player.teleports) fmt.Println("Teleports:", game.player.teleports)
@ -66,8 +62,6 @@ func initialize() (Game, error) {
teleports: 0, teleports: 0,
}, },
level: 1, level: 1,
// robots: []Robot, // I need this maybe
// trash: []Position, // I will need this
gameover: 0, gameover: 0,
} }
@ -79,23 +73,24 @@ func initialize() (Game, error) {
func (game *Game) gameDirector() { func (game *Game) gameDirector() {
defer quit(game) defer quit(game)
// TODO reorder this
for game.gameover == 0 { for game.gameover == 0 {
// Clean the screen // Clean the screen
game.screen.Clear() game.screen.Clear()
// Draw robots??
if len(game.robots) == 0 {
game.level++
game.cleanTrash()
game.resetPlayer()
game.initRobots()
}
// Draw starter conditions // Draw starter conditions
game.drawBox() game.drawBox()
game.drawPlayer() game.drawPlayer()
//game.drawCoords() //game.drawCoords()
// Draw robots??
if len(game.robots) == 0 {
game.level++
game.cleanTrash()
game.initRobots()
}
game.drawRobots() game.drawRobots()
// Draw my ex-wife // Draw my ex-wife
@ -165,7 +160,6 @@ func (game *Game) keyboardProcessor() {
} }
func quit(game *Game) { func quit(game *Game) {
game.screen.Clear()
maybePanic := recover() maybePanic := recover()
game.screen.Fini() game.screen.Fini()
if maybePanic != nil { if maybePanic != nil {

View File

@ -1,12 +1,18 @@
package main package robots
func (game *Game) drawPlayer() { func (game *Game) drawPlayer() {
game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style) game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
} }
func (game *Game) movePlayer(press int) { func (game *Game) movePlayer(press int) {
if game.blockedByTrash(press) {
game.player.moves++
return
}
if press == Left { if press == Left {
if game.player.position.x != 2 { if game.player.position.x != 1 {
game.player.position.x-- game.player.position.x--
game.player.moves++ game.player.moves++
} }
@ -26,7 +32,7 @@ func (game *Game) movePlayer(press int) {
game.player.moves++ game.player.moves++
} }
} else if press == upleft { } else if press == upleft {
if game.player.position.x != 2 && game.player.position.y != 1 { if game.player.position.x != 1 && game.player.position.y != 1 {
game.player.position.x-- game.player.position.x--
game.player.position.y-- game.player.position.y--
game.player.moves++ game.player.moves++
@ -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() { 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.x = pos.x
game.player.position.y = pos.y game.player.position.y = pos.y
} }
func (game *Game) onPlayer(pos Position) bool { func (game *Game) onPlayer(pos Position) bool {
@ -70,3 +119,8 @@ func (game *Game) onPlayer(pos Position) bool {
} }
return onPlayer return onPlayer
} }
func (game *Game) resetPlayer() {
game.player.position.x = 40
game.player.position.y = 12
}

View File

@ -1,4 +1,4 @@
package main package robots
import ( import (
"runtime" "runtime"

153
robots/robots.go Normal file
View File

@ -0,0 +1,153 @@
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 {
rndPos = game.randPos()
if !game.onPlayer(rndPos) {
found = true
}
}
robot.position = rndPos
game.robots = append(game.robots, robot)
go game.Robot(robot)
}
}
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() {
var robotPos Position
// Iterate through the robots
for i, _ := range game.robots {
game.robots[i].comms <- game.player.position
robotPos = <-game.robots[i].comms
game.robots[i].position = robotPos
}
// 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
// Delete from Array
for _, r := range game.robots {
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
}

View File

@ -1,4 +1,4 @@
package main package robots
func (game *Game) drawTrash() { func (game *Game) drawTrash() {
for _, t := range game.trash { for _, t := range game.trash {
@ -9,15 +9,15 @@ func (game *Game) drawTrash() {
func (game *Game) onTrash(pos Position) bool { func (game *Game) onTrash(pos Position) bool {
var found bool var found bool
for _, t := range game.trash { for _, t := range game.trash {
if t.x == pos.x && t.y == pos.y { if t == pos {
found = true found = true
} }
} }
return found return found
} }
func (game *Game) addTrash(pos Position) { func (game *Game) addTrash(robot Robot) {
game.trash = append(game.trash, pos) game.trash = append(game.trash, robot.position)
} }
func (game *Game) cleanTrash() { func (game *Game) cleanTrash() {

View File

@ -1,6 +1,8 @@
package main package robots
import "github.com/gdamore/tcell/v2" import (
"github.com/gdamore/tcell/v2"
)
const ( const (
Up = iota Up = iota
@ -32,7 +34,13 @@ type Game struct {
keypresses chan int keypresses chan int
player Player player Player
level int level int
robots []Position robots []Robot
trash []Position trash []Position
gameover int gameover int
} }
type Robot struct {
id int
comms chan Position
position Position
}