From b3c789303b6cb2ac4979138d501ba9c2cb951e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Sun, 1 Sep 2024 09:12:17 +0200 Subject: [PATCH] Fix bullets offsets Add keyboard offset --- main.go | 2 ++ player.go | 55 ++++++++++++++++++++----------------------------------- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/main.go b/main.go index a671673..0d37e4e 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,7 @@ type Config struct { DoJoyStickPlayers bool `json:"do_joystick_players"` DoKeymapPlayer bool `json:"do_keymap_player"` RecentlyDugBlocksClearInterval uint16 `json:"recently_dug_blocks_clear_interval"` + KeyBindOffset uint16 `json:"key_bind_offset"` ServerConfig ServerConfig `json:"server_config"` } @@ -81,6 +82,7 @@ func loadOrCreateConfig(filename string) (*Config, error) { DoJoyStickPlayers: true, DoKeymapPlayer: true, RecentlyDugBlocksClearInterval: 20, + KeyBindOffset: 0, ServerConfig: ServerConfig{ MapWidth: 1000, MapHeight: 1000, diff --git a/player.go b/player.go index dba61c1..1f6c494 100644 --- a/player.go +++ b/player.go @@ -290,6 +290,11 @@ func (player *Player) getRGBAColor(colorIndex uint8, format *sdl.PixelFormat) ui } +type Point struct { + X int32 + Y int32 +} + func (player *Player) shoot(super bool, bullets map[uint32]*Bullet) { if (super && (player.energy <= serverConfig.SuperShotCost || player.ammunition < serverConfig.MaxAmmunition)) || (!super && (player.energy <= serverConfig.NormalShotCost || player.ammunition < 1)) { return @@ -297,41 +302,21 @@ func (player *Player) shoot(super bool, bullets map[uint32]*Bullet) { if player.shootCooldown == 0 { var shootX, shootY int32 - switch player.gameObject.orientation { - case 0: // Up - shootY = player.gameObject.baseRect.Y - 1 - shootX = player.gameObject.baseRect.X + 2 - break - case 1: // Right - shootY = player.gameObject.baseRect.Y + 3 - shootX = player.gameObject.baseRect.X + 8 - break - case 2: // Down - shootX = player.gameObject.baseRect.X + 2 - shootY = player.gameObject.baseRect.Y + 8 - break - case 3: // Left - shootY = player.gameObject.baseRect.Y + 3 - shootX = player.gameObject.baseRect.X - 2 - break - case 4: // Up-Right - shootY = player.gameObject.baseRect.Y - shootX = player.gameObject.baseRect.X + 5 - break - case 5: // Up-Left - shootY = player.gameObject.baseRect.Y - shootX = player.gameObject.baseRect.X - 1 - break - case 6: // Down-Right - shootY = player.gameObject.baseRect.Y + 5 - shootX = player.gameObject.baseRect.X + 5 - break - case 7: // Down-Left - shootY = player.gameObject.baseRect.Y + 5 - shootX = player.gameObject.baseRect.X - 1 - break + offsets := []Point{ + {X: 2, Y: 0}, // 0: Up + {X: 6, Y: 2}, // 1: Right + {X: 2, Y: 6}, // 2: Down + {X: 0, Y: 2}, // 3: Left + {X: 6, Y: 0}, // 4: Up-Right + {X: 0, Y: 0}, // 5: Up-Left + {X: 5, Y: 5}, // 6: Down-Right + {X: 0, Y: 6}, // 7: Down-Left } + // Access the offset based on the player's orientation + shootX = player.gameObject.baseRect.X + offsets[player.gameObject.orientation].X + shootY = player.gameObject.baseRect.Y + offsets[player.gameObject.orientation].Y + player.shootCooldown = serverConfig.ShootCooldown // Set cooldown and decrease energy if super { @@ -446,7 +431,7 @@ func createPlayers(amount uint8, playerColors []PlayerColors, keyMaps []KeyMap, var joyStick *sdl.Joystick if (config.DoAllKeymapsPlayers && i <= uint8(len(keyMaps))) || (config.DoKeymapPlayer && i == 0) || (uint8(joyStickCount) <= i) { - keyMap = keyMaps[addedKeyboardPlayers] + keyMap = keyMaps[(addedKeyboardPlayers+int(config.KeyBindOffset))%(len(keyMaps)-1)] addedKeyboardPlayers++ } else { joyStickIndex := i - uint8(addedKeyboardPlayers) @@ -1416,7 +1401,7 @@ func handleConnectionClient(conn *net.TCPConn, players map[uint32]*Player, bases } gameMap.createGameMap(false) lastPlayerID = serverInfo.PlayerID - createPlayer(true, playerColors[serverInfo.PlayerColorID], conn, keyMaps[0], JoyMap{}, nil, gameMap, players, bases) + createPlayer(true, playerColors[serverInfo.PlayerColorID], conn, keyMaps[config.KeyBindOffset], JoyMap{}, nil, gameMap, players, bases) playersMutex.RLock() player = players[serverInfo.PlayerID] playersMutex.RUnlock()