GOingTunneling/playerData.go

390 lines
11 KiB
Go
Raw Permalink Normal View History

2024-08-29 18:48:27 +02:00
package main
import (
"github.com/veandco/go-sdl2/sdl"
"image/color"
)
type KeyMap struct {
exit int
shoot int
super int
up int
down int
left int
right int
}
var keyMaps = []KeyMap{
{
exit: sdl.SCANCODE_ESCAPE,
shoot: sdl.SCANCODE_SPACE,
super: sdl.SCANCODE_LCTRL,
up: sdl.SCANCODE_W,
down: sdl.SCANCODE_S,
left: sdl.SCANCODE_A,
right: sdl.SCANCODE_D,
},
{
exit: sdl.SCANCODE_END,
shoot: sdl.SCANCODE_RETURN,
super: sdl.SCANCODE_RCTRL,
up: sdl.SCANCODE_UP,
down: sdl.SCANCODE_DOWN,
left: sdl.SCANCODE_LEFT,
right: sdl.SCANCODE_RIGHT,
},
}
type JoyMap struct {
xAxis int
yAxis int
exitButton int
shootButton int
superButton int
upButton int
downButton int
leftButton int
rightButton int
}
var joyMaps = []JoyMap{
{
xAxis: 0,
yAxis: 1,
exitButton: 0,
shootButton: 1,
superButton: 2,
upButton: 3,
downButton: 4,
leftButton: 5,
rightButton: 6,
},
}
type PlayerColors struct {
tracks color.Color
body color.Color
cannon color.Color
}
func getNeededPlayers() (neededPlayers uint8) {
neededPlayers = 0
2024-08-30 19:07:56 +02:00
if config.DoAllKeymapsPlayers {
2024-08-29 18:48:27 +02:00
neededPlayers += uint8(len(keyMaps))
2024-08-30 19:07:56 +02:00
} else if config.DoKeymapPlayer && len(keyMaps) > 0 {
2024-08-29 18:48:27 +02:00
neededPlayers++
}
2024-08-30 19:07:56 +02:00
if config.DoJoyStickPlayers {
2024-08-29 18:48:27 +02:00
neededPlayers += uint8(sdl.NumJoysticks())
}
2024-08-30 19:07:56 +02:00
if neededPlayers < 2 && !config.Server {
2024-08-29 18:48:27 +02:00
neededPlayers = 2
}
return neededPlayers
}
var playerColors []PlayerColors
func initPlayerColors() {
playerColors = []PlayerColors{
{
tracks: color.RGBA{B: 182, A: 255},
body: color.RGBA{R: 44, G: 44, B: 255, A: 255},
cannon: color.RGBA{R: 243, G: 235, B: 28, A: 255},
},
{
tracks: color.RGBA{R: 44, G: 184, B: 44, A: 255},
body: color.RGBA{G: 249, A: 255},
cannon: color.RGBA{R: 243, G: 235, B: 28, A: 255},
},
// Set 3
{
tracks: color.RGBA{R: 255, A: 255}, // Bright Red
body: color.RGBA{G: 255, B: 255, A: 255}, // Cyan
cannon: color.RGBA{R: 255, G: 165, A: 255}, // Orange
},
// Set 4
{
tracks: color.RGBA{R: 75, B: 130, A: 255}, // Indigo
body: color.RGBA{R: 255, G: 20, B: 147, A: 255}, // Deep Pink
cannon: color.RGBA{G: 128, B: 128, A: 255}, // Teal
},
// Set 5
{
tracks: color.RGBA{R: 255, G: 105, B: 180, A: 255}, // Hot Pink
body: color.RGBA{R: 34, G: 139, B: 34, A: 255}, // Forest Green
cannon: color.RGBA{A: 255}, // Black
},
// Set 6
{
tracks: color.RGBA{R: 128, A: 255}, // Maroon
body: color.RGBA{R: 255, G: 255, A: 255}, // Yellow
cannon: color.RGBA{B: 128, A: 255}, // Navy Blue
},
// Set 7
{
tracks: color.RGBA{R: 255, G: 69, A: 255}, // Red-Orange
body: color.RGBA{G: 206, B: 209, A: 255}, // Dark Turquoise
cannon: color.RGBA{R: 50, G: 205, B: 50, A: 255}, // Lime Green
},
// Set 8
{
tracks: color.RGBA{R: 128, G: 128, A: 255}, // Olive
body: color.RGBA{R: 255, G: 105, B: 180, A: 255}, // Hot Pink
cannon: color.RGBA{G: 191, B: 255, A: 255}, // Deep Sky Blue
},
// Set 9
{
tracks: color.RGBA{R: 255, G: 20, B: 20, A: 255}, // Bright Red
body: color.RGBA{G: 255, B: 127, A: 255}, // Spring Green
cannon: color.RGBA{R: 186, G: 85, B: 211, A: 255}, // Medium Orchid
},
// Set 10
{
tracks: color.RGBA{R: 255, G: 215, A: 255}, // Gold
body: color.RGBA{B: 139, A: 255}, // Dark Blue
cannon: color.RGBA{R: 238, G: 130, B: 238, A: 255}, // Violet
},
// Set 11
{
tracks: color.RGBA{R: 210, G: 105, B: 30, A: 255}, // Chocolate
body: color.RGBA{R: 72, G: 61, B: 139, A: 255}, // Dark Slate Blue
cannon: color.RGBA{R: 152, G: 251, B: 152, A: 255}, // Pale Green
},
// Set 12
{
tracks: color.RGBA{R: 220, G: 20, B: 60, A: 255}, // Crimson
body: color.RGBA{R: 255, G: 250, B: 205, A: 255}, // Lemon Chiffon
cannon: color.RGBA{G: 128, A: 255}, // Green
},
// Set 13
{
tracks: color.RGBA{R: 139, B: 139, A: 255}, // Dark Magenta
body: color.RGBA{R: 144, G: 238, B: 144, A: 255}, // Light Green
cannon: color.RGBA{R: 255, G: 69, A: 255}, // Orange Red
},
// Set 14
{
tracks: color.RGBA{B: 255, A: 255}, // Blue
body: color.RGBA{R: 240, G: 128, B: 128, A: 255}, // Light Coral
cannon: color.RGBA{R: 218, G: 165, B: 32, A: 255}, // Goldenrod
},
// Set 15
{
tracks: color.RGBA{R: 65, G: 105, B: 225, A: 255}, // Royal Blue
body: color.RGBA{R: 255, G: 222, B: 173, A: 255}, // Navajo White
cannon: color.RGBA{R: 255, B: 255, A: 255}, // Magenta
},
// Set 16
{
tracks: color.RGBA{R: 147, G: 112, B: 219, A: 255}, // Medium Purple
body: color.RGBA{G: 255, B: 127, A: 255}, // Spring Green
cannon: color.RGBA{R: 255, G: 255, A: 255}, // Yellow
},
// Set 17
{
tracks: color.RGBA{R: 255, G: 140, A: 255}, // Dark Orange
body: color.RGBA{R: 127, G: 255, A: 255}, // Chartreuse
cannon: color.RGBA{R: 25, G: 25, B: 112, A: 255}, // Midnight Blue
},
// Set 18
{
tracks: color.RGBA{R: 240, G: 230, B: 140, A: 255}, // Khaki
body: color.RGBA{R: 173, G: 216, B: 230, A: 255}, // Light Blue
cannon: color.RGBA{R: 128, B: 128, A: 255}, // Purple
},
// Set 19
{
tracks: color.RGBA{R: 70, G: 130, B: 180, A: 255}, // Steel Blue
body: color.RGBA{R: 255, G: 99, B: 71, A: 255}, // Tomato
cannon: color.RGBA{R: 154, G: 205, B: 50, A: 255}, // Yellow Green
},
// Set 20
{
tracks: color.RGBA{R: 106, G: 90, B: 205, A: 255}, // Slate Blue
body: color.RGBA{R: 255, G: 182, B: 193, A: 255}, // Light Pink
cannon: color.RGBA{R: 46, G: 139, B: 87, A: 255}, // Sea Green
},
// Set 21
{
tracks: color.RGBA{R: 199, G: 21, B: 133, A: 255}, // Medium Violet Red
body: color.RGBA{G: 255, B: 255, A: 255}, // Aqua
cannon: color.RGBA{R: 255, G: 160, B: 122, A: 255}, // Light Salmon
},
// Set 22
{
tracks: color.RGBA{B: 139, A: 255}, // Dark Blue
body: color.RGBA{R: 173, G: 255, B: 47, A: 255}, // Green Yellow
cannon: color.RGBA{R: 255, G: 228, B: 181, A: 255},
},
// Set 23
{
tracks: color.RGBA{R: 205, G: 92, B: 92, A: 255}, // Indian Red
body: color.RGBA{G: 191, B: 255, A: 255}, // Deep Sky Blue
cannon: color.RGBA{R: 255, G: 255, B: 224, A: 255}, // Light Yellow
},
// Set 24
{
tracks: color.RGBA{R: 139, G: 69, B: 19, A: 255}, // Saddle Brown
body: color.RGBA{R: 255, G: 99, B: 71, A: 255}, // Tomato
cannon: color.RGBA{R: 70, G: 130, B: 180, A: 255}, // Steel Blue
},
// Set 25
{
tracks: color.RGBA{R: 135, G: 206, B: 250, A: 255}, // Light Sky Blue
body: color.RGBA{R: 255, G: 105, B: 180, A: 255}, // Hot Pink
cannon: color.RGBA{R: 184, G: 134, B: 11, A: 255}, // Dark Goldenrod
},
// Set 26
{
tracks: color.RGBA{G: 255, A: 255}, // Lime
body: color.RGBA{R: 255, G: 228, B: 225, A: 255}, // Misty Rose
cannon: color.RGBA{R: 128, A: 255}, // Maroon
},
// Set 27
{
tracks: color.RGBA{R: 255, B: 255, A: 255}, // Magenta
body: color.RGBA{G: 100, A: 255}, // Dark Green
cannon: color.RGBA{R: 255, G: 222, B: 173, A: 255}, // Navajo White
},
// Set 28
{
tracks: color.RGBA{R: 138, G: 43, B: 226, A: 255}, // Blue Violet
body: color.RGBA{R: 255, G: 228, B: 181, A: 255}, // Moccasin
cannon: color.RGBA{R: 47, G: 79, B: 79, A: 255}, // Dark Slate Gray
},
// Set 29
{
tracks: color.RGBA{R: 255, G: 182, B: 193, A: 255}, // Light Pink
body: color.RGBA{G: 128, B: 128, A: 255}, // Teal
cannon: color.RGBA{R: 255, G: 255, B: 240, A: 255}, // Ivory
},
// Set 30
{
tracks: color.RGBA{R: 255, G: 127, B: 80, A: 255}, // Coral
body: color.RGBA{R: 25, G: 25, B: 112, A: 255}, // Midnight Blue
cannon: color.RGBA{R: 173, G: 255, B: 47, A: 255}, // Green Yellow
},
// Set 31
{
tracks: color.RGBA{R: 218, G: 112, B: 214, A: 255}, // Orchid
body: color.RGBA{R: 244, G: 164, B: 96, A: 255}, // Sandy Brown
cannon: color.RGBA{R: 255, G: 239, B: 213, A: 255}, // Papaya Whip
},
// Set 32
{
tracks: color.RGBA{G: 250, B: 154, A: 255}, // Medium Spring Green
body: color.RGBA{R: 138, G: 43, B: 226, A: 255}, // Blue Violet
cannon: color.RGBA{R: 255, G: 69, A: 255}, // Orange Red
},
}
}
2024-08-30 19:07:56 +02:00
func handleInput(keyboard []uint8, bullets map[uint32]*Bullet, player *Player, gameMap *GameMap, players map[uint32]*Player) bool {
if !player.local || player.shields <= 0 || player.shields > serverConfig.MaxShields {
return true
2024-08-29 18:48:27 +02:00
}
shoot := false
super := false
// Flags to track movement in each direction
moveUp := false
moveDown := false
moveLeft := false
moveRight := false
if player.keyMap.exit != player.keyMap.shoot {
// Process keyboard input
for key, value := range keyboard {
if value == 0 {
continue
}
if key == player.keyMap.exit {
2024-08-30 19:07:56 +02:00
return false
2024-08-29 18:48:27 +02:00
} else if key == player.keyMap.shoot {
shoot = true
} else if key == player.keyMap.super {
super = true
} else if key == player.keyMap.up {
moveUp = true
} else if key == player.keyMap.down {
moveDown = true
} else if key == player.keyMap.left {
moveLeft = true
} else if key == player.keyMap.right {
moveRight = true
}
}
} else if player.joyStick != nil {
if player.joyStick.NumAxes() > player.joyMap.xAxis+1 &&
player.joyStick.NumAxes() > player.joyMap.yAxis+1 &&
player.joyMap.exitButton != player.joyMap.shootButton {
xAxis := player.joyStick.Axis(player.joyMap.xAxis)
yAxis := player.joyStick.Axis(player.joyMap.yAxis)
2024-08-30 19:07:56 +02:00
moveRight = xAxis > config.JoyStickDeadZone
moveLeft = xAxis < -config.JoyStickDeadZone
moveUp = yAxis > config.JoyStickDeadZone
moveDown = yAxis < -config.JoyStickDeadZone
2024-08-29 18:48:27 +02:00
} else {
moveUp = player.joyStick.Button(player.joyMap.upButton) == sdl.PRESSED
moveDown = player.joyStick.Button(player.joyMap.downButton) == sdl.PRESSED
moveLeft = player.joyStick.Button(player.joyMap.leftButton) == sdl.PRESSED
moveRight = player.joyStick.Button(player.joyMap.rightButton) == sdl.PRESSED
}
if player.joyStick.Button(player.joyMap.exitButton) == sdl.PRESSED {
2024-08-30 19:07:56 +02:00
return false
2024-08-29 18:48:27 +02:00
}
if player.joyStick.Button(player.joyMap.shootButton) == sdl.PRESSED {
shoot = true
}
if player.joyStick.Button(player.joyMap.superButton) == sdl.PRESSED {
super = true
}
}
// Handle shooting after the loop
if shoot {
player.shoot(super, bullets)
}
// Determine player orientation for diagonal movement
if moveUp && moveRight {
player.gameObject.orientation = 4 // Up-Right
} else if moveUp && moveLeft {
player.gameObject.orientation = 5 // Up-Left
} else if moveDown && moveRight {
player.gameObject.orientation = 6 // Down-Right
} else if moveDown && moveLeft {
player.gameObject.orientation = 7 // Down-Left
} else if moveUp {
player.gameObject.orientation = 0 // Up
} else if moveRight {
player.gameObject.orientation = 1 // Right
} else if moveDown {
player.gameObject.orientation = 2 // Down
} else if moveLeft {
player.gameObject.orientation = 3 // Left
}
// Handle movement after the loop
if moveUp || moveDown || moveLeft || moveRight {
if player.tryMove(gameMap, shoot, players) {
2024-09-01 20:13:53 +02:00
if player.energy > serverConfig.MovementCost {
player.energy -= serverConfig.MovementCost
}
2024-08-29 18:48:27 +02:00
}
}
2024-08-30 19:07:56 +02:00
return true
2024-08-29 18:48:27 +02:00
}