Skip to content

Commit

Permalink
Add project files
Browse files Browse the repository at this point in the history
  • Loading branch information
vytautashi committed Dec 1, 2022
0 parents commit 436fb21
Show file tree
Hide file tree
Showing 33 changed files with 1,533 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 vytautashi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# go-tank
<div align="center">
<img src="logo.png"/>
</div>

---
Simple multiplayer 'real-time' online game that uses _WebSockets_ and is played via browser. Also known as 'io game' type. Created with [Golang](https://go.dev/) (backend) and [JavaScript/Phaser 3](https://phaser.io/) (frontend).

[more info](#more-info)

## Getting Started
### Prerequisites
Things you need to install.
- [Golang](https://go.dev/dl/) _(tested on 1.19.3)_
- Browser that support JavaScript

### Installation and Building
1. Clone repository using command `git clone https://github.com/vytautashi/go-tank.git`
2. Run command `go build` within `/game-server` directory
3. Run command `go build` within `/game-client` directory

### Running Game
1. Run game-server executable in `/game-server` directory
2. Run game-client executable in `/game-client` directory
3. Access game using browser via url http://localhost:8081 _(open on multiple tabs to spawn multiple players)_

## More Info
This repository demonstrates basic created 'io game' using _Golang_ and _Phaser_. Player can move and fire bullets which collides with other players. Its not blooted with a lot of functionality in order to make it simple and easy to understand.

It does have:
* Movable game characters (tanks)
* Bullets collision with players
* Communication between server and clients via _WebSockects_
* Backend uses concurrency (Golang go routines, communication via channels, non-blocking)

It does not have:
* IP restrictor _(same person from same computer can have multiple active players in game)_ **(backend)**
* Spam restrictor _(do not limit message count received from clients/players via WebSockects)_ **(backend)**
* Client-side prediction (https://en.wikipedia.org/wiki/Client-side_prediction) **(frontend)**
* Code is not performance friendly _(highly optimised code would be hard to read/understand)_
* Now bullet objects are recreated everytime in frontend when it gets message about bullets positions from `game-server`, better would be to track bullets using id and move them accordingly instead of recreating **or/and** instead of deleting hide somewhere of the game screen and when command received, just set to correct position. **(frontend)**
* Might be good to send only once Bullet data (position, speed, lifetime, moving direction) and let frontend calculate every next frame bullets positions, only send additional message to frontend if bullet collided with player and exploded so to remove bullet ealier from frontend. **(backend+frontend)**
* And other places.

## Screenshots
#### Screenshot 1
![screenshot 1](screenshot1.png)
6 changes: 6 additions & 0 deletions game-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# game-client
Frontend also contains:
* js/phaser.min.js (3.55.2)

## Installation and Building
* To build, run command `go build` within this directory
Binary file added game-client/assets/tank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions game-client/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/vytautashi/go-tank/game-client

go 1.19
22 changes: 22 additions & 0 deletions game-client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>go-tank</title>
<script src="js/phaser.min.js"></script>
<style>
body {
margin: 0px;
background-color: black;
}
</style>
</head>

<body>
<script src="src/SceneGame.js"></script>
<script src="src/Game.js"></script>
<div id="game-client"></div>
</body>

</html>
1 change: 1 addition & 0 deletions game-client/js/phaser.min.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions game-client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"log"
"net/http"
)

func main() {
log.Println("host: localhost:8081")

http.Handle("/", http.FileServer(http.Dir(".")))

err := http.ListenAndServe(":8081", nil)
if err != nil {
log.Fatal(err)
}
}
21 changes: 21 additions & 0 deletions game-client/src/Game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Game {
static GAME_SERVER_URL = "ws://" + document.location.hostname + ":8080/ws";

static config = {
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#999999',
parent: 'game-client',
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: [SceneGame]
};

static game = new Phaser.Game(Game.config);
}

const NOT_FOUND_INDEX = -1;

Loading

0 comments on commit 436fb21

Please sign in to comment.