bombsquad-plugin-manager/plugins/minigames/snake.py

325 lines
10 KiB
Python
Raw Normal View History

2024-01-17 23:09:18 +03:00
# Porting to api 8 made easier by baport.(https://github.com/bombsquad-community/baport)
2023-05-15 10:58:00 +00:00
# snake
2023-05-15 15:56:45 +05:30
# Released under the MIT License. See LICENSE for details.
#
"""Snake game by SEBASTIAN2059"""
2024-01-17 23:09:18 +03:00
# ba_meta require api 8
2023-05-15 15:56:45 +05:30
# (see https://ballistica.net/wiki/meta-tag-system)
from __future__ import annotations
from typing import TYPE_CHECKING
2024-01-17 23:09:18 +03:00
import babase
import bauiv1 as bui
import bascenev1 as bs
from bascenev1lib.actor.playerspaz import PlayerSpaz
from bascenev1lib.actor.scoreboard import Scoreboard
from bascenev1lib.actor import bomb as stdbomb
2023-05-15 15:56:45 +05:30
if TYPE_CHECKING:
from typing import Any, Type, List, Dict, Tuple, Union, Sequence, Optional
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
class ScoreMessage:
"""It will help us with the scores."""
2023-05-15 10:58:00 +00:00
def __init__(self, player: Player):
2023-05-15 15:56:45 +05:30
self.player = player
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
def getplayer(self):
return self.player
2023-05-15 10:58:00 +00:00
2024-01-17 23:09:18 +03:00
class Player(bs.Player['Team']):
2023-05-15 15:56:45 +05:30
"""Our player type for this game."""
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
def __init__(self) -> None:
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
self.mines = []
self.actived = None
2023-05-15 10:58:00 +00:00
2024-01-17 23:09:18 +03:00
class Team(bs.Team[Player]):
2023-05-15 15:56:45 +05:30
"""Our team type for this game."""
def __init__(self) -> None:
self.score = 0
2023-05-15 10:58:00 +00:00
2024-01-17 23:09:18 +03:00
lang = bs.app.lang.language
2023-05-15 15:56:45 +05:30
if lang == 'Spanish':
description = 'Sobrevive a un número determinado de minas para ganar.'
join_description = 'Corre y no te dejes matar.'
view_description = 'sobrevive ${ARG1} minas'
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
else:
description = 'Survive a set number of mines to win.'
join_description = "Run and don't get killed."
view_description = 'survive ${ARG1} mines'
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
class Custom_Mine(stdbomb.Bomb):
"""Custom a mine :)"""
2023-05-15 10:58:00 +00:00
def __init__(self, position, source_player):
stdbomb.Bomb.__init__(self, position=position, bomb_type='land_mine',
source_player=source_player)
def handlemessage(self, msg: Any) -> Any:
2024-01-17 23:09:18 +03:00
if isinstance(msg, bs.HitMessage):
2023-05-15 15:56:45 +05:30
return
else:
super().handlemessage(msg)
2024-01-17 23:09:18 +03:00
# ba_meta export bascenev1.GameActivity
2023-05-15 10:58:00 +00:00
2024-01-17 23:09:18 +03:00
class SnakeGame(bs.TeamGameActivity[Player, Team]):
2023-05-15 15:56:45 +05:30
"""A game type based on acquiring kills."""
name = 'Snake'
description = description
# Print messages when players die since it matters here.
announce_player_deaths = True
@classmethod
def get_available_settings(
2024-01-17 23:09:18 +03:00
cls, sessiontype: Type[bs.Session]) -> List[babase.Setting]:
2023-05-15 15:56:45 +05:30
settings = [
2024-01-17 23:09:18 +03:00
bs.IntSetting(
2023-05-15 15:56:45 +05:30
'Score to Win',
min_value=40,
default=80,
increment=5,
),
2024-01-17 23:09:18 +03:00
bs.IntChoiceSetting(
2023-05-15 15:56:45 +05:30
'Time Limit',
choices=[
('None', 0),
('1 Minute', 60),
('2 Minutes', 120),
('5 Minutes', 300),
('10 Minutes', 600),
('20 Minutes', 1200),
],
default=0,
),
2024-01-17 23:09:18 +03:00
bs.FloatChoiceSetting(
2023-05-15 15:56:45 +05:30
'Respawn Times',
choices=[
('Shorter', 0.25),
('Short', 0.5),
('Normal', 1.0),
('Long', 2.0),
('Longer', 4.0),
],
default=1.0,
),
2024-01-17 23:09:18 +03:00
bs.BoolSetting('Epic Mode', default=False),
2023-05-15 15:56:45 +05:30
]
return settings
@classmethod
2024-01-17 23:09:18 +03:00
def supports_session_type(cls, sessiontype: Type[bs.Session]) -> bool:
return (issubclass(sessiontype, bs.DualTeamSession)
or issubclass(sessiontype, bs.FreeForAllSession))
2023-05-15 15:56:45 +05:30
@classmethod
2024-01-17 23:09:18 +03:00
def get_supported_maps(cls, sessiontype: Type[bs.Session]) -> List[str]:
return bs.app.classic.getmaps('melee')
2023-05-15 15:56:45 +05:30
def __init__(self, settings: dict):
super().__init__(settings)
self._scoreboard = Scoreboard()
self._score_to_win: Optional[int] = None
2024-01-17 23:09:18 +03:00
self._dingsound = bs.getsound('dingSmall')
2023-05-15 10:58:00 +00:00
2024-01-17 23:09:18 +03:00
self._beep_1_sound = bs.getsound('raceBeep1')
self._beep_2_sound = bs.getsound('raceBeep2')
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
self._epic_mode = bool(settings['Epic Mode'])
self._kills_to_win_per_player = int(
settings['Score to Win'])
self._time_limit = float(settings['Time Limit'])
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
self._started = False
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
# Base class overrides.
self.slow_motion = self._epic_mode
2024-01-17 23:09:18 +03:00
self.default_music = (bs.MusicType.EPIC if self._epic_mode else
bs.MusicType.TO_THE_DEATH)
2023-05-15 15:56:45 +05:30
def get_instance_description(self) -> Union[str, Sequence]:
return join_description
def get_instance_description_short(self) -> Union[str, Sequence]:
return view_description, self._score_to_win
def on_team_join(self, team: Team) -> None:
if self.has_begun():
self._update_scoreboard()
def on_begin(self) -> None:
super().on_begin()
self.setup_standard_time_limit(self._time_limit)
2023-05-15 10:58:00 +00:00
# self.setup_standard_powerup_drops()
2023-05-15 15:56:45 +05:30
# Base kills needed to win on the size of the largest team.
self._score_to_win = (self._kills_to_win_per_player *
max(1, max(len(t.players) for t in self.teams)))
self._update_scoreboard()
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
if self.slow_motion:
t_scale = 0.4
light_y = 50
else:
t_scale = 1.0
light_y = 150
lstart = 7.1 * t_scale
inc = 1.25 * t_scale
2024-01-17 23:09:18 +03:00
bs.timer(lstart, self._do_light_1)
bs.timer(lstart + inc, self._do_light_2)
bs.timer(lstart + 2 * inc, self._do_light_3)
bs.timer(lstart + 3 * inc, self._start_race)
2023-05-15 15:56:45 +05:30
self._start_lights = []
for i in range(4):
2024-01-17 23:09:18 +03:00
lnub = bs.newnode('image',
2023-05-15 15:56:45 +05:30
attrs={
2024-01-17 23:09:18 +03:00
'texture': bs.gettexture('nub'),
2023-05-15 15:56:45 +05:30
'opacity': 1.0,
'absolute_scale': True,
'position': (-75 + i * 50, light_y),
'scale': (50, 50),
'attach': 'center'
})
2024-01-17 23:09:18 +03:00
bs.animate(
2023-05-15 15:56:45 +05:30
lnub, 'opacity', {
4.0 * t_scale: 0,
5.0 * t_scale: 1.0,
12.0 * t_scale: 1.0,
12.5 * t_scale: 0.0
})
2024-01-17 23:09:18 +03:00
bs.timer(13.0 * t_scale, lnub.delete)
2023-05-15 15:56:45 +05:30
self._start_lights.append(lnub)
self._start_lights[0].color = (0.2, 0, 0)
self._start_lights[1].color = (0.2, 0, 0)
self._start_lights[2].color = (0.2, 0.05, 0)
self._start_lights[3].color = (0.0, 0.3, 0)
def _do_light_1(self) -> None:
assert self._start_lights is not None
self._start_lights[0].color = (1.0, 0, 0)
2024-01-17 23:09:18 +03:00
self._beep_1_sound.play()
2023-05-15 15:56:45 +05:30
def _do_light_2(self) -> None:
assert self._start_lights is not None
self._start_lights[1].color = (1.0, 0, 0)
2024-01-17 23:09:18 +03:00
self._beep_1_sound.play()
2023-05-15 15:56:45 +05:30
def _do_light_3(self) -> None:
assert self._start_lights is not None
self._start_lights[2].color = (1.0, 0.3, 0)
2024-01-17 23:09:18 +03:00
self._beep_1_sound.play()
2023-05-15 15:56:45 +05:30
def _start_race(self) -> None:
assert self._start_lights is not None
self._start_lights[3].color = (0.0, 1.0, 0)
2024-01-17 23:09:18 +03:00
self._beep_2_sound.play()
2023-05-15 15:56:45 +05:30
self._started = True
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
for player in self.players:
self.generate_mines(player)
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
# overriding the default character spawning..
2024-01-17 23:09:18 +03:00
def spawn_player(self, player: Player) -> bs.Actor:
2023-05-15 15:56:45 +05:30
spaz = self.spawn_player_spaz(player)
# Let's reconnect this player's controls to this
# spaz but *without* the ability to attack or pick stuff up.
spaz.connect_controls_to_player(enable_punch=False,
enable_bomb=False,
enable_pickup=False)
# Also lets have them make some noise when they die.
spaz.play_big_death_sound = True
if self._started:
self.generate_mines(player)
return spaz
2023-05-15 10:58:00 +00:00
def generate_mines(self, player: Player):
2023-05-15 15:56:45 +05:30
try:
2024-01-17 23:09:18 +03:00
player.actived = bs.Timer(0.5, babase.Call(self.spawn_mine, player), repeat=True)
2023-05-15 15:56:45 +05:30
except Exception as e:
2023-05-15 10:58:00 +00:00
print('Exception -> ' + str(e))
def spawn_mine(self, player: Player):
2023-05-15 15:56:45 +05:30
if player.team.score >= self._score_to_win:
return
pos = player.actor.node.position
# mine = stdbomb.Bomb(position=(pos[0], pos[1] + 2.0, pos[2]),
2023-05-15 10:58:00 +00:00
# velocity=(0, 0, 0),
# bomb_type='land_mine',
# #blast_radius=,
# source_player=player.actor.source_player,
# owner=player.actor.node).autoretain()
2023-05-15 15:56:45 +05:30
mine = Custom_Mine(position=(pos[0], pos[1] + 2.0, pos[2]),
2023-05-15 10:58:00 +00:00
source_player=player.actor.source_player)
2023-05-15 15:56:45 +05:30
def arm():
mine.arm()
2024-01-17 23:09:18 +03:00
bs.timer(0.5, arm)
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
player.mines.append(mine)
if len(player.mines) > 15:
for m in player.mines:
try:
m.node.delete()
except Exception:
pass
player.mines.remove(m)
break
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
self.handlemessage(ScoreMessage(player))
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
def handlemessage(self, msg: Any) -> Any:
2024-01-17 23:09:18 +03:00
if isinstance(msg, bs.PlayerDiedMessage):
2023-05-15 15:56:45 +05:30
# Augment standard behavior.
super().handlemessage(msg)
player = msg.getplayer(Player)
self.respawn_player(player)
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
player.actived = None
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
elif isinstance(msg, ScoreMessage):
player = msg.getplayer()
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
player.team.score += 1
self._update_scoreboard()
2023-05-15 10:58:00 +00:00
2023-05-15 15:56:45 +05:30
assert self._score_to_win is not None
if any(team.score >= self._score_to_win for team in self.teams):
2024-01-17 23:09:18 +03:00
self.end_game() # bs.timer(0.5, self.end_game)
2023-05-15 15:56:45 +05:30
else:
return super().handlemessage(msg)
return None
def _update_scoreboard(self) -> None:
for team in self.teams:
self._scoreboard.set_team_value(team, team.score,
self._score_to_win)
def end_game(self) -> None:
2024-01-17 23:09:18 +03:00
results = bs.GameResults()
2023-05-15 15:56:45 +05:30
for team in self.teams:
results.set_team_score(team, team.score)
self.end(results=results)