Section 4

Go back to the LaserGame and fill-in the #newGame method now.

newGame
    self grid initializeCells.
    self grid stopLaser.
    self moves: 0.
    self activeCellLocation: nil.
    self initializeDirty.
    GridFactory randomizeGrid: self grid.
    self updateGameBoardAndControls

We still have to complete our refactoring here. Here's the modified #fireLaser code.

fireLaser
    self laserActive
        ifTrue: [self grid stopLaser]
        ifFalse: [self grid fireLaser].
    self updateGameBoardAndControls

With the possibility of a grid being randomized with the new button, we have to ensure that old cells are properly repainting now. Before we relied on the fact that the cells were all initially blank. Now it's possible for a cell to change from a blank to being a mirror, or from a mirror to being blank, and the orientation of a mirror can change. We need better cleanup and redraw code. Here's a new instance method on CellRenderer.

fillBackground
    | offset backgroundRect |
    offset := self offsetWithinGridForm.
    backgroundRect := offset extent: CellRenderer cellExtent - 2.
    self targetForm fill: backgroundRect fillColor: LaserGameColors gameBoardBackgroundColor.

The #redrawCell method needs to change.

redrawCell
    self fillBackground.
    self render

The blank cell should ensure that it's really blank...

renderContents
    self fillBackground

The mirror cell should repaint its background too now.

renderContents
    self fillBackground.
    self cell isLeft
        ifTrue: [self renderContentsLeanLeft]
        ifFalse: [self renderContentsLeanRight]

And the target cell.

renderContents
    self fillBackground.
    self drawTargetOutlines.
    self cell isOn
        ifTrue: [self renderContentsOn]
        ifFalse: [self renderContentsOff]

Okay, that should do it. Open up a new LaserGame morph. Move some mirror cells around, rotate others. Then try the new game button and everything should be correct.

Once again we should save our package with Monticello. Save as version 6.

Index Page Next Page

Copyright © 2007, 2008, 2009, 2010 Stephan B Wessels    stevewessels@me.com