From a4753a812e9e71ee09bee87b008815beab97fbf5 Mon Sep 17 00:00:00 2001 From: Mike Bjorge Date: Sat, 10 Feb 2018 17:25:58 -0800 Subject: Add win streak rainbows to the trails. Whenever a player collides with another player's trail, the surviving player gets the dead player's color added to their trail rainbow, indicating their win streak. When a player dies, their streak ends and their trail starts back again as a solid color. --- game.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/game.go b/game.go index 1b9193d..159722e 100644 --- a/game.go +++ b/game.go @@ -141,14 +141,15 @@ type PlayerTrailSegment struct { type Player struct { s *Session - Name string + Name string CreatedAt time.Time Direction PlayerDirection Marker rune Color color.Attribute Pos *Position - Trail []PlayerTrailSegment + Trail []PlayerTrailSegment + WinStreak []color.Attribute score float64 } @@ -624,10 +625,17 @@ func (g *Game) worldString(s *Session) string { pos := player.Pos strWorld[pos.RoundX()+1][pos.RoundY()+1] = colorizer(string(player.Marker)) + // Make the rainbow of trail colors to cycle over + trailColorizers := make([](func(...interface{}) string), len(player.WinStreak)+1) + trailColorizers[0] = colorizer + for i, winColor := range player.WinStreak { + trailColorizers[i+1] = color.New(winColor).SprintFunc() + } + // Load the player's trail into the rune slice - for _, segment := range player.Trail { + for i, segment := range player.Trail { x, y := segment.Pos.RoundX()+1, segment.Pos.RoundY()+1 - strWorld[x][y] = colorizer(string(segment.Marker)) + strWorld[x][y] = trailColorizers[i%len(trailColorizers)](string(segment.Marker)) } } @@ -717,7 +725,7 @@ func (g *Game) Run() { func (g *Game) Update(delta float64) { // We'll use this to make a set of all of the coordinates that are occupied by // trails - trailCoordMap := make(map[string]bool) + trailCoordMap := make(map[string]*Player) // Update player data for player, session := range g.players() { @@ -749,14 +757,15 @@ func (g *Game) Update(delta float64) { for _, seg := range player.Trail { coordStr := fmt.Sprintf("%d,%d", seg.Pos.RoundX(), seg.Pos.RoundY()) - trailCoordMap[coordStr] = true + trailCoordMap[coordStr] = player } } // Check if any players collide with a trail and restart them if so for player, session := range g.players() { playerPos := fmt.Sprintf("%d,%d", player.Pos.RoundX(), player.Pos.RoundY()) - if collided := trailCoordMap[playerPos]; collided { + if otherPlayer, collided := trailCoordMap[playerPos]; collided { + otherPlayer.WinStreak = append(otherPlayer.WinStreak, player.Color) session.StartOver(g.WorldWidth(), g.WorldHeight()) } } -- cgit v1.2.3 From b4d7fd50813b0bf08bc876f3bcfc4dfbb3d6fb29 Mon Sep 17 00:00:00 2001 From: Michael Bjorge Date: Sat, 10 Feb 2018 17:34:20 -0800 Subject: Fix spacing on a comment --- game.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game.go b/game.go index 159722e..3b85a79 100644 --- a/game.go +++ b/game.go @@ -625,7 +625,7 @@ func (g *Game) worldString(s *Session) string { pos := player.Pos strWorld[pos.RoundX()+1][pos.RoundY()+1] = colorizer(string(player.Marker)) - // Make the rainbow of trail colors to cycle over + // Make the rainbow of trail colors to cycle over trailColorizers := make([](func(...interface{}) string), len(player.WinStreak)+1) trailColorizers[0] = colorizer for i, winColor := range player.WinStreak { -- cgit v1.2.3 From b80ab86eaddce4d00ff1c53047394644bfcb08e1 Mon Sep 17 00:00:00 2001 From: Mike Bjorge Date: Tue, 13 Feb 2018 21:48:34 -0800 Subject: Change the segment color when a player collides. When a player collides into another player's segment, change the color of the segment as a way to show how many times players have run into the trail. --- game.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/game.go b/game.go index 3b85a79..7577475 100644 --- a/game.go +++ b/game.go @@ -136,6 +136,7 @@ var playerColorNames = map[color.Attribute]string{ type PlayerTrailSegment struct { Marker rune Pos Position + Color color.Attribute } type Player struct { @@ -148,8 +149,7 @@ type Player struct { Color color.Attribute Pos *Position - Trail []PlayerTrailSegment - WinStreak []color.Attribute + Trail []PlayerTrailSegment score float64 } @@ -178,7 +178,7 @@ func NewPlayer(s *Session, worldWidth, worldHeight int, } func (p *Player) addTrailSegment(pos Position, marker rune) { - segment := PlayerTrailSegment{marker, pos} + segment := PlayerTrailSegment{marker, pos, p.Color} p.Trail = append([]PlayerTrailSegment{segment}, p.Trail...) } @@ -620,22 +620,16 @@ func (g *Game) worldString(s *Session) string { // Load the players into the rune slice for player := range g.players() { - colorizer := color.New(player.Color).SprintFunc() pos := player.Pos + colorizer := color.New(player.Color).SprintFunc() strWorld[pos.RoundX()+1][pos.RoundY()+1] = colorizer(string(player.Marker)) - // Make the rainbow of trail colors to cycle over - trailColorizers := make([](func(...interface{}) string), len(player.WinStreak)+1) - trailColorizers[0] = colorizer - for i, winColor := range player.WinStreak { - trailColorizers[i+1] = color.New(winColor).SprintFunc() - } - // Load the player's trail into the rune slice - for i, segment := range player.Trail { + for _, segment := range player.Trail { x, y := segment.Pos.RoundX()+1, segment.Pos.RoundY()+1 - strWorld[x][y] = trailColorizers[i%len(trailColorizers)](string(segment.Marker)) + colorizer := color.New(segment.Color).SprintFunc() + strWorld[x][y] = colorizer(string(segment.Marker)) } } @@ -725,7 +719,7 @@ func (g *Game) Run() { func (g *Game) Update(delta float64) { // We'll use this to make a set of all of the coordinates that are occupied by // trails - trailCoordMap := make(map[string]*Player) + trailCoordMap := make(map[string]*PlayerTrailSegment) // Update player data for player, session := range g.players() { @@ -755,17 +749,21 @@ func (g *Game) Update(delta float64) { return } - for _, seg := range player.Trail { + // range gives copies, but we need a reference in the trailCoordMap so we + // can modify the Color value if there is a collision, so iterate by index + // instead. + for i := range player.Trail { + seg := &player.Trail[i] coordStr := fmt.Sprintf("%d,%d", seg.Pos.RoundX(), seg.Pos.RoundY()) - trailCoordMap[coordStr] = player + trailCoordMap[coordStr] = seg } } // Check if any players collide with a trail and restart them if so for player, session := range g.players() { playerPos := fmt.Sprintf("%d,%d", player.Pos.RoundX(), player.Pos.RoundY()) - if otherPlayer, collided := trailCoordMap[playerPos]; collided { - otherPlayer.WinStreak = append(otherPlayer.WinStreak, player.Color) + if segment, collided := trailCoordMap[playerPos]; collided { + segment.Color = player.Color session.StartOver(g.WorldWidth(), g.WorldHeight()) } } -- cgit v1.2.3