mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-03 14:34:26 +01:00
blocks: Introduce level and gravity
This commit is contained in:
parent
bb0dfcfc3e
commit
0346e81161
@ -75,22 +75,23 @@ func (f *Field) RotatePieceRight(piece *Piece, x, y int, angle Angle) Angle {
|
||||
return angle.RotateRight()
|
||||
}
|
||||
|
||||
func (f *Field) AbsorbPiece(piece *Piece, x, y int, angle Angle) {
|
||||
func (f *Field) AbsorbPiece(piece *Piece, x, y int, angle Angle) int {
|
||||
piece.AbsorbInto(f, x, y, angle)
|
||||
f.Flush()
|
||||
return f.Flush()
|
||||
}
|
||||
|
||||
func (f *Field) setBlock(x, y int, blockType BlockType) {
|
||||
f.blocks[x][y] = blockType
|
||||
}
|
||||
|
||||
func (f *Field) Flush() {
|
||||
func (f *Field) Flush() int {
|
||||
flushedLineNum := 0
|
||||
for j := fieldBlockNumY - 1; 0 <= j; j-- {
|
||||
if f.flushLine(j + flushedLineNum) {
|
||||
flushedLineNum++
|
||||
}
|
||||
}
|
||||
return flushedLineNum
|
||||
}
|
||||
|
||||
func (f *Field) flushLine(j int) bool {
|
||||
|
@ -48,9 +48,12 @@ type GameScene struct {
|
||||
currentPiece *Piece
|
||||
currentPieceX int
|
||||
currentPieceY int
|
||||
currentPieceYCarry int
|
||||
currentPieceAngle Angle
|
||||
nextPiece *Piece
|
||||
landingCount int
|
||||
currentFrame int
|
||||
lines int
|
||||
}
|
||||
|
||||
func NewGameScene() *GameScene {
|
||||
@ -74,12 +77,18 @@ func (s *GameScene) initCurrentPiece(piece *Piece) {
|
||||
x, y := s.currentPiece.InitialPosition()
|
||||
s.currentPieceX = x
|
||||
s.currentPieceY = y
|
||||
s.currentPieceYCarry = 0
|
||||
s.currentPieceAngle = Angle0
|
||||
}
|
||||
|
||||
func (s *GameScene) Update(state *GameState) error {
|
||||
const maxLandingCount = 60
|
||||
func (s *GameScene) level() int {
|
||||
return s.lines / 10
|
||||
}
|
||||
|
||||
func (s *GameScene) Update(state *GameState) error {
|
||||
s.currentFrame++
|
||||
|
||||
const maxLandingCount = 60
|
||||
if s.currentPiece == nil {
|
||||
s.initCurrentPiece(s.choosePiece())
|
||||
}
|
||||
@ -107,16 +116,26 @@ func (s *GameScene) Update(state *GameState) error {
|
||||
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
|
||||
moved = y != s.currentPieceY
|
||||
}
|
||||
|
||||
// Drop the current piece with gravity.
|
||||
s.currentPieceYCarry += 2*s.level() + 1
|
||||
for 60 <= s.currentPieceYCarry {
|
||||
s.currentPieceYCarry -= 60
|
||||
s.currentPieceY = s.field.DropPiece(piece, s.currentPieceX, s.currentPieceY, angle)
|
||||
moved = y != s.currentPieceY
|
||||
}
|
||||
|
||||
if moved {
|
||||
s.landingCount = 0
|
||||
} else if !s.field.PieceDroppable(piece, x, y, angle) {
|
||||
} else if !s.field.PieceDroppable(piece, s.currentPieceX, s.currentPieceY, angle) {
|
||||
if 0 < state.Input.StateForKey(ebiten.KeyDown) {
|
||||
s.landingCount += 10
|
||||
} else {
|
||||
s.landingCount++
|
||||
}
|
||||
if maxLandingCount <= s.landingCount {
|
||||
s.field.AbsorbPiece(piece, x, y, angle)
|
||||
lines := s.field.AbsorbPiece(piece, s.currentPieceX, s.currentPieceY, angle)
|
||||
s.lines += lines
|
||||
s.initCurrentPiece(s.nextPiece)
|
||||
s.nextPiece = nil
|
||||
s.landingCount = 0
|
||||
|
Loading…
Reference in New Issue
Block a user