A simple 2D game engine to make a simple beautiful iOS/tvOS Games!
A simple 2D UIKit game engine to make a simple beautiful iOS/tvOS Games!
The main goal of this project is to use the storyboard and UIKit components to build 2D games!.
You can check Zaina game to have the full source code
Check at the end of the read me for the games i have done using SimpleEngine
.
First step, inherit the BaseGameViewController
it will notify you when:
1-Two objects collide.
override func objectsDidCollide(object1: ObjectView, object2: ObjectView) -> Bool {
switch (object1.type, object2.type) {
case (CollideTypes.virus, CollideTypes.whiteCell):
return collideBetween(virus: object1, whiteCell: object2)
case (CollideTypes.whiteCell, CollideTypes.virus):
return collideBetween(virus: object2, whiteCell: object1)
default:
break
}
return true
}
2-The game pause or resume.
override func didPause() {
showPauseDialog()
stopTimer()
}
override func didResume() {
startTimer()
}
paused = true // to pause the game.
paused = false // to resume the game.
It’s where your SpriteView
s and NodeView
s should be added. In the BaseGameViewController
you will find an IBOutlet
with a type of SceneView
you need to connect this IBOutlet
to your Storyboard SceneView
.
A sprite view is what will be moving in the game, like the player or the enemy. You can inherit the sprite view
to make your own custom sprite view or you can use the IBInspectable
properties in the storyboard.
virusSprite = VirusSpriteView()
virusSprite.attachTo(analogView)
let rightMargen: CGFloat = virusSprite.frame.width
let x = view.frame.width - virusSprite.frame.width - rightMargen
let y = (view.frame.height / 2) - virusSprite.frame.height
virusSprite.frame.origin = CGPoint(x: x, y: y)
sceneView.addSubview(virusSprite)
You can change the speed of the sprite view by changing this property.
speed = 10
The first image it will show when you add the sprite to the storyboard. after that it will show the Freams
.
initialImage = UIImage(named: "man")
You can set the images (frames) that will show when the user move to right, left, top, bottom, topLeft, bottomLeft, topRight, bottomRight or idel.
frames.top = Frames(images: [UIImage(named: "top_1"), UIImage(named: "top_2")], duration: 0.2)
frames.left = Frames(images: [UIImage(named: "for_1"), UIImage(named: "for_2")], duration: 0.3)
frames.right = Frames(images: [UIImage(named: "move_back_1"), UIImage(named: "move_back_2")], duration: 0.4)
frames.bottom = Frames(images: [UIImage(named: "move_bottom_1"), UIImage(named: "move_bottom_2")], duration: 0.2)
frames.idel = Frames(images: [UIImage(named: "idel_1"), UIImage(named: "idel_2")], duration: 0.5)
The SpriteView
will stop when it collide with one of this types. Like if some Tree has a Type of 8
and you add this number to this array when this sprite view collide with this tree it will not move through it.
stopWhenCollideTypes = [CollideTypes.virus, CollideTypes.fire]
If the value is true, the object will not pass through out the screen edges.
shouldHitTheEdges = true
It can be overrided to do extra setups in the subview side.
override func setup() {
super.setup()
type = CollideTypes.fire
speed = 20
stopWhenCollideTypes = []
frames.idel = Frames(images: [UIImage(named: "idel_1"), UIImage(named: "idel_2")], duration: 0.5)
}
Make the sprite move to specific x
and y
.
spriteView.moveTo(x: 20, y: 200)
AnalogView
By setting this, you are attaching this SpriteView
to Analog to control it, each sprite view has one analog to control it.
let virusSprite = VirusSpriteView()
virusSprite.attachTo(analogView)
A method will be called when any object collided with this object. super.onCollisionEnter(with object:)
must always be called when you override this method.
override func onCollisionEnter(with object: ObjectView?) -> Bool {
super.onCollisionEnter(with: object)
guard !didColide else {
return false
}
destroy()
}
override to be notifide when the SpriteView
reaches the desired point.
override func didRechedDesiredPoint() {
removeFromSuperview()
}
Use it to update the SpriteView
in case you changed any of the Frames
.
self.frames.idel = Frames(images: [UIImage(named: "infected_6")])
self.updateFrames()
Animate frames for the sprite.
startAnimationWith(frames: frames,
repeatCount: 1,
stopOtherAnimations: false)
A NodeView
is a thing that don’t move, it’s a chair, table or wall in your game.
FramesHolder
is a group of Frames
for the sprite view, each Frames
object content multible images that will be animated.
To set a moving background for your game, The MovingBackgroundView
take a view and animate it again and again to make it look like your sprites moving.
movingBackgroundView.view = StreamBackgroundView(frame: view.bounds) // StreamBackgroundView is a custom view i made.
A view to set as a background for the SceneView
it could be a normal image or a pattern image.
An Analog controller to control the Sprite
movement.
A music player to help you playe background music and sound effects.
enum Music: String, MusicType {
var format: String {
var type: Type!
switch self {
case .fire:
type = .mp3
case .gameBackground:
type = .wav
return type.rawValue
}
case gameBackground = "game_background"
case fire = "fire"
}
enum Type: String {
case mp3
case wav
}
SimpleMusicPlayer.shared.playBackgroundMusicWith(music: Music.gameBackground) // background music
SimpleMusicPlayer.shared.playMusic(music: Music.fire) // effect sound.
To be done.
You can support this project by:
1- Checking my apps.
2- Star the repo.
3- Share the repo with your friends.
4- Buy Me A Coffee.
Facebook | Twitter | Instagram | Youtube
The MIT License (MIT)
Copyright (c) 2020 Abedalkareem
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.