Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Scene = scene.Scene{ Start: func(ctx *scene.Context) { cfg, ok := ctx.SceneInput.(GameConfig) if !ok { fmt.Println("no game config passed to scene") } st := NewGameState(ctx, cfg) ctx.DrawStack.Draw(st) var keyRepeatDuration = 70 * time.Millisecond var fallDuration = 700 * time.Millisecond dropAt := time.Now().Add(fallDuration) st.SetTrisActive(RandomKind()) st.NextTris = RandomKind() paused := false gameOver := false canSwap := true keyRepeat := time.Now().Add(keyRepeatDuration) ctx.EventHandler.GlobalBind(event.Enter, func(_ event.CID, payload interface{}) int { if paused || gameOver { return 0 } tileDone := false if time.Now().After(dropAt) { tileDone = st.ActiveTris.MoveDown() dropAt = time.Now().Add(fallDuration) } else if time.Now().After(keyRepeat) { if ctx.KeyState.IsDown(key.A) { st.ActiveTris.MoveLeft() keyRepeat = time.Now().Add(keyRepeatDuration) } else if ctx.KeyState.IsDown(key.D) { st.ActiveTris.MoveRight() keyRepeat = time.Now().Add(keyRepeatDuration) } else if ctx.KeyState.IsDown(key.S) { tileDone = st.ActiveTris.MoveDown() dropAt = time.Now().Add(fallDuration) keyRepeat = time.Now().Add(keyRepeatDuration / 2) } else if ctx.KeyState.IsDown(key.Q) { st.ActiveTris.RotateLeft() keyRepeat = time.Now().Add(keyRepeatDuration * 2) } else if ctx.KeyState.IsDown(key.E) { st.ActiveTris.RotateRight() keyRepeat = time.Now().Add(keyRepeatDuration * 2) } else if canSwap && ctx.KeyState.IsDown(key.Enter) { canSwap = false if st.StoredTris == KindNone { st.StoredTris = st.ActiveTris.TrisKind st.SetTrisActive(st.NextTris) st.NextTris = RandomKind() } else { toStore := st.ActiveTris.TrisKind st.SetTrisActive(st.StoredTris) st.StoredTris = toStore } } } if tileDone { canSwap = true clears := st.GameBoard.PlaceActiveTile() collidedAtStart := st.SetTrisActive(st.NextTris) if collidedAtStart { gameOver = true for y, row := range st.Set { for x, kind := range row { if kind != KindNone { st.Set[y][x] = KindFinal } } } ctx.EventHandler.GlobalBind(key.Down+key.R, func(c event.CID, i interface{}) int { ctx.Window.GoToScene(scenes.Viertris) return 0 }) } st.NextTris = RandomKind() st.Clears += uint64(clears) switch clears { case 1: st.Score += 1 case 2: st.Score += 4 case 3: st.Score += 9 case 4: st.Score += 16 } lastLevel := st.Level st.Level = st.Clears / 10 if lastLevel < st.Level { fallDuration = time.Duration(float64(fallDuration) * .85) } } return 0 }) var dropAtDelta time.Duration ctx.EventHandler.GlobalBind(key.Down+key.P, func(c event.CID, i interface{}) int { paused = !paused if paused { dropAtDelta = time.Until(dropAt) } else { dropAt = time.Now().Add(dropAtDelta) } return 0 }) ctx.EventHandler.GlobalBind(key.Down+key.Spacebar, func(c event.CID, i interface{}) int { var tileDone bool for !tileDone { tileDone = st.ActiveTris.MoveDown() } dropAt = time.Now() return 0 }) if buildinfo.AreCheatsEnabled() { ctx.EventHandler.GlobalBind(key.Down+key.L, func(c event.CID, i interface{}) int { st.ActiveTris.TrisKind = KindLine return 0 }) ctx.EventHandler.GlobalBind(key.Down+key.One, func(c event.CID, i interface{}) int { st.Clears += 10 st.Level = st.Clears / 10 fallDuration = time.Duration(float64(fallDuration) * .85) return 0 }) } }, }
TODO: menu scene to start game, pick level, pick settings
Functions ¶
Types ¶
type ActiveTris ¶
type ActiveTris struct { Board *GameBoard Rotation X BoardDimension Y BoardDimension TrisKind }
func (*ActiveTris) MoveDown ¶
func (at *ActiveTris) MoveDown() bool
func (*ActiveTris) MoveLeft ¶
func (at *ActiveTris) MoveLeft()
func (*ActiveTris) MoveRight ¶
func (at *ActiveTris) MoveRight()
func (*ActiveTris) Offsets ¶
func (at *ActiveTris) Offsets() [4][2]int8
Offsets on an ActiveTris takes into account rotation
func (*ActiveTris) RotateLeft ¶
func (at *ActiveTris) RotateLeft()
func (*ActiveTris) RotateRight ¶
func (at *ActiveTris) RotateRight()
func (*ActiveTris) TestOffsets ¶
func (at *ActiveTris) TestOffsets(rotation Rotation) [4][2]int8
type BoardDimension ¶
type BoardDimension uint8
type GameBoard ¶
type GameBoard struct { Width BoardDimension Height BoardDimension // Unexpected! Y x X! Because that makes it easier to clear lines! Set [][]TrisKind ActiveTris ActiveTris }
func NewGameBoard ¶
func NewGameBoard(cfg GameConfig) GameBoard
func (*GameBoard) CheckIfTileIsPlaced ¶
func (*GameBoard) ClearFullLines ¶
func (*GameBoard) IsOffscreen ¶
func (*GameBoard) PlaceActiveTile ¶
type GameConfig ¶
type GameConfig struct { }
type GameState ¶
type GameState struct { render.LayeredPoint GameBoard GameConfig Clears uint64 Score uint64 Level uint64 ClearsText *render.Text LevelText *render.Text ScoreText *render.Text NextTris TrisKind StoredTris TrisKind // contains filtered or unexported fields }
func NewGameState ¶
func NewGameState(ctx *scene.Context, cfg GameConfig) *GameState
func (*GameState) SetTrisActive ¶
Click to show internal directories.
Click to hide internal directories.