Landing a piece

This commit is contained in:
Hajime Hoshi 2013-12-20 03:03:35 +09:00
parent 2ac027c452
commit 14994d57f2
2 changed files with 35 additions and 12 deletions

View File

@ -44,6 +44,10 @@ func (f *Field) MovePieceToRight(piece *Piece, x, y int, angle Angle) int {
return x + 1 return x + 1
} }
func (f *Field) PieceDroppable(piece *Piece, x, y int, angle Angle) bool {
return !f.collides(piece, x, y+1, angle)
}
func (f *Field) DropPiece(piece *Piece, x, y int, angle Angle) int { func (f *Field) DropPiece(piece *Piece, x, y int, angle Angle) int {
if f.collides(piece, x, y+1, angle) { if f.collides(piece, x, y+1, angle) {
return y return y

View File

@ -21,6 +21,7 @@ type GameScene struct {
currentPieceY int currentPieceY int
currentPieceAngle Angle currentPieceAngle Angle
nextPiece *Piece nextPiece *Piece
landingCount int
} }
func NewGameScene() *GameScene { func NewGameScene() *GameScene {
@ -42,6 +43,8 @@ func (s *GameScene) choosePiece() *Piece {
} }
func (s *GameScene) Update(state *GameState) { func (s *GameScene) Update(state *GameState) {
const maxLandingCount = 30
if s.currentPiece == nil { if s.currentPiece == nil {
s.currentPiece = s.choosePiece() s.currentPiece = s.choosePiece()
x, y := s.currentPiece.InitialPosition() x, y := s.currentPiece.InitialPosition()
@ -49,28 +52,44 @@ func (s *GameScene) Update(state *GameState) {
s.currentPieceY = y s.currentPieceY = y
s.currentPieceAngle = Angle0 s.currentPieceAngle = Angle0
} }
piece := s.currentPiece
x := s.currentPieceX
y := s.currentPieceY
angle := s.currentPieceAngle
moved := false
if state.Input.StateForKey(ui.KeySpace) == 1 { if state.Input.StateForKey(ui.KeySpace) == 1 {
s.currentPieceAngle = s.field.RotatePieceRight( orig := angle
s.currentPiece, s.currentPieceX, s.currentPieceY, s.currentPieceAngle = s.field.RotatePieceRight(piece, x, y, angle)
s.currentPieceAngle) moved = orig != angle
} }
if l := state.Input.StateForKey(ui.KeyLeft); if l := state.Input.StateForKey(ui.KeyLeft);
l == 1 || (10 <= l && l % 2 == 0) { l == 1 || (10 <= l && l % 2 == 0) {
s.currentPieceX = s.field.MovePieceToLeft( orig := x
s.currentPiece, s.currentPieceX, s.currentPieceY, s.currentPieceX = s.field.MovePieceToLeft(piece, x, y, angle)
s.currentPieceAngle) moved = orig != x
} }
if r := state.Input.StateForKey(ui.KeyRight); if r := state.Input.StateForKey(ui.KeyRight);
r == 1 || (10 <= r && r % 2 == 0) { r == 1 || (10 <= r && r % 2 == 0) {
s.currentPieceX = s.field.MovePieceToRight( orig := y
s.currentPiece, s.currentPieceX, s.currentPieceY, s.currentPieceX = s.field.MovePieceToRight(piece, x, y, angle)
s.currentPieceAngle) moved = orig != y
} }
if d := state.Input.StateForKey(ui.KeyDown); if d := state.Input.StateForKey(ui.KeyDown);
(d - 1) % 2 == 0 { (d - 1) % 2 == 0 {
s.currentPieceY = s.field.DropPiece( orig := y
s.currentPiece, s.currentPieceX, s.currentPieceY, s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
s.currentPieceAngle) moved = orig != y
}
if moved {
s.landingCount = 0
} else if !s.field.PieceDroppable(piece, x, y, angle) {
s.landingCount++
if maxLandingCount <= s.landingCount {
print("landing!\n")
// TODO: implement
s.currentPiece = nil
s.landingCount = 0
}
} }
} }