summaryrefslogtreecommitdiff
path: root/main.go
blob: b2795b02e394695a3d5b9ab1a051b86b50fb9f4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main

import (
	"git.kiefte.eu/lapingvino/clitris/tris"
	//	"image/color"
	"fmt"
	"gioui.org/app"
	"gioui.org/font/gofont"
	"gioui.org/io/key"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/unit"
	"log"
	"os"
	"time"
	//	"gioui.org/text"
	"gioui.org/widget/material"
	"math/rand"
)

func main() {
	go func() {
		w := app.NewWindow(app.Title("Gioco - another block stacking game"), app.Size(unit.Dp(500), unit.Dp(500)))
		if err := loop(w); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()

	app.Main()
}

func loop(w *app.Window) error {
	th := material.NewTheme(gofont.Collection())
	rand.Seed(time.Now().UnixNano()) // to start with truly random pieces
	f := tris.NewField(20, 10)       // the playing field (10x20)
	var floor, topout, harddrop bool // state machine variables to check special situations

	// define outside of game loop to avoid accidental resets of position
	// x, y and rot are the values calculated to feed to Move
	// which then checks collisions and does the wall kicks
	var x, y int
	var rot tris.Rotation

	var level, linescleared, score, dropfrom int

	b := tris.NewBag()
	b, p := b.Pick()
	lev := time.NewTicker(time.Second)
	level = 1
	var debug string
	var ops op.Ops
	for {
		x, y, rot = p.X, p.Y, p.Rot
		select {
		case e := <-w.Events():
			switch e := e.(type) {
			case system.DestroyEvent:
				return e.Err
			case key.Event:
				if e.State == key.Press {
					switch e.Name {
					case key.NameUpArrow: // Up
						debug = "up"
						rot = (p.Rot + 1) % 4
					case key.NameDownArrow: // Down
						debug = "down"
						y = p.Y + 1
						score += 1
					case key.NameRightArrow: // Right
						debug = "right"
						x = p.X + 1
					case key.NameLeftArrow: // Left
						debug = "left"
						x = p.X - 1
					case key.NameEscape, "q", "Q":
						os.Exit(0)
					case "x":
						rot = (p.Rot + 1) % 4
					case "z":
						rot = (p.Rot + 3) % 4
					case "c":
						b, p = b.Swap(p)
						continue
					case " ":
						if !harddrop {
							dropfrom = p.Y
						}
						harddrop = true
					}
				}
			case system.FrameEvent:
				gtx := layout.NewContext(&ops, e)
				level = linescleared/10 + 1
				sdisplay := fmt.Sprintf("level %d\nscore %d\nlines %d\n%s", level, score, linescleared, debug)
				/*
					ppos(0, 0, "Hold (c)")
					npos(3, 0, tris.HoldBox)
					fpos(0, 10, f.Add(p))
					var next tris.Field
					b, next = b.Next(5)
					npos(0, 34, next)
					ppos(1, 42, sscore)
					ppos(3, 42, slevel)
					ppos(5, 42, slines)
				*/
				inset := layout.UniformInset(unit.Dp(10))
				inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
					return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
						layout.Rigid(func(gtx layout.Context) layout.Dimensions {
							return material.H6(th, f.Add(p).String()).Layout(gtx)
						}),
						layout.Rigid(func(gtx layout.Context) layout.Dimensions {
							return material.H6(th, sdisplay).Layout(gtx)
						}),
					)
				})
				op.InvalidateOp{}.Add(gtx.Ops)
				e.Frame(gtx.Ops)
			}
		case <-lev.C:
			y = p.Y + 1
			lev.Reset(time.Second * 100 / (100 * time.Duration(level)))
		default:
			if harddrop {
				y = p.Y + 1
			}
		}
		debug = "move"
		p, floor, topout = p.Move(f, rot, x, y) // Check if the piece can move, then do it and communicate back for housekeeping

		if harddrop && floor {
			score += 2 * (p.Y - dropfrom)
			harddrop = false
		}
		// Give some time before actually locking in to enable tucks
		// This code runs when the time is over
		if floor && p.Lock.Add(tris.LockDelay).Before(time.Now()) {
			var l int
			f = f.Add(p)
			l, f = f.Lines() // count and remove full lines
			if l > 0 {
				linescleared += l
				score += 40 * level * l * l
			}
			b, p = b.Pick() // ... and pick a new piece from the bag
		}
		// when not touching a piece or floor below yet, reset lock delay
		if !floor {
			p.Lock = time.Now()
		}

		if topout {
			return fmt.Errorf("GAME OVER")
		}
	}
	return nil
}