teststrecke-game/scripts/gamestate.gd

135 lines
2.9 KiB
GDScript3
Raw Permalink Normal View History

2024-11-03 18:16:30 +00:00
extends Node
#var playerkeys: Array[int] = []
var players: Array[Player] = []
2024-11-09 11:44:44 +00:00
var last_players: Array[Player] = []
2024-11-03 18:16:30 +00:00
var userinput_prefix="inputP"
var time_elapsed=0
2024-11-09 12:47:13 +00:00
var map_scene_name=""
2025-01-01 21:07:02 +00:00
var map_scene_path_prefix="res://scenes/"
var map_scene_name_prefix="map_"
2024-11-09 12:47:13 +00:00
var map_scene_name_postfix=".tscn"
2024-11-09 23:45:22 +00:00
var rounds=3
2024-11-09 12:47:13 +00:00
var colorwheelstep:float=0.3
2024-11-09 12:58:22 +00:00
func getRounds():
return rounds
func setRounds(r):
2024-11-09 13:27:34 +00:00
print("Rounds set to "+str(r))
2024-11-09 12:58:22 +00:00
rounds=r
2024-11-09 23:45:22 +00:00
func getSelectedMap(): #return only short map name "00"
2024-11-09 12:47:13 +00:00
return map_scene_name
2025-01-01 21:07:02 +00:00
func getSelectedMapName():
return map_scene_name_prefix+map_scene_name
2024-11-09 12:47:13 +00:00
2024-11-09 23:45:22 +00:00
func getSelectedMapSceneName(): #returns full scene path "res://scenes/map_00.tscn"
2025-01-01 21:07:02 +00:00
return map_scene_path_prefix+map_scene_name_prefix+map_scene_name+map_scene_name_postfix
2024-11-09 23:45:22 +00:00
2024-11-09 12:47:13 +00:00
func setSelectedMap(mapname):
2024-11-09 23:45:22 +00:00
map_scene_name=mapname
2024-11-09 12:47:13 +00:00
2024-11-09 11:44:44 +00:00
func checkColoriFree(i):
var free=true
for p in players:
if p.colori==i: #already taken
free=false
return free
func setColorwheelstepByPlayernum(playernum:int):
if playernum==2:
colorwheelstep=0.5
elif playernum==3:
colorwheelstep=0.33
elif playernum==4:
colorwheelstep=0.25
elif playernum==5:
colorwheelstep=0.4
elif playernum==6:
colorwheelstep=0.16
elif playernum==7:
colorwheelstep=1.0/7*4
elif playernum==8:
colorwheelstep=1.0/8*3
else:
colorwheelstep=0.3
2024-11-09 11:44:44 +00:00
2024-11-03 18:16:30 +00:00
func addPlayer(key:int):
2024-11-09 11:07:59 +00:00
#return -1 if player already exists with this key. else return id of player added
2024-11-09 11:44:44 +00:00
if not getPlayerkeys().has(key): #key is not yet registered
var prefered_colori=0
var setrank=((randf()-0.5)/10.0)+0.5
2024-11-09 11:44:44 +00:00
for p in last_players:
if p.inputkey==key: #key was used in last round
prefered_colori=p.colori
setrank=p.rank
print("Player found in last_players. rank="+str(setrank)+" colori="+str(prefered_colori))
2024-11-09 11:44:44 +00:00
#check if color is free, else assign next free
while not checkColoriFree(prefered_colori):
prefered_colori+=1
var newplayer: Player = Player.new(key,prefered_colori)
newplayer.rank=setrank
players.append(newplayer)
2024-11-09 11:44:44 +00:00
return prefered_colori
2024-11-09 11:07:59 +00:00
else:
return -1
2024-11-03 18:16:30 +00:00
func removeAllPlayers():
2024-11-09 11:44:44 +00:00
last_players=players
2024-11-03 18:16:30 +00:00
players=[]
2024-11-04 18:55:13 +00:00
2024-11-03 18:16:30 +00:00
func getPlayerkeys() -> Array[int]:
var playerkeys: Array[int] = []
for player in players:
playerkeys.append(player.inputkey)
return playerkeys
2024-11-04 18:55:13 +00:00
func getPlayers() -> Array[Player]:
return players
2024-11-03 18:16:30 +00:00
class Player:
var inputkey:int
var color:Color
2024-11-09 11:44:44 +00:00
var colori
var rank:float #0-1, nonlinear, higher when player wins often
2024-11-03 18:16:30 +00:00
2024-11-09 11:44:44 +00:00
func _init(key:int, pcolori:int=-1):
2024-11-03 18:16:30 +00:00
inputkey=key
2024-11-09 11:44:44 +00:00
colori=pcolori
2024-11-04 20:56:07 +00:00
var sat = 1.0
2024-11-12 17:26:37 +00:00
var val = 1.0 #1 is medium gray
2024-11-03 18:16:30 +00:00
2024-11-09 11:44:44 +00:00
if colori<0:
2024-11-03 18:16:30 +00:00
color=Color.from_hsv(randf_range(0.0,1.0), sat,val,1.0)
else:
color=Color.from_hsv(fmod(Gamestate.colorwheelstep*colori,1.0), sat,val,1.0)
2024-11-03 18:16:30 +00:00
func startGame():
for player in players:
player.setRunning(true)
func addTimeElapsed(delta):
time_elapsed+=delta
func getTimeElapsed():
return time_elapsed
2024-11-09 10:54:56 +00:00
func resetTimeElapsed():
time_elapsed=0