viertris

package
v0.0.0-...-a8bc9d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

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

func NewUint64Text

func NewUint64Text(prefix string, f *render.Font, val *uint64) *render.Text

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 (gb *GameBoard) CheckIfTileIsPlaced() (placed bool)

func (*GameBoard) ClearFullLines

func (gb *GameBoard) ClearFullLines(y int) (cleared bool)

func (*GameBoard) IsOffscreen

func (gb *GameBoard) IsOffscreen(x, y int) bool

func (*GameBoard) IsSet

func (gb *GameBoard) IsSet(x, y int) bool

func (*GameBoard) PlaceActiveTile

func (gb *GameBoard) PlaceActiveTile() (clears uint32)

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) Draw

func (gs *GameState) Draw(buff draw.Image, _ float64, _ float64)

func (*GameState) GetDims

func (gs *GameState) GetDims() (int, int)

func (*GameState) SetTrisActive

func (gs *GameState) SetTrisActive(kind TrisKind) (gameOver bool)

type Rotation

type Rotation uint8
const (
	Rotation0   Rotation = iota
	Rotation90  Rotation = iota
	Rotation180 Rotation = iota
	Rotation270 Rotation = iota
	RotationMax Rotation = iota
)

func (Rotation) RotateLeft

func (r Rotation) RotateLeft() Rotation

func (Rotation) RotateRight

func (r Rotation) RotateRight() Rotation

type TrisKind

type TrisKind uint8
const (
	KindNone   TrisKind = iota
	KindT      TrisKind = iota
	KindLine   TrisKind = iota
	KindSquare TrisKind = iota
	KindZ      TrisKind = iota
	KindS      TrisKind = iota
	KindL      TrisKind = iota
	KindJ      TrisKind = iota
	KindFinal  TrisKind = iota
)

func RandomKind

func RandomKind() TrisKind

func (TrisKind) Color

func (tk TrisKind) Color() color.RGBA

func (TrisKind) Offsets

func (tk TrisKind) Offsets() [4][2]int8

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL