Skip to content

Commit

Permalink
vidosik
Browse files Browse the repository at this point in the history
  • Loading branch information
jilio committed Sep 24, 2019
0 parents commit a6823c4
Show file tree
Hide file tree
Showing 339 changed files with 594 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# tomb_mates

## Запустить сервер

```
cd server
go run *go
```

## Запустить игру

###### Не будет работать без запущенного сервера

```
go run main.go
```

## Спрайты

[DungeonTilesetII](https://0x72.itch.io/dungeontileset-ii)
133 changes: 133 additions & 0 deletions game/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package game

import (
"encoding/json"
"math/rand"
"time"

uuid "github.com/satori/go.uuid"
)

type Unit struct {
ID string `json:"id"`
X float64 `json:"x"`
Y float64 `json:"y"`
SpriteName string `json:"sprite_name"`
Action string `json:"action"`
Frame int `json:"frame"`
HorizontalDirection int `json:"direction"`
}

type Units map[string]*Unit

type World struct {
MyID string `json:"-"`
IsServer bool `json:"-"`
Units `json:"units"`
}

type Event struct {
Type string `json:"type"`
Data interface{} `json:"data"`
}

type EventConnect struct {
Unit
}

type EventMove struct {
UnitID string `json:"unit_id"`
Direction int `json:"direction"`
}

type EventIdle struct {
UnitID string `json:"unit_id"`
}

type EventInit struct {
PlayerID string `json:"player_id"`
Units Units `json:"units"`
}

const EventTypeConnect = "connect"
const EventTypeMove = "move"
const EventTypeIdle = "idle"
const EventTypeInit = "init"

const ActionRun = "run"
const ActionIdle = "idle"

const DirectionUp = 0
const DirectionDown = 1
const DirectionLeft = 2
const DirectionRight = 3

func (world *World) HandleEvent(event *Event) {
switch event.Type {
case EventTypeConnect:
str, _ := json.Marshal(event.Data)
var ev EventConnect
json.Unmarshal(str, &ev)

world.Units[ev.ID] = &ev.Unit

case EventTypeInit:
str, _ := json.Marshal(event.Data)
var ev EventInit
json.Unmarshal(str, &ev)

if !world.IsServer {
world.MyID = ev.PlayerID
world.Units = ev.Units
}

case EventTypeMove:
str, _ := json.Marshal(event.Data)
var ev EventMove
json.Unmarshal(str, &ev)

unit := world.Units[ev.UnitID]
unit.Action = ActionRun

switch ev.Direction {
case DirectionUp:
unit.Y--
case DirectionDown:
unit.Y++
case DirectionLeft:
unit.X--
unit.HorizontalDirection = ev.Direction
case DirectionRight:
unit.X++
unit.HorizontalDirection = ev.Direction
}

case EventTypeIdle:
str, _ := json.Marshal(event.Data)
var ev EventIdle
json.Unmarshal(str, &ev)

unit := world.Units[ev.UnitID]
unit.Action = ActionIdle
}
}

func (world *World) AddPlayer() *Unit {
skins := []string{
"elf_f", "elf_m", "knight_f", "knight_m",
"lizard_f", "lizard_m", "wizzard_f", "wizzard_m",
}
id := uuid.NewV4().String()
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
unit := &Unit{
ID: id,
Action: ActionIdle,
X: rnd.Float64() * 320,
Y: rnd.Float64() * 240,
Frame: rnd.Intn(4),
SpriteName: skins[rnd.Intn(len(skins))],
}
world.Units[id] = unit

return unit
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/jilio/tomb_mates

go 1.12

require (
github.com/gin-gonic/gin v1.4.0
github.com/gorilla/websocket v1.4.1
github.com/hajimehoshi/ebiten v1.9.3
github.com/satori/go.uuid v1.2.0
)
64 changes: 64 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f h1:7MsFMbSn8Lcw0blK4+NEOf8DuHoOBDhJsHz04yh13pM=
github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/gofrs/flock v0.7.0/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherwasm v0.1.1/go.mod h1:kx4n9a+MzHH0BJJhvlsQ65hqLFXDO/m256AsaDPQ+/4=
github.com/gopherjs/gopherwasm v1.0.0/go.mod h1:SkZ8z7CWBz5VXbhJel8TxCmAcsQqzgWGR/8nMhyhZSI=
github.com/gopherjs/gopherwasm v1.1.0/go.mod h1:SkZ8z7CWBz5VXbhJel8TxCmAcsQqzgWGR/8nMhyhZSI=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hajimehoshi/bitmapfont v1.1.1/go.mod h1:Hamfxgney7tDSmVOSDh2AWzoDH70OaC+P24zc02Gum4=
github.com/hajimehoshi/ebiten v1.9.3 h1:YijWGMBwH2XA1ZytUQFy33UDHeCSS6d4JZKH1wq38O0=
github.com/hajimehoshi/ebiten v1.9.3/go.mod h1:XxiJ4Eltvb1KmcD0i6F81eIB1asJhK47y5DC+FPkyso=
github.com/hajimehoshi/go-mp3 v0.2.0/go.mod h1:4i+c5pDNKDrxl1iu9iG90/+fhP37lio6gNhjCx9WBJw=
github.com/hajimehoshi/oto v0.1.1/go.mod h1:hUiLWeBQnbDu4pZsAhOnGqMI1ZGibS6e2qhQdfpwz04=
github.com/hajimehoshi/oto v0.3.3/go.mod h1:e9eTLBB9iZto045HLbzfHJIc+jP3xaKrjZTghvb6fdM=
github.com/jakecoffman/cp v0.1.0/go.mod h1:a3xPx9N8RyFAACD644t2dj/nK4SuLg1v+jL61m2yVo4=
github.com/jfreymuth/oggvorbis v1.0.0/go.mod h1:abe6F9QRjuU9l+2jek3gj46lu40N4qlYxh2grqkLEDM=
github.com/jfreymuth/vorbis v1.0.0/go.mod h1:8zy3lUAm9K/rJJk223RKy6vjCZTWC61NA2QD06bfOE0=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20180710024300-14dda7b62fcd/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20180926015637-991ec62608f3/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190118043309-183bebdce1b2/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/mobile v0.0.0-20180806140643-507816974b79/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190127143845-a42111704963/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190203050204-7ae0202eb74c/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-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190202235157-7414d4c1f71c/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
131 changes: 131 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"log"
"sort"
"strconv"

"github.com/gorilla/websocket"
e "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/jilio/tomb_mates/game"
)

var world game.World
var frame int
var img *e.Image

func init() {
world = game.World{
IsServer: false,
Units: game.Units{},
}
}

func update(c *websocket.Conn) func(screen *e.Image) error {
return func(screen *e.Image) error {
frame++

img, _, _ = ebitenutil.NewImageFromFile(
"sprites/background.png",
e.FilterDefault,
)
screen.DrawImage(img, nil)

unitList := []*game.Unit{}
for _, unit := range world.Units {
unitList = append(unitList, unit)
}
sort.Slice(unitList, func(i, j int) bool {
return unitList[i].Y < unitList[j].Y
})

for _, unit := range unitList {
op := &e.DrawImageOptions{}
if unit.HorizontalDirection == game.DirectionLeft {
op.GeoM.Scale(-1, 1)
op.GeoM.Translate(16, 0)
}
op.GeoM.Translate(unit.X, unit.Y)

spriteIndex := (frame/7 + unit.Frame) % 4
img, _, _ = ebitenutil.NewImageFromFile(
"sprites/"+unit.SpriteName+"_"+unit.Action+"_anim_f"+strconv.Itoa(spriteIndex)+".png",
e.FilterDefault,
)
screen.DrawImage(img, op)
}

if e.IsKeyPressed(e.KeyD) || e.IsKeyPressed(e.KeyRight) {
c.WriteJSON(game.Event{
Type: game.EventTypeMove,
Data: game.EventMove{
UnitID: world.MyID,
Direction: game.DirectionRight,
},
})
return nil
}

if e.IsKeyPressed(e.KeyA) || e.IsKeyPressed(e.KeyLeft) {
c.WriteJSON(game.Event{
Type: game.EventTypeMove,
Data: game.EventMove{
UnitID: world.MyID,
Direction: game.DirectionLeft,
},
})
return nil
}

if e.IsKeyPressed(e.KeyW) || e.IsKeyPressed(e.KeyUp) {
c.WriteJSON(game.Event{
Type: game.EventTypeMove,
Data: game.EventMove{
UnitID: world.MyID,
Direction: game.DirectionUp,
},
})
return nil
}

if e.IsKeyPressed(e.KeyS) || e.IsKeyPressed(e.KeyDown) {
c.WriteJSON(game.Event{
Type: game.EventTypeMove,
Data: game.EventMove{
UnitID: world.MyID,
Direction: game.DirectionDown,
},
})
return nil
}

if world.Units[world.MyID].Action == game.ActionRun {
c.WriteJSON(game.Event{
Type: game.EventTypeIdle,
Data: game.EventMove{
UnitID: world.MyID,
},
})
}

return nil
}
}

func main() {
c, _, _ := websocket.DefaultDialer.Dial("ws://127.0.0.1:3000/ws", nil)
go func(c *websocket.Conn) {
defer c.Close()

for {
var event game.Event
c.ReadJSON(&event)
world.HandleEvent(&event)
log.Println(event)
}
}(c)

e.SetRunnableInBackground(true)
e.Run(update(c), 320, 240, 2, "Tomb Mates")
}
Loading

0 comments on commit a6823c4

Please sign in to comment.