Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 85954347 rédigé par Pol Lamothe's avatar Pol Lamothe
Parcourir les fichiers

Fonctionnement des portails avec le mode multijoueurs

parent a3c33cc5
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!12Ajout du mode multijoueur
...@@ -32,4 +32,5 @@ type Character struct { ...@@ -32,4 +32,5 @@ type Character struct {
animationFrameCount int animationFrameCount int
XShift, YShift int XShift, YShift int
CharacterNumber int CharacterNumber int
PortalSecure bool
} }
...@@ -8,13 +8,13 @@ import ( ...@@ -8,13 +8,13 @@ import (
"gitlab.univ-nantes.fr/jezequel-l/quadtree/portal" "gitlab.univ-nantes.fr/jezequel-l/quadtree/portal"
) )
var PortalSecur bool = false //variable qui sert a temporiser le mouvement a la sortie d'un portail pour empecher le personnage d'aller sur une case -1 //variable qui sert a temporiser le mouvement a la sortie d'un portail pour empecher le personnage d'aller sur une case -1
// Update met à jour la position du personnage, son orientation // Update met à jour la position du personnage, son orientation
// et son étape d'animation (si nécessaire) à chaque pas // et son étape d'animation (si nécessaire) à chaque pas
// de temps, c'est-à-dire tous les 1/60 secondes. // de temps, c'est-à-dire tous les 1/60 secondes.
func (c *Character) Update(blocking [4]bool, f *floor.Floor) { func (c *Character) Update(blocking [4]bool, f *floor.Floor) {
if !c.moving && !PortalSecur { if !c.moving && !c.PortalSecure {
if (ebiten.IsKeyPressed(ebiten.KeyRight) && (configuration.Global.MultiplayerKind == 0 || configuration.Global.MultiplayerKind == c.CharacterNumber)) || (multiplayer.KeyPressed == "right" && configuration.Global.MultiplayerKind != c.CharacterNumber) { if (ebiten.IsKeyPressed(ebiten.KeyRight) && (configuration.Global.MultiplayerKind == 0 || configuration.Global.MultiplayerKind == c.CharacterNumber)) || (multiplayer.KeyPressed == "right" && configuration.Global.MultiplayerKind != c.CharacterNumber) {
c.orientation = orientedRight c.orientation = orientedRight
if !blocking[1] { if !blocking[1] {
...@@ -51,18 +51,26 @@ func (c *Character) Update(blocking [4]bool, f *floor.Floor) { ...@@ -51,18 +51,26 @@ func (c *Character) Update(blocking [4]bool, f *floor.Floor) {
multiplayer.SendKeyPressed("down") multiplayer.SendKeyPressed("down")
} }
} }
} else if ebiten.IsKeyPressed(ebiten.KeyTab) && !portal.IsPortalHere(c.X, c.Y) && configuration.Global.Portal { } else if ebiten.IsKeyPressed(ebiten.KeyTab) && !portal.IsPortalHere(c.X, c.Y) && configuration.Global.Portal && (configuration.Global.MultiplayerKind == 0 || configuration.Global.MultiplayerKind == c.CharacterNumber) {
if len(portal.PortalStore) == 2 { if len(portal.PortalStore) == 2 {
portal.PortalStore = portal.PortalStore[1:] portal.PortalStore = portal.PortalStore[1:]
} }
portal.PortalStore = append(portal.PortalStore, []int{c.X, c.Y}) portal.PortalStore = append(portal.PortalStore, []int{c.X, c.Y})
if configuration.Global.MultiplayerKind != 0 {
multiplayer.SendKeyPressed("tab")
}
} else if configuration.Global.MultiplayerKind != 0 && c.CharacterNumber != configuration.Global.MultiplayerKind && multiplayer.KeyPressed == "tab" {
if len(multiplayer.MultiplayerPortal) == 2 {
multiplayer.MultiplayerPortal = multiplayer.MultiplayerPortal[1:]
}
multiplayer.MultiplayerPortal = append(multiplayer.MultiplayerPortal, []int{c.X, c.Y})
} }
if configuration.Global.MultiplayerKind != c.CharacterNumber { if configuration.Global.MultiplayerKind != c.CharacterNumber {
multiplayer.KeyPressed = "" multiplayer.KeyPressed = ""
} }
} else { } else {
c.animationFrameCount++ c.animationFrameCount++
PortalSecur = false c.PortalSecure = false
if c.animationFrameCount >= configuration.Global.NumFramePerCharacterAnimImage { if c.animationFrameCount >= configuration.Global.NumFramePerCharacterAnimImage {
c.animationFrameCount = 0 c.animationFrameCount = 0
shiftStep := configuration.Global.TileSize / configuration.Global.NumCharacterAnimImages shiftStep := configuration.Global.TileSize / configuration.Global.NumCharacterAnimImages
...@@ -95,28 +103,36 @@ func (c *Character) Update(blocking [4]bool, f *floor.Floor) { ...@@ -95,28 +103,36 @@ func (c *Character) Update(blocking [4]bool, f *floor.Floor) {
c.Y = c.Y % f.QuadtreeContent.Height c.Y = c.Y % f.QuadtreeContent.Height
} }
} }
if portal.IsPortalHere(c.X, c.Y) && len(portal.PortalStore) == 2 { if portal.IsPortalHere(c.X, c.Y) {
var newCoord []int = portal.GetOtherCoordonate(c.X, c.Y) if portal.IsInLocalPortalStore(c.X, c.Y) {
c.X = newCoord[0] if len(portal.PortalStore) == 2 {
c.Y = newCoord[1] var newCoord []int = portal.GetOtherCoordonate(c.X, c.Y)
f.AllBlockDisplayed = false c.X = newCoord[0]
c.xInc, c.yInc = 0, 0 c.Y = newCoord[1]
if configuration.Global.SingleUsagePortal { f.AllBlockDisplayed = false
portal.PortalStore = [][]int{} c.xInc, c.yInc = 0, 0
if configuration.Global.SingleUsagePortal {
portal.PortalStore = [][]int{}
}
c.PortalSecure = true
c.Update(blocking, f)
}
} else {
if len(multiplayer.MultiplayerPortal) == 2 {
var newCoord []int = portal.GetOtherCoordonate(c.X, c.Y)
c.X = newCoord[0]
c.Y = newCoord[1]
f.AllBlockDisplayed = false
c.xInc, c.yInc = 0, 0
if configuration.Global.SingleUsagePortal {
multiplayer.MultiplayerPortal = [][]int{}
}
c.PortalSecure = true
c.Update(blocking, f)
}
} }
PortalSecur = true
c.Update(blocking, f)
} }
} }
} }
} }
if c.CharacterNumber == 1 && configuration.Global.MultiplayerKind == 2 {
c.X = multiplayer.ServerPos["X"]
c.Y = multiplayer.ServerPos["Y"]
}
if c.CharacterNumber == 2 && configuration.Global.MultiplayerKind == 1 {
c.X = multiplayer.ClientPos["X"]
c.Y = multiplayer.ClientPos["Y"]
}
} }
...@@ -10,21 +10,35 @@ import ( ...@@ -10,21 +10,35 @@ import (
// sont bloquantes. // sont bloquantes.
func (f Floor) Blocking(characterXPos, characterYPos, camXPos, camYPos int) (blocking [4]bool) { func (f Floor) Blocking(characterXPos, characterYPos, camXPos, camYPos int) (blocking [4]bool) {
var relativeXPos, relativeYPos int var relativeXPos, relativeYPos int
if configuration.Global.CameraFluide { if configuration.Global.MultiplayerKind == 0 {
relativeXPos = (characterXPos - camXPos) + configuration.Global.ScreenCenterTileX + 1 if configuration.Global.CameraFluide {
relativeYPos = (characterYPos - camYPos) + configuration.Global.ScreenCenterTileY + 1 relativeXPos = (characterXPos - camXPos) + configuration.Global.ScreenCenterTileX + 1
blocking[0] = relativeYPos <= 0 || f.Content[relativeYPos-1][relativeXPos] == -1 relativeYPos = (characterYPos - camYPos) + configuration.Global.ScreenCenterTileY + 1
blocking[1] = relativeXPos-1 >= configuration.Global.NumTileX-1 || f.Content[relativeYPos][relativeXPos+1] == -1 blocking[0] = relativeYPos <= 0 || f.Content[relativeYPos-1][relativeXPos] == -1
blocking[2] = relativeYPos-1 >= configuration.Global.NumTileY-1 || f.Content[relativeYPos+1][relativeXPos] == -1 blocking[1] = relativeXPos-1 >= configuration.Global.NumTileX-1 || f.Content[relativeYPos][relativeXPos+1] == -1
blocking[3] = relativeXPos <= 0 || f.Content[relativeYPos][relativeXPos-1] == -1 blocking[2] = relativeYPos-1 >= configuration.Global.NumTileY-1 || f.Content[relativeYPos+1][relativeXPos] == -1
blocking[3] = relativeXPos <= 0 || f.Content[relativeYPos][relativeXPos-1] == -1
} else {
relativeXPos = characterXPos - camXPos + configuration.Global.ScreenCenterTileX
relativeYPos = characterYPos - camYPos + configuration.Global.ScreenCenterTileY
blocking[0] = relativeYPos <= 0 || f.Content[relativeYPos-1][relativeXPos] == -1
blocking[1] = relativeXPos >= configuration.Global.NumTileX-1 || f.Content[relativeYPos][relativeXPos+1] == -1
blocking[2] = relativeYPos >= configuration.Global.NumTileY-1 || f.Content[relativeYPos+1][relativeXPos] == -1
blocking[3] = relativeXPos <= 0 || f.Content[relativeYPos][relativeXPos-1] == -1
}
} else { } else {
relativeXPos = characterXPos - camXPos + configuration.Global.ScreenCenterTileX var topLeftY, topLeftX int = characterYPos - configuration.Global.NumTileY/2, characterXPos - configuration.Global.NumTileX/2
relativeYPos = characterYPos - camYPos + configuration.Global.ScreenCenterTileY var mapContent [][]int = f.QuadtreeContent.GetContent(topLeftX, topLeftY, f.Content)
blocking[0] = relativeYPos <= 0 || f.Content[relativeYPos-1][relativeXPos] == -1 relativeXPos = configuration.Global.ScreenCenterTileX
blocking[1] = relativeXPos >= configuration.Global.NumTileX-1 || f.Content[relativeYPos][relativeXPos+1] == -1 relativeYPos = configuration.Global.ScreenCenterTileY
blocking[2] = relativeYPos >= configuration.Global.NumTileY-1 || f.Content[relativeYPos+1][relativeXPos] == -1 if configuration.Global.CameraFluide {
blocking[3] = relativeXPos <= 0 || f.Content[relativeYPos][relativeXPos-1] == -1 relativeXPos++
relativeYPos++
}
blocking[0] = mapContent[relativeYPos-1][relativeXPos] == -1
blocking[1] = mapContent[relativeYPos][relativeXPos+1] == -1
blocking[2] = mapContent[relativeYPos+1][relativeXPos] == -1
blocking[3] = mapContent[relativeYPos][relativeXPos-1] == -1
} }
return blocking return blocking
} }
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil" "github.com/hajimehoshi/ebiten/v2/inpututil"
"gitlab.univ-nantes.fr/jezequel-l/quadtree/configuration" "gitlab.univ-nantes.fr/jezequel-l/quadtree/configuration"
"gitlab.univ-nantes.fr/jezequel-l/quadtree/multiplayer"
) )
// Update met à jour les données du jeu à chaque 1/60 de seconde. // Update met à jour les données du jeu à chaque 1/60 de seconde.
...@@ -17,14 +16,6 @@ func (g *Game) Update() error { ...@@ -17,14 +16,6 @@ func (g *Game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyD) { if inpututil.IsKeyJustPressed(ebiten.KeyD) {
configuration.Global.DebugMode = !configuration.Global.DebugMode configuration.Global.DebugMode = !configuration.Global.DebugMode
} }
if configuration.Global.MultiplayerKind == 1 {
g.Character2.X = multiplayer.ClientPos["X"]
g.Character2.Y = multiplayer.ClientPos["Y"]
}
if configuration.Global.MultiplayerKind == 2 {
g.Character.X = multiplayer.ServerPos["X"]
g.Character.Y = multiplayer.ServerPos["Y"]
}
g.Character.Update(g.floor.Blocking(g.Character.X, g.Character.Y, int(g.camera.X), int(g.camera.Y)), &(g.floor)) g.Character.Update(g.floor.Blocking(g.Character.X, g.Character.Y, int(g.camera.X), int(g.camera.Y)), &(g.floor))
g.Character2.Update(g.floor.Blocking(g.Character2.X, g.Character2.Y, int(g.camera.X), int(g.camera.Y)), &(g.floor)) g.Character2.Update(g.floor.Blocking(g.Character2.X, g.Character2.Y, int(g.camera.X), int(g.camera.Y)), &(g.floor))
g.Character.RefreshShift() g.Character.RefreshShift()
......
...@@ -13,6 +13,7 @@ var WaitingForResponse bool = false ...@@ -13,6 +13,7 @@ var WaitingForResponse bool = false
var ServerPos map[string]int = map[string]int{"X": 0, "Y": 0} var ServerPos map[string]int = map[string]int{"X": 0, "Y": 0}
var ClientPos map[string]int = map[string]int{"X": 0, "Y": 0} var ClientPos map[string]int = map[string]int{"X": 0, "Y": 0}
var KeyPressed string = "" var KeyPressed string = ""
var MultiplayerPortal [][]int = [][]int{}
func SendMap() { func SendMap() {
JSONData := map[string]interface{}{ JSONData := map[string]interface{}{
......
package portal package portal
import (
"gitlab.univ-nantes.fr/jezequel-l/quadtree/configuration"
"gitlab.univ-nantes.fr/jezequel-l/quadtree/multiplayer"
)
var PortalStore [][]int = [][]int{} var PortalStore [][]int = [][]int{}
func IsPortalHere(x, y int) bool { func IsPortalHere(x, y int) bool {
...@@ -8,14 +13,38 @@ func IsPortalHere(x, y int) bool { ...@@ -8,14 +13,38 @@ func IsPortalHere(x, y int) bool {
return true return true
} }
} }
if configuration.Global.MultiplayerKind != 0 {
for i := 0; i < len(multiplayer.MultiplayerPortal); i++ {
if multiplayer.MultiplayerPortal[i][0] == x && multiplayer.MultiplayerPortal[i][1] == y {
return true
}
}
}
return false return false
} }
func GetOtherCoordonate(x0, y0 int) []int { func GetOtherCoordonate(x0, y0 int) []int {
for i := 0; i < len(PortalStore); i++ { if IsInLocalPortalStore(x0, y0) {
if PortalStore[i][0] != x0 || PortalStore[i][1] != y0 { for i := 0; i < len(PortalStore); i++ {
return []int{PortalStore[i][0], PortalStore[i][1]} if PortalStore[i][0] != x0 || PortalStore[i][1] != y0 {
return []int{PortalStore[i][0], PortalStore[i][1]}
}
}
} else if configuration.Global.MultiplayerKind != 0 {
for i := 0; i < len(multiplayer.MultiplayerPortal); i++ {
if multiplayer.MultiplayerPortal[i][0] != x0 || multiplayer.MultiplayerPortal[i][1] != y0 {
return []int{multiplayer.MultiplayerPortal[i][0], multiplayer.MultiplayerPortal[i][1]}
}
} }
} }
return nil return nil
} }
func IsInLocalPortalStore(x, y int) bool {
for i := 0; i < len(PortalStore); i++ {
if PortalStore[i][0] == x && PortalStore[i][1] == y {
return true
}
}
return false
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter