Skip to content

Commit

Permalink
version 0.2.0, some code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrider committed Aug 12, 2022
1 parent 4a0bce7 commit d687500
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

An example project using [SpriteKit](https://developer.apple.com/spritekit/) and [HexGrid](https://github.com/fananek/hex-grid) to draw hexagonal grids in various configurations.

> Please note that main is currently (temporarily) being used for development. For the most stable experience, you may want to download a release, or `git checkout ` a release tag.

## How to use this project

Expand All @@ -16,11 +14,14 @@ An example project using [SpriteKit](https://developer.apple.com/spritekit/) and

## History / Release Notes

### pre-v0.2.0
### v0.2.0

Roughly corresponds to HexGrid version 0.4.10.

* draws coordinates on top of each grid cell (type is based on the configuration)
* fixed iPadOS & macOS targets which were crashing when opening the config menu
* moved from using `isHighlighted` to using a state `Int` instead, with some state colors defined in `Cell+State.swift`
* moved from touch delegate methods to gesture-based input in `HexGridScene`

### v0.1.0

Expand All @@ -35,6 +36,8 @@ An example project using [SpriteKit](https://developer.apple.com/spritekit/) and
- the frame of the hexagon(s)
- allow toggling on/off the secondary hexagon (currently a yellow background)
- or _maybe_ make an array of configuration objects, and allow any number of hexagons to be drawn
* Allow drawing a border around the edges of the grid
- maybe margin values & colors for each `Direction`
* Add more visual effects
- animate the border of a tapped cell
- screen shake
Expand All @@ -45,13 +48,13 @@ An example project using [SpriteKit](https://developer.apple.com/spritekit/) and
- shortest path to some other cell
* Add a UINavigationController and additional types of examples.
- An example drawn with `UIKit`
- An example that does not "fit" to the screen size, and instead is much larger and scrolls off screen in all directions
- A fully functioning game example? ([Hex](https://en.wikipedia.org/wiki/Hex_(board_game)) maybe?)
* allow shifting of all grid coordinates (this has been started in the shifting-coordinates branch)
- 1 direction at a time
- moving offset coordinate 0,0 all the way to an edge of the grid

### Some ideas more suitable for the main hex-grid project

* allow shifting of all grid coordinates
- 1 direction at a time
- moving 0,0,0 all the way to an edge of the grid
* remove `Cell` `isOpaque` and `isBlocked` properties in favor of attributes with the same names
- write optional "where" clauses for all the functions that use them instead. (This would allow the API to return all cells neighboring a cell _where_ an attribute is true, or to find a path of cells sharing an attribute, similar.)
* fix rectangle grid generation
2 changes: 0 additions & 2 deletions SKHexGrid.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
85331851288C87240000DB75 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
8536B108289D9439005CDF87 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; };
8536B109289D961D005CDF87 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = SOURCE_ROOT; };
85C12F3E2896F13100E623A0 /* hex-grid */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "hex-grid"; path = "../hex-grid"; sourceTree = "<group>"; };
85CD4FF6288C891100B73468 /* HexGridScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HexGridScene.swift; sourceTree = "<group>"; };
85CD4FFB288C918D00B73468 /* Point+CGPoint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Point+CGPoint.swift"; sourceTree = "<group>"; };
85CD4FFD288C920E00B73468 /* Cell+Highlight.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Cell+Highlight.swift"; sourceTree = "<group>"; };
Expand All @@ -60,7 +59,6 @@
85331835288C87230000DB75 = {
isa = PBXGroup;
children = (
85C12F3E2896F13100E623A0 /* hex-grid */,
85331840288C87230000DB75 /* SKHexGrid */,
8533183F288C87230000DB75 /* Products */,
);
Expand Down
17 changes: 9 additions & 8 deletions SKHexGrid/Cell+State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ extension Cell {
}
}

fileprivate var stateInt : Int {
return self.attributes[Cell.kStateAttribute]?.value as? Int ?? -1
}

var state: State {
return State(rawValue: stateInt) ?? .empty
get {
guard let stateInt = self.attributes[Cell.kStateAttribute]?.value as? Int else {
return .empty
}
return State(rawValue: stateInt) ?? .empty
}
set (newValue) {
self.attributes[Cell.kStateAttribute] = .init(newValue.rawValue)
}
}

func setState(to: State) {
self.attributes[Cell.kStateAttribute] = .init(to.rawValue)
}
}
31 changes: 14 additions & 17 deletions SKHexGrid/HexGridScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,16 @@ class HexGridScene: SKScene {
updateCell(cell: cell)
}

// Create shape node to use during mouse interaction
// Create shape node to use during drag interaction
let w = (self.size.width + self.size.height) * 0.05
// self.spinnyNode = SKShapeNode.init(rectOf: CGSize.init(width: w, height: w), cornerRadius: w * 0.3)
self.spinnyNode = SKShapeNode(circleOfRadius: w)

if let spinnyNode = self.spinnyNode {
spinnyNode.lineWidth = 2.5

// spinnyNode.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(Double.pi), duration: 1)))
spinnyNode.run(SKAction.sequence([SKAction.scale(by: 0.01, duration: 0.5),
SKAction.fadeOut(withDuration: 0.5),
SKAction.removeFromParent()]))
}
let spinnyNode = SKShapeNode(circleOfRadius: w)
spinnyNode.lineWidth = 2.5
spinnyNode.run(SKAction.sequence([
SKAction.scale(by: 0.01, duration: 0.5),
SKAction.fadeOut(withDuration: 0.5),
SKAction.removeFromParent(),
]))
self.spinnyNode = spinnyNode

// add some gesture recognizers
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
Expand All @@ -158,9 +155,9 @@ class HexGridScene: SKScene {
} else {
print( "Cell tapped - x: \(cell.coordinates.x), y: \(cell.coordinates.y), z: \(cell.coordinates.z)")
if cell.state == .tapped {
cell.setState(to: .empty)
cell.state = .empty
} else {
cell.setState(to: .tapped)
cell.state = .tapped
}
updateCell(cell: cell)
}
Expand All @@ -173,7 +170,7 @@ class HexGridScene: SKScene {
print( "Cell x: \(cell.coordinates.x), y: \(cell.coordinates.y), z: \(cell.coordinates.z) is blocked!")
} else {
print( "Cell x: \(cell.coordinates.x), y: \(cell.coordinates.y), z: \(cell.coordinates.z)")
cell.setState(to: .touchStarted)
cell.state = .touchStarted
updateCell(cell: cell)
}
}
Expand All @@ -189,7 +186,7 @@ class HexGridScene: SKScene {
!cell.isBlocked,
cell.state != .touchStarted
{
cell.setState(to: .touchContinued)
cell.state = .touchContinued
updateCell(cell: cell)
}
if let n = self.spinnyNode?.copy() as! SKShapeNode? {
Expand All @@ -201,7 +198,7 @@ class HexGridScene: SKScene {

func touchUp(atPoint pos : CGPoint) {
if let cell = try? grid.cellAt(pos.hexPoint), !cell.isBlocked {
cell.setState(to: .touchEnded)
cell.state = .touchEnded
updateCell(cell: cell)
}
if let n = self.spinnyNode?.copy() as! SKShapeNode? {
Expand Down

0 comments on commit d687500

Please sign in to comment.