SimpleEngine 0.6.0

SimpleEngine 0.6.0

Abedalkareem维护。



Facebook Buy Me A Coffee Youtube Twitter

Simple Engine

A simple 2D UIKit game engine to make a simple beautiful iOS/tvOS Games!

It's Just a UIKit!

本项目的目标是使用 Storyboard 和 UIKit 组件来构建 2D 游戏。

Simple Engine

您可以通过查看Zaina 游戏来获取完整源代码。

在下文的“已使用 SimpleEngine 的作品”部分查看我使用 SimpleEngine 完成的游戏。

如何使用

游戏视图控制器

第一步是继承 BaseGameViewController,这将在以下情况发生时通知您
1-两个物体相撞。

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-游戏暂停或恢复。

override func didPause() {
  showPauseDialog()
  stopTimer()
}

override func didResume() {
  startTimer()
}

它还将允许您使用 `paused` 属性暂停和恢复游戏。
paused = true // to pause the game.
paused = false // to resume the game.

场景视图

这里是你需要添加 SpriteViewNodeView 的地方。在 BaseGameViewController 中,你可以找到一个类型的 IBOutlet,需要将此 IBOutlet 连接到 Storyboard 中的 SceneView

Sprite view

Sprite view 是游戏中将移动的元素,如玩家或敌人。你可以继承 Sprite view 来创建自己的自定义 sprite view,或者可以使用 storyboard 中的 IBInspectable 属性。

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)

1-速度

你可以通过更改此属性来更改 sprite view 的速度。
速度 = 10

2-初始图像

当你将 Sprite 添加到 Storyboard 中时,将先显示的第一张图像。之后,它将显示 Freams
initialImage = UIImage(named: "man")

3-帧

你可以设置在用户移动到右、左、上、下、左上、右下、右上、右下或空闲时显示的图像(帧)。

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)

4-碰撞停止类型

SpriteView 与这些类型之一碰撞时,它将停止。例如,如果某些树具有类型 8,并且当此 sprite view 与此树碰撞时,它不会穿过它。
stopWhenCollideTypes = [CollideTypes.virus, CollideTypes.fire]

5-应击中边缘

如果值为true,则对象不会穿过屏幕边缘。
shouldHitTheEdges = true

6-设置

它可以重写以在子视图中执行额外的设置。

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)
}

7-移动到x和y

使精灵移动到特定的xy
spriteView.moveTo(x: 20, y: 200)

8-附加到AnalogView

通过设置此选项,您将此SpriteView附加到模拟控制它,每个精灵视图都有一个模拟来控制它。

let virusSprite = VirusSpriteView()
virusSprite.attachTo(analogView)

9-碰撞进入

当任何对象与此对象碰撞时,将调用一个方法。super.onCollisionEnter(with object:)必须始终在重写此方法时调用。

override func onCollisionEnter(with object: ObjectView?) -> Bool {
  super.onCollisionEnter(with: object)
  guard !didColide else {
    return false
  }
  destroy()
}

10-已到达预期的点

重写以在SpriteView达到预期点时通知。

override func didRechedDesiredPoint() {
  removeFromSuperview()
}

11-更新帧

使用它来更新SpriteView,以防你修改了任何Frames

self.frames.idel = Frames(images: [UIImage(named: "infected_6")])
self.updateFrames()

12-使用帧开始动画

为精灵的帧动画。

startAnimationWith(frames: frames,
                   repeatCount: 1,
                   stopOtherAnimations: false)

节点视图

NodeView是一种不会移动的东西,它可以是你的游戏中的椅子、桌子或墙壁。

FramesHolder 和 帧集

FramesHolder是一组用于精灵视图的Frames,每个Frames对象包含多个将被动画化的图像。

移动背景视图

为了设置游戏的移动背景,MovingBackgroundView获取一个视图并反复动画化它,使其看起来像你的精灵在移动。

movingBackgroundView.view = StreamBackgroundView(frame: view.bounds) // StreamBackgroundView is a custom view i made.

背景视图

这是一个视图,可以设置为SceneView的背景,它可以是一个普通图像或模式图像。

模拟视图

一个模拟控制器,用于控制 精灵 移动。

简易音乐播放器

一个音乐播放器,帮助您播放背景音乐和音效。

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.

更多游戏

1- Zaina
2- Virus - 游戏版

教程

待完成。

支持我🚀

您可以通过以下方式支持这个项目:

1- 检查我的 应用
2- 给仓库打星。
3- 与你的朋友分享仓库。
给我买杯咖啡

关注我❤️

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.