Skip to content
This repository was archived by the owner on Sep 24, 2022. It is now read-only.

Commit ec710da

Browse files
committed
Add initial version
0 parents  commit ec710da

26 files changed

+1233
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Jetbrain IDE
2+
.idea

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Candy
2+
3+
Simple 2D multi-player online game.
4+
5+
## Prerequisites
6+
7+
- [Go v1.15.6](https://golang.org/doc/install)
8+
9+
## Getting Started
10+
11+
```bash
12+
go run main.go
13+
```
14+
15+
![](docs/preview.png)
16+
17+
## Usage
18+
19+
| Action | Function |
20+
| ------ | -------- |
21+
| Up arrow key | Move player upward |
22+
| Donw arrow key | Move player downward |
23+
| Left arrow key| Move player to the left |
24+
| Right arrow key| Move player to the right |
25+
26+
## Testing
27+
28+
```bash
29+
go test ./...
30+
```
31+
32+
## Tools
33+
34+
### Format code
35+
36+
```bash
37+
goimports -w .
38+
```
39+
40+
## Author
41+
42+
- Harry - *Initial works*
43+
44+
## Contributors
45+
46+
## License
47+
48+
This project is maintained under MIT license.

assets.sketch

256 KB
Binary file not shown.

assets/assets.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package assets
2+
3+
import (
4+
"image"
5+
_ "image/png"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
var images = []string{
11+
"map/default.png",
12+
"sprite_sheet.png",
13+
}
14+
15+
type Assets struct {
16+
imageMap map[string]image.Image
17+
}
18+
19+
func (a Assets) GetImage(imageName string) image.Image {
20+
return a.imageMap[imageName]
21+
}
22+
23+
func LoadAssets(assetRootDir string) (Assets, error) {
24+
imageMap := make(map[string]image.Image)
25+
26+
for _, imgPath := range images {
27+
file, err := os.Open(filepath.Join(assetRootDir, imgPath))
28+
if err != nil {
29+
return Assets{}, err
30+
}
31+
32+
img, _, err := image.Decode(file)
33+
if err != nil {
34+
_ = file.Close()
35+
return Assets{}, err
36+
}
37+
_ = file.Close()
38+
imageMap[imgPath] = img
39+
}
40+
41+
return Assets{imageMap: imageMap}, nil
42+
}

assets/map/default.png

40.3 KB
Loading

assets/sprite_sheet.png

157 KB
Loading

collison.sketch

236 KB
Binary file not shown.

docs/preview.png

254 KB
Loading

game/cell/cell.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cell
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Cell struct {
8+
Row int
9+
Col int
10+
}
11+
12+
func (c Cell) String() string {
13+
return fmt.Sprintf("[Row:%d,Col:%d]", c.Row, c.Col)
14+
}

game/cell/corners.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package cell
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type CornerCells struct {
8+
TopLeft Cell
9+
TopRight Cell
10+
BottomLeft Cell
11+
BottomRight Cell
12+
}
13+
14+
func (c CornerCells) String() string {
15+
return fmt.Sprintf("[TopLeft:%v,TopRight:%v,BottomLeft:%v,BottomRight:%v]", c.TopLeft, c.TopRight, c.BottomLeft, c.BottomRight)
16+
}
17+
18+
// Detailed analysis: https://docs.google.com/document/d/1FF49lQrCCNLEuqInmXRPnbl-jvDLLtqor6Q4OgGUIy4/edit
19+
func GetCornerCells(
20+
bottomLeftX int,
21+
bottomLeftY int,
22+
objectWidth int,
23+
objectHeight int,
24+
cellWidth int,
25+
cellHeight int,
26+
) CornerCells {
27+
topLeft := getTopLeftCell(bottomLeftX, bottomLeftY, objectHeight, cellWidth, cellHeight)
28+
topRight := getTopRightCell(bottomLeftX, bottomLeftY, objectWidth, objectHeight, cellWidth, cellHeight)
29+
bottomLeft := Cell{
30+
Row: bottomLeftY / cellHeight,
31+
Col: bottomLeftX / cellWidth,
32+
}
33+
bottomRight := getBottomRightCell(bottomLeftX, bottomLeftY, objectWidth, cellWidth, cellHeight)
34+
return CornerCells{
35+
TopLeft: topLeft,
36+
TopRight: topRight,
37+
BottomLeft: bottomLeft,
38+
BottomRight: bottomRight,
39+
}
40+
}
41+
42+
func getTopLeftCell(bottomLeftX int, bottomLeftY int, objectHeight int, cellWidth int, cellHeight int) Cell {
43+
topY := bottomLeftY + objectHeight
44+
row := topY / cellHeight
45+
if topY%cellHeight == 0 {
46+
row--
47+
}
48+
column := bottomLeftX / cellWidth
49+
return Cell{
50+
Row: row,
51+
Col: column,
52+
}
53+
}
54+
55+
func getTopRightCell(bottomLeftX int, bottomLeftY int, objectWidth int, objectHeight int, cellWidth int, cellHeight int) Cell {
56+
topY := bottomLeftY + objectHeight
57+
row := topY / cellHeight
58+
if topY%cellHeight == 0 {
59+
row--
60+
}
61+
rightX := bottomLeftX + objectWidth
62+
column := rightX / cellWidth
63+
if rightX%cellWidth == 0 {
64+
column--
65+
}
66+
return Cell{
67+
Row: row,
68+
Col: column,
69+
}
70+
}
71+
72+
func getBottomRightCell(bottomLeftX int, bottomLeftY int, objectWidth int, cellWidth int, cellHeight int) Cell {
73+
row := bottomLeftY / cellHeight
74+
rightX := bottomLeftX + objectWidth
75+
column := rightX / cellWidth
76+
if rightX%cellWidth == 0 {
77+
column--
78+
}
79+
return Cell{
80+
Row: row,
81+
Col: column,
82+
}
83+
}

0 commit comments

Comments
 (0)