Skip to content

Commit 647af63

Browse files
Laissé en plan : players mov through tcp, not pos
1 parent 1fb2750 commit 647af63

15 files changed

+215
-189
lines changed

main.go

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import (
1010
func main() {
1111
args := os.Args
1212

13-
for k, a := range args {
14-
fmt.Println(k, a)
15-
}
16-
1713
if len(args) >= 3 && args[1] == "server" {
1814
host := args[2]
1915
port := args[3]

src/game/entity/player.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package entity
22

3-
const DEFAULT_SPEED float64 = 0.08
4-
const DEFAULT_JUMP_SPEED float64 = 0.21
5-
const GRAVITY float64 = 0.0065
3+
const DEFAULT_SPEED float64 = 0.045
4+
const DEFAULT_JUMP_SPEED float64 = 0.136
5+
const GRAVITY float64 = 0.003
66

77
type Player struct {
88
// Identification properties
@@ -15,6 +15,7 @@ type Player struct {
1515
VerticalVelocity float64
1616
MovesLeft bool
1717
MovesRight bool
18+
IsJumping bool
1819

1920
// Statistics of player
2021
EatBox [4]float64
@@ -32,6 +33,7 @@ func NewPlayer(pos Pos, nickname, character string) Player {
3233
TouchesGround: false,
3334
MovesLeft: false,
3435
MovesRight: false,
36+
IsJumping: false,
3537

3638
EatBox: [4]float64{0.1, 0.1, 0.9, 1.95},
3739
Speed: DEFAULT_SPEED,

src/game/entity/playerInfo.go

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
package entity
22

33
import (
4-
"errors"
5-
"strings"
4+
"encoding/json"
5+
"gopherLand2/src/localInstance/input"
66
)
77

88
// Used for multiplayer data sharing
99
type PlayerInfo struct {
10-
Nickname string
11-
Pos Pos
10+
Nickname string `json:"nickname"`
11+
Pos Pos `json:"pos"`
12+
KeyPressed input.KeyPressed `json:"keyPressed"`
1213
}
1314

14-
func (pi PlayerInfo) ToString() string {
15-
return pi.Nickname + "|" + pi.Pos.ToString()
15+
func (pi PlayerInfo) Stringify() ([]byte, error) {
16+
data, err := json.Marshal(pi)
17+
if err != nil {
18+
return []byte{}, err
19+
}
20+
return data, nil
1621
}
1722

18-
func ParsePlayerInfo(s string) (PlayerInfo, error) {
19-
split := strings.Split(s, "|")
20-
if len(split) == 2 {
21-
pp, err := ParsePos(split[1])
22-
if err != nil {
23-
return PlayerInfo{}, err
24-
} else {
25-
pi := PlayerInfo{
26-
Nickname: split[0],
27-
Pos: pp,
28-
}
29-
return pi, nil
30-
}
31-
} else {
32-
return PlayerInfo{}, errors.New("there's extra | symbols in pseudo")
23+
func Parse(data []byte) (PlayerInfo, error) {
24+
var pi PlayerInfo
25+
err := json.Unmarshal(data, &pi)
26+
if err != nil {
27+
return PlayerInfo{}, err
3328
}
29+
return pi, nil
3430
}

src/game/entity/pos.go

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
package entity
22

3-
import "fmt"
4-
53
type Pos struct {
6-
X float64
7-
Y float64
8-
}
9-
10-
func (p Pos) ToString() string {
11-
return fmt.Sprintf("(%v, %v)", p.X, p.Y)
12-
}
13-
14-
func ParsePos(s string) (Pos, error) {
15-
var p Pos
16-
_, err := fmt.Sscanf(s, "(%f, %f)", &p.X, &p.Y)
17-
if err != nil {
18-
return Pos{}, err
19-
}
20-
return p, nil
4+
X float64 `json:"x"`
5+
Y float64 `json:"y"`
216
}

src/game/game.go

+27-31
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"gopherLand2/src/game/entity"
88
"gopherLand2/src/game/gameMap"
99
"gopherLand2/src/game/ressources"
10-
"time"
10+
"gopherLand2/src/localInstance/input"
1111
)
1212

1313
type Game struct {
@@ -19,45 +19,44 @@ type Game struct {
1919
GameMap gameMap.GameMap // A map
2020

2121
// Local Player
22-
Player entity.Player // Playable player
23-
Channel chan string // Controls the player
24-
Tick int // Tick of the game
25-
TickMS int // Delay between each tick in ms
22+
Player entity.Player // Playable player
23+
Tick int // Tick of the game
24+
TickMS int // Delay between each tick in ms
25+
26+
// PlayerInputChannel for getting input of player
27+
PlayerInputChannel chan input.KeyPressed // Controls the player
2628

2729
// Multiplayer data
28-
PlayerPos map[string]entity.Pos
30+
PlayerInfos map[string]entity.PlayerInfo
2931

3032
// Channels for multiplayer
31-
PlayerPosChannel chan entity.PlayerInfo // Channel for sending own player position if multiplayer
32-
AllPlayersPosChannel chan []byte // Channel for receiving all players positions if multiplayer
33+
PlayerPosChannel chan entity.PlayerInfo // Channel for sending own player position if multiplayer
34+
AllPlayerInfosChannel chan []byte // Channel for receiving all players positions if multiplayer
3335
}
3436

3537
// Create a new game with a channel that receive players' actions
36-
func New(ch chan string) Game {
38+
func New(ch chan input.KeyPressed) Game {
3739
// Load game's config
3840
cfg := loadConfig()
3941

4042
// Create Game
4143
g := Game{
42-
Config: cfg,
43-
Ressources: ressources.New(cfg.Size),
44-
GameMap: gameMap.New(),
45-
Player: entity.Player{},
46-
Channel: ch,
47-
Tick: 0,
48-
TickMS: cfg.TickMS,
44+
Config: cfg,
45+
Ressources: ressources.New(cfg.Size),
46+
GameMap: gameMap.New(),
47+
Player: entity.Player{},
48+
PlayerInputChannel: ch,
49+
Tick: 0,
50+
TickMS: cfg.TickMS,
4951
}
5052

5153
return g
5254
}
5355

54-
// Run the game
56+
// Run the game, function called by ebiten's Update() function (in file gameWindow.go)
5557
func (g *Game) Run() {
56-
for {
57-
g.Tick++
58-
time.Sleep(time.Duration(g.TickMS) * time.Millisecond)
59-
g.ComputeTick()
60-
}
58+
g.Tick++
59+
g.ComputeTick()
6160
}
6261

6362
// Add a new player
@@ -79,24 +78,21 @@ func (g *Game) SetPlayerNickname(nickname string) {
7978
}
8079

8180
// Bind a channel for sending to multiplayer server player's data
82-
func (g *Game) BindMultiplayerChannels(playerPosChannel chan entity.PlayerInfo, allPlayersPosChannel chan []byte) {
81+
func (g *Game) BindMultiplayerChannels(playerPosChannel chan entity.PlayerInfo, allPlayerInfosChannel chan []byte) {
8382
g.PlayerPosChannel = playerPosChannel
84-
g.AllPlayersPosChannel = allPlayersPosChannel
83+
g.AllPlayerInfosChannel = allPlayerInfosChannel
8584
go g.UpdateAllPlayers()
8685
}
8786

8887
// Update all player in case of server send data
8988
func (g *Game) UpdateAllPlayers() {
9089
for {
91-
data := <-g.AllPlayersPosChannel
92-
playersConnected := map[string]entity.Pos{}
90+
data := <-g.AllPlayerInfosChannel
91+
playersConnected := map[string]entity.PlayerInfo{}
9392
err := json.Unmarshal(bytes.Trim(data, string([]byte{0})), &playersConnected)
9493
if err != nil {
95-
fmt.Println("Error parsing players' data sent by server: " + err.Error())
96-
97-
fmt.Println(string(data))
94+
fmt.Println("Error parsing players' data sent by server: " + string(data) + " - Error:" + err.Error())
9895
}
99-
100-
g.PlayerPos = playersConnected
96+
g.PlayerInfos = playersConnected
10197
}
10298
}

src/game/playerMovment.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package game
22

33
import (
44
"gopherLand2/src/game/entity"
5+
"gopherLand2/src/localInstance/input"
56
"math"
67
)
78

@@ -13,6 +14,12 @@ func (g *Game) ComputeTick() {
1314
if !g.Player.MovesLeft && g.Player.MovesRight {
1415
g.moveHorizonally(entity.DEFAULT_SPEED)
1516
}
17+
if g.Player.IsJumping {
18+
if g.Player.TouchesGround {
19+
g.Player.TouchesGround = false
20+
g.Player.VerticalVelocity -= g.Player.JumpSpeed
21+
}
22+
}
1623

1724
if !g.Player.TouchesGround {
1825
g.moveVertically(g.Player.VerticalVelocity)
@@ -22,26 +29,26 @@ func (g *Game) ComputeTick() {
2229

2330
// Handle player's control
2431
func (g *Game) RunPlayer() {
25-
var action string
32+
var keyPressed input.KeyPressed
2633

2734
for {
28-
action = <-g.Channel
35+
keyPressed = <-g.PlayerInputChannel
2936

30-
switch action {
31-
case "left":
37+
if keyPressed.Left {
3238
g.Player.MovesLeft = true
33-
case "right":
34-
g.Player.MovesRight = true
35-
case "up":
36-
if g.Player.TouchesGround {
37-
g.Player.TouchesGround = false
38-
g.Player.VerticalVelocity -= g.Player.JumpSpeed
39-
}
40-
case "released_left":
39+
} else {
4140
g.Player.MovesLeft = false
42-
case "released_right":
41+
}
42+
if keyPressed.Right {
43+
g.Player.MovesRight = true
44+
} else {
4345
g.Player.MovesRight = false
4446
}
47+
if keyPressed.Up {
48+
g.Player.IsJumping = true
49+
} else {
50+
g.Player.IsJumping = false
51+
}
4552
}
4653
}
4754

src/localInstance/TCPClient/TCPClient.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
const TYPE string = "tcp"
1111

12-
func StartTCPClient(host, port, nickname string, playerPosChannel chan entity.PlayerInfo, allPlayersPosChannel chan []byte) {
12+
func StartTCPClient(host, port, nickname string, playerPosChannel chan entity.PlayerInfo, allPlayerInfosChannel chan []byte) {
1313
tcpServer, err := net.ResolveTCPAddr(TYPE, host+":"+port)
1414

1515
if err != nil {
@@ -36,7 +36,7 @@ func StartTCPClient(host, port, nickname string, playerPosChannel chan entity.Pl
3636
fmt.Println("Read data failed:", err.Error())
3737
os.Exit(1)
3838
} else {
39-
allPlayersPosChannel <- received
39+
allPlayerInfosChannel <- received
4040
}
4141

4242
fmt.Println("Connected to server " + host + ":" + port)
@@ -50,19 +50,24 @@ func StartTCPClient(host, port, nickname string, playerPosChannel chan entity.Pl
5050
fmt.Println("Read data failed:", err.Error())
5151
os.Exit(1)
5252
} else {
53-
allPlayersPosChannel <- received
53+
allPlayerInfosChannel <- received
5454
}
5555
}
5656
}()
5757

58-
// Send own player position
58+
// Send own player info
5959
for {
60-
action := <-playerPosChannel
60+
playerInfo := <-playerPosChannel
6161

62-
_, err = conn.Write([]byte("1" + action.ToString()))
62+
pi, err := playerInfo.Stringify()
6363
if err != nil {
64-
fmt.Println("Write data failed:", err.Error())
65-
os.Exit(1)
64+
fmt.Println("Cannot parse playerInfo into JSON: " + err.Error())
65+
} else {
66+
_, err = conn.Write([]byte("1" + string(pi)))
67+
if err != nil {
68+
fmt.Println("Write data failed:", err.Error())
69+
os.Exit(1)
70+
}
6671
}
6772
}
6873
}

src/localInstance/gameWindow/drawEntities.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ func (g *Graphics) drawEntities(screen *ebiten.Image) {
1616
xPlayer := g.game.Player.Pos.X
1717
yPlayer := g.game.Player.Pos.Y
1818

19-
for nickname, pos := range g.game.PlayerPos {
19+
for nickname, pi := range g.game.PlayerInfos {
2020
if g.game.Player.Nickname != nickname {
2121
op := &ebiten.DrawImageOptions{}
2222

2323
op.GeoM.Translate(
24-
(float64(pos.X)-xPlayer)*g.size+g.halfWidth,
25-
(float64(pos.Y)-yPlayer)*g.size+g.halfHeight,
24+
(float64(pi.Pos.X)-xPlayer)*g.size+g.halfWidth,
25+
(float64(pi.Pos.Y)-yPlayer)*g.size+g.halfHeight,
2626
)
2727

2828
screen.DrawImage(g.game.Ressources.Elements["p"].Img, op)

0 commit comments

Comments
 (0)