Skip to content

Commit

Permalink
object dynamics update
Browse files Browse the repository at this point in the history
- fixed a bug where object properties were added to the parent object group
- `SKTileLayer.addTileAt` now  tries to resolves `gid` argument
- add support for `SKTileObject` physics:
    - add `SKTiledObject.hasProperties` property
    - add `SKTileObject.physicsType` property
    - add `SKTiledSceneCamera.overlay` property
- add `SKTileObject` texture rendering
- add `flippedTileFlags` function
- add `SKTileset.getTileRealID` method
- `SKTiledScene` automatically resizes maps with the `SKTilemap.autoResize` is set
- add `SKTileLayer.addTileAt(coord:texture)`
- add `SKTilemap.pointForCoordinate` and `SKTilemap.coordinateForPoint` methods
- updated README
  • Loading branch information
mfessenden committed Oct 30, 2016
1 parent d324d2e commit 87529f2
Show file tree
Hide file tree
Showing 38 changed files with 969 additions and 422 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Custom/
Sources/SKTiled+Debug.swift
Resources/shaders/
Resources/test/
docs/swift/
Expand Down
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ Change Log
--------------------------

#### Changes
- add `SKTileObject.isDynamic` property
- add `SKTiledSceneCamera.overlay` property
- fixed a bug where object properties were added to the parent object group
- `SKTileLayer.addTileAt` now tries to resolves `gid` argument
- add support for `SKTileObject` physics:
- add `SKTiledObject.hasProperties` property
- add `SKTileObject.physicsType` property
- add `SKTiledSceneCamera.overlay` property
- add `SKTileObject` texture rendering
- add `flippedTileFlags` function
- add `SKTileset.getTileRealID` method
- `SKTiledScene` automatically resizes maps with the `SKTilemap.autoResize` is set
- add `SKTileLayer.addTileAt(coord:texture)`
- add `SKTilemap.pointForCoordinate` and `SKTilemap.coordinateForPoint` methods
- updated README

1.0.3
--------------------------
Expand Down
19 changes: 0 additions & 19 deletions LICENSE

This file was deleted.

22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Licensed under the **MIT** license

> Copyright (c) 2016 Michael Fessenden
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
> "Software"), to deal in the Software without restriction, including
> without limitation the rights to use, copy, modify, merge, publish,
> distribute, sublicense, and/or sell copies of the Software, and to
> permit persons to whom the Software is furnished to do so, subject to
> the following conditions:
>
> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
187 changes: 95 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,103 @@
[![SKTiled](docs/Images/header.png)](https://mfessenden.github.io/SKTiled)
![SKTiled](docs/Images/header.png)

**SKTiled** is a simple library for using [Tiled](http://www.mapeditor.org) files with Apple's SpriteKit, allowing the creation of game assets from .tmx files. Inspired by [TilemapKit](http://tilemapkit.com) and written purely in Swift, I began working on this for a project after the development of TilemapKit was halted. While initially created as an exercise in learning Apple's new programming language, I've decided to open-source it in case others find it helpful.
> SKTiled is a lightweight framework for using [Tiled](http://www.mapeditor.org) files with Apple's SpriteKit.
[![Swift Version][swift-image]][swift-url]
[![Build Status][travis-image]][travis-url]
[![License][license-image]][license-url]
[![iOS][ios-image]][ios-url]
[![macOS][macos-image]][macos-url]

**SKTiled** supports most Tiled features, with custom features that take advantage of Apple's SpriteKit API.

**SKTiled** is a simple library for using [Tiled](http://www.mapeditor.org) files with Apple's SpriteKit, allowing the creation of game assets from .tmx files. Inspired by [TilemapKit](http://tilemapkit.com) and written purely in Swift, I began working on this for a project after the development of TilemapKit was halted. Initially created as an exercise to learn Apple's new programming language, I've decided to open-source it in case others find it helpful.

![SKTiled Intro](docs/Images/intro.gif)

Check out the [official documentation](https://mfessenden.github.io/SKTiled).
![macOS Demo](docs/Images/demo-macos-iso.png)

## Requirements

Xcode 8 and Swift 3.

See [this branch](https://github.com/mfessenden/SKTiled/tree/swift2) for Swift 2.3+ version.


![](https://img.shields.io/badge/Swift-3.0-brightgreen.svg)
[![Build Status](https://travis-ci.org/mfessenden/SKTiled.svg?branch=master)](https://travis-ci.org/mfessenden/SKTiled)
[![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/mfessenden/SKTiled/blob/master/LICENSE)


##Installation

Simply drag the *Sources* directory into your Xcode project, and add the files to your game target:

![Xcode installation](docs/Images/installation.png)
Check out the [Official Documentation](https://mfessenden.github.io/SKTiled).

## Features

Alternately, you can include this directory in your project's workspace.
- [x] iOS & macOS versions
- [x] parses inline & external tilesets
- [x] translates custom properties for maps, layers, objects & tiles
- [x] renders all projections: (orthogonal, isometric, hexagonal & isometric staggered)
- [x] renders all layer types: (tile, object, image)
- [x] supports all compression types: (base64, zlib, gzip)
- [x] renders animated and fliped tiles
- [ ] multi-threaded rendering
- [ ] generate GKGridGraph graphs from custom attributes (iOS10, macOS 10.11+)
- [ ] user-definable cost properties for GKGridGraph nodes (iOS10, macOS 10.11+)
- [ ] custom tile classes (iOS10, macOS 10.11+)

### Linking zlib

You'll need to add a path to the zlib module in your project under **Import Paths:**
## Requirements

*Project > Build Settings > Swift Compiler - Search Paths > Import Paths*
- iOS 10+
- macOS 10.11+
- Xcode 8

Add the following to the project:
## Installation

`$(SRCROOT)/Sources`
1. Clone and drag the `Sources` and `zlib` directories to your project.
2. Add the `zlib` path to your project's include paths.

The demo project includes targets for iOS and macOS, with example scenes you can compile and run.

![zlib compression](docs/Images/zlib_linking.png)

## Usage

Loading a tilemap is simple:
Loading a tilemap is very straightforward:

```swift
if let tilemap = SKTilemap.load(fromFile: "sample-map") {
scene.addChild(tilemap)
}
```
Once loaded, the rendered `SKTilemap` node reflects the various properties defined in the originating scene:

- `SKTilemap.size`: size of the map in tiles.
- `SKTilemap.tileSize`: size of individual tiles.
- `SKTilemap.orientation`: map orientation (ie orthogonal, isometric, etc).


The included `SKTiledScene` object conforms to the `SKTiledSceneDelegate` protocol and can be used as a template. The tilemap is accessed via the `SKTiledScene.tilemap` property, and should be added as a child of the `SKTiledScene.worldNode` object.
The `SKTilemap` node also allows access to child layers, tilesets, objects and individual tiles.

### Working with Layers

## Working with Tilemaps
Layers represent containers that house various types of data:

- tile layers hold an array of tile sprites and associated tileset data
- object groups contain vector shape objects
- image layers display a single image

**Acessing Layers**
All **SKTiled** layer types are subclasses of the `TiledLayerObject` class and provide access to coordinate transformation and positioning information. Additionally, every layer type can have individual offset transforms and rendering flags.

Layers can be accessed by type:
Layers can be accessed by type, name or index:

```swift
// query layers by type
let tileLayers = tilemap.tileLayers
let objectGroups = tilemap.objectGroups
let imageLayers = tilemap.imageLayers
```

or by name:

```swift
// query named layers
let groundLayer = tilemap.getLayer(named: "Ground") as! SKTileLayer
let objectsGroup = tilemap.getLayer(named: "Objects") as! SKObjectGroup
let hudLayer = tilemap.getLayer(named: "HUD") as! SKImageLayer

// query a named tile layer
if let groundLayer = tilemap.tileLayer(named: "Ground") {
groundLayer.showGrid = true
}
```

Properties like map size & tile size can be accessed via the `SKTilemap.size` and `SKTilemap.tileSize` properties.


**Accessing Tiles**
// query layer at a specific index
let firstLayer = tilemap.getLayer(atIndex: 1) as! SKTileLayer
```

There are a number of ways to access and work with tile objects. Tiles
### Working with Tiles

You can query them from the parent `SKTileLayer` layer or the parent `SKTilemap` node.
There are a number of ways to access and work with tile objects. Tiles can be queried from the `SKTilemap` node, or the parent `SKTileLayer` layer:

```swift
// access a tile via CGPoint
Expand All @@ -100,7 +108,7 @@ let tile = groundLayer.tileAt(coord: tileCoord)
let tile = groundLayer.tileAt(7, 12)
```

There are many ways to work with tile objects; globally from the `SKTilemap` node:
Tiles assigned custom properties in **Tiled** can be accessed in **SKTiled**:

```swift
// query tiles of a certain type
Expand All @@ -112,17 +120,17 @@ if let fireTiles = tilemap.getTiles(ofType: "fire") {
let tiles = tilemap.tilesAt(2, 4)
```

... or from individual layers:
You can also return tiles with a specific ID value:

```swift
if let waterTiles = waterLayer.getTiles(withID: 17) {
// do something watery here
}
```

**Accessing Objects**
### Working with Objects

`SKTileObject` objects can be returned from both the `SKTilemap` and `SKObjectGroup` nodes:
`SKTileObject` objects can be queried from both the `SKTilemap` and `SKObjectGroup` nodes:

```swift
let allObjects = tilemap.getObjects()
Expand All @@ -133,18 +141,17 @@ let allCollisionObjects = tilemap.getObjects(ofType: "Collision")
let entrances = objectsLayer.getObjects(ofType: "Entrance")
```

**Acessing Tile Data**
### Acessing Tile Data

Tile data is accessible from either the `SKTileSet` object:
The `SKTilemap` node stores an array of individual tilesets parsed from the original **Tiled** document. Individual tile data is accessible from either the `SKTileSet` object:

```swift
let tileSet = tilemap.getTileset("spritesheet-16x16")
// get data for a specific id
let tileData = tileSet.getTileData(gid: 177)
```


as well as the parent `SKTilemap`:
and the parent `SKTilemap`:

```swift
let tileData = tilemap.getTileData(gid: 177)
Expand All @@ -160,41 +167,49 @@ let newTile = SKTile(data: tileData)
scene.addChild(newTile)
```

Coordinate information is accessible within each layer via the `TiledLayerObject.pointForCoordinate` method:
Coordinate information is available from each layer via the `TiledLayerObject.pointForCoordinate` method:

```swift
let tilePoint = groundLayer.pointForCoordinate(4, 5)
tile.position = tilePoint
```

New nodes (any `SKNode` type) can be added directly to any layer. All `TiledLayerObject` layer types have convenience methods for adding child nodes with coordinates and z-position.

New nodes (any `SKNode` type) can be added directly to any layer. All `TiledLayerObject` layer types have expanded convenience methods for adding child nodes with coordinates and z-position.

```swift
let newNode = SKNode()
groundLayer.addChild(newNode, 4, 5, zpos: 100.0)
```

**SKTiled** also provides methods for getting coordinate data from `UITouch` and `NSEvent` mouse events:

```swift
// get the coordinate at the location of a mouse event
let touchLocation: CGPoint = objectsLayer.coordinateAtTouchLocation(touch)
```

## Animated Tiles

Animated tiles will animate automatically; animated tiles can be accesssed from the tilemap. The `SKTile.pauseAnimation` property can stop/start animations:
Tiles with animation will animate automatically; animated tiles can be accesssed from the either the `SKTilemap` node or the parent layer. The `SKTile.pauseAnimation` property can stop/start animations:

```swift
let animatedTiles = tilemap.getAnimatedTiles()
let allAnimated = tilemap.getAnimatedTiles()
let layerAnimated = groundLayer.getAnimatedTiles()

for tile in animatedTiles {
for tile in allAnimated {
// pause the current animation
tile.pauseAnimation = true
}
```

## Custom Properties

Custom properties are supported on all object types, and can be accessed easily:
Custom properties are supported on all object types. All **SKTiled** objects conform to the `SKTiledObject` protocol and allow access to and parsing of custom properties.

Any property added to an object in **Tiled** will be translated and stored in the `SKTiledObject.properties` dictionary.

```swift
let value = groundLayer.getValue(forProperty: "type")
let tileType = groundLayer.getValue(forProperty: "type")
groundLayer.setValue("water", forProperty: "type")
```

Expand All @@ -205,41 +220,29 @@ let waterTiles = groundLayer.getTiles(ofType: "water")
let allWaterTiles = tilemap.getTiles(ofType: "water")
```

For specific property/value types, query the parent layer:
For specific property/value types:

```swift
let walkableTiles = groundLayer.getTilesWithProperty("walkable", "1")
let groundWalkable = groundLayer.getTilesWithProperty("walkable", "1")
let allWalkable = tilemap.getTilesWithProperty("walkable", "1")
```

or the tilemap:

```swift
let walkableTiles = tilemap.getTilesWithProperty("walkable", "1")
```

##Features

- iOS & macOS versions
- parses compressed data (base64, zlib, gzip)
- renders all Tiled layer types (tile, object, image)
- custom properties for maps, layers, objects & tiles
- parses inline & external tilesets
- render tile layers as a single sprite
- render animated tiles
- render flipped tiles

## Acknowledgments

##Upcoming Features
- [GZipSwift](https://github.com/1024jp/GzipSwift): zlib decompression extensions
- [Steffen Itterheim](http://www.learn-cocos2d.com): Author of TilemapKit, the inspiration for this project
- [Kenney Vleugels](http://www.kenney.nl): demo spritesheet assets
- [Amit Patel](http://www-cs-students.stanford.edu/~amitp/gameprog.html): tile-based game logic
- [Clint Bellanger: Isometric Tiles Math](http://clintbellanger.net/articles/isometric_math)

- multi-threaded rendering
- generate GKGridGraph graphs based on custom tile attributes (iOS10)
- user-definable cost properties for GKGridGraph nodes (iOS10)
- custom tile classes
- objects containing images

##Acknowledgements
- Decompression functions based on: [GZipSwift](https://github.com/1024jp/GzipSwift)
- [Steffen Itterheim](http://www.learn-cocos2d.com) creator of TilemapKit, the inspiration for this project
- [Kenney Vleugels](http://www.kenney.nl) for use of his spritesheet assets.
- [Amit Patel](http://www-cs-students.stanford.edu/~amitp/gameprog.html) for his excellent tutorials on tile-based game logic
- [Clint Bellanger: Isometric Tiles Math](http://clintbellanger.net/articles/isometric_math/)
[swift-image]:https://img.shields.io/badge/Swift-3.0-brightgreen.svg
[swift-url]: https://swift.org/
[license-image]:https://img.shields.io/badge/License-MIT-blue.svg
[license-url]:https://github.com/mfessenden/SKTiled/blob/master/LICENSE
[travis-image]:https://travis-ci.org/mfessenden/SKTiled.svg?branch=master
[travis-url]:https://travis-ci.org/mfessenden/SKTiled
[ios-image]:https://img.shields.io/badge/platform-ios-lightgrey.svg
[ios-url]:http://www.apple.com/ios/ios-10
[macos-image]:https://img.shields.io/badge/platform-macos-lightgrey.svg
[macos-url]:http://www.apple.com/macos
Loading

0 comments on commit 87529f2

Please sign in to comment.