JustWeEngine - 輕量級游戲框架


JustWeEngine - 輕量級游戲框架

An easy open source Android game engine.

Github地址

Android Arsenal

引擎核心類流程圖

使用方法

引入Engine作為Library進行使用。

引擎初步封裝完畢

以之開發的微信打飛機游戲Demo:Demo地址

快速入門

1.基礎功能

1.1繼承引擎核心類:

由於框架全部使用SurfaceView進行繪制,不使用諸如Button、Layout等原生控件,所以應該首先新建類繼承引擎核心類Engine,負責游戲的流程,注釋中已有明確的標明功能。


public class Game extends Engine {
// 一般在構造函數完成變量的初始化
public Game() {
// 控制debug模式是否開始,debug模式打印日志、幀數、pause次數等信息
super(true);

}

// 載入一些UI參數和設置屏幕放向,默認背景色,設定屏幕的掃描方式等
@Override
public void init() {
// 初始化UI默認參數,必須調用該方法,其中有一些用於多機型適配的參數需要初始化
UIdefaultData.init(this);
}

// 通常用於精靈,背景,圖片等物體的設置和載入
@Override
public void load() {

}

// draw 和 update 在線程中進行不斷的循環刷新
// draw 負責繪制 update 負責更新數據和對象信息
@Override
public void draw() {

}

@Override
public void update() {

}

// 將touch 事件傳回 功能和所設定的屏幕掃描方式有關
@Override
public void touch(MotionEvent event) {

}

// 將碰撞事件傳回 傳回精靈和物品的基類 
// 用於處理碰撞事件 默認使用矩形碰撞
@Override
public void collision(BaseSub baseSub) {

}
}

1.2繪制文字:

使用GamePrinter進行文字繪制,除此以外還有多種方法繪制:


@Override
public void draw() {
Canvas canvas = getCanvas();
GameTextPrinter printer = new GameTextPrinter(canvas);
printer.drawText("哈哈哈", 100, 100);
}

效果圖:

1.3繪制圖片:

建議圖片存放在Asset中:

GameTexture texture = new GameTexture(this);
texture.loadFromAsset("pic/logo.jpg")
texture.draw(canvas, 100, 100);

效果圖:

另外也可使用loadFromAssetStripFrame從一個大的圖片中取出對應位置的圖片。


/**
* get bitmap from a big bitmap
*
* @param filename
* @param x
* @param y
* @param width
* @param height
* @return
*/
public boolean loadFromAssetStripFrame(String filename,
int x, int y,
int width, int height)

比如可以通過這四個參數把這個小飛機取出來:

PicUtils中提供了在Bitmap處理中很有用的各種特效和壓縮方法,大家可以一試。

1.4使用精靈:

使用精靈可以使用BaseSprite也可以繼承該類使用,BaseSprite封裝了很多方法供各種動畫使用,這些功能很多都是需要結合動畫系統來使用的,動畫系統會在后面介紹。

新建精靈:

1.簡單初始化:


sprite = new BaseSprite(this);

2.初始化連續幀動畫:
連續幀的初始化需要這樣的連續幀圖片:


GameTexture texture = new GameTexture(this);
texture.loadFromAsset("pic/zombie.png");
// 長寬以及列數
sprite = new BaseSprite(this, 96, 96, 8);
sprite.setTexture(texture);
sprite.setPosition(100, 100);
sprite.setDipScale(100, 100);
// 幀切換動畫是關鍵
sprite.addAnimation(new FrameAnimation(0, 63, 1));
addToSpriteGroup(sprite);

效果圖:

3.使用從大圖取出的多幀圖片:


// 新建圖片資源(此圖為上圖的大圖)
GameTexture texture = new GameTexture(this);
texture.loadFromAsset("pic/shoot.png");
// 初始化設定模式和長寬
ship = new BaseSprite(this, 100, 124, FrameType.COMMON);
// 設定資源
ship.setTexture(texture);
// 從大圖中取出兩幀
ship.addRectFrame(0, 100, 100, 124);
ship.addRectFrame(167, 361, 100, 124);
ship.addAnimation(new FrameAnimation(0, 1, 1));

效果圖(兩幀圖片不斷切換):

4.一些重要的其他設定:


// 圖片資源
ship.setTexture(texture);
// 大圖取幀模式
ship.addRectFrame(0, 100, 100, 124);
// 設定位置
ship.setPosition(x, y);
// 設置dp大小
ship.setDipScale(96, 96);
// 設定dp位置
ship.setDipPosition(x, y);
// 設定透明度
ship.setAlpha(...);
// 只有將精靈添加到SpriteGroup中框架才會自行繪制,否則需要手動調用
addToSpriteGroup(ship);
...

1.5使用按鈕:

使用的按鈕可以繼承BaseButton進行拓展,也可以直接使用TextureButton和TextButton進行使用。
Button設定功能的方式和原生一樣,通過設定接口回調的方式進行:


button.setOnClickListener(new OnClickListener() {
@Override
public void onClick() {
Log.e("button", "onClick");
}
});

1.TextureButton:


TextureButton button;
// 初始化並設定名字
button = new TextureButton(this, "logo");
texture = new GameTexture(this);
texture.loadFromAsset("pic/logo.jpg");
// 添加圖片資源
button.setTexture(texture);
// button的接口回調,不是View的那個接口
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick() {
Log.e("button", "onClick");
}
});
button.setPosition(200, 300);
button.setDipScale(100, 150);
// 添加到ButtonGroup進行繪制和處理
addToButtonGroup(button);

效果圖:

結合PicUtil中的各種Bitmap處理方法可以很容易的做出各種樣式的Button:

2.TextButton:


TextButton button; 
button = new TextButton(this, "logo");
button.setText("劉豐愷");
addToButtonGroup(button);
// 余略見源碼
...

效果圖:

2.動畫系統

目前的動畫系統可以使用已經封裝好的繼承了BaseAnimation的動畫,也可以繼承BaseAnim進行自我定義動畫類進行使用。

2.1綁定在BaseSub物品及精靈基類上的動畫類

AnimType中保存了Animation的應用類型。

Animation method function
AliveAnimation adjustAlive(boolean ori) 碰撞檢測的時候進行判斷存活狀態
AlphaAnimation adjustAlpha(int ori) 修改物體透明度
CircleMoveAnimation adjustPosition(Float2 ori) 沿某一圓心進行圓周運動
FenceAnimation adjustPosition(Float2 ori) 使用圍欄動畫防止出界
FrameAnimation adjustFrame(int ori) 逐幀動畫
MoveAnimation adjustPosition(Float2 ori) 位移動畫
SpinAnimation adjustRotation(float ori) 旋轉動畫
ThrobAnimation adjustScale(Float2 ori) 跳躍動畫
VelocityAnimation adjustPosition/adjustAlive 線性加速度計
WrapMoveAnimation adjustPosition(Float2 ori) 圍欄動畫防止出界
ZoomAnimation adjustScale(Float2 ori) 放大縮小動畫
待續 ... ...

綁定動畫分為兩類,ListAnimation和FixedAnimation,ListAnimation將動畫存儲到固定的一個List中,用於重復更新的動畫,
而FixedAnimation存儲在Map中,使用名字進行調用,用於點擊或者非自動更新的動畫。
比如前面精靈類動畫的就是添加到ListAnimation。
下面的這種寫法就是FixedAnimation,這個動畫是小飛機入場,因為只使用了一次,所以使用了FixedAnimation。


ship.addfixedAnimation("start",
new MoveAnimation(UIdefaultData.centerInHorizontalX - ship.getWidthWithScale() / 2,
UIdefaultData.screenHeight - 2 * ship.getHeightWidthScale(), new Float2(10, 10)));

效果圖:

2.2綁定在Button上的動畫類

BaseButtonAnimation是BaseButton的動畫類繼承了BaseAnim的動畫基類,通過提供Button的狀態,設定Button的動畫。

Animation method function
ZoomCenterButtonAnim adjustButtonRect(Rect ori,boolean touchType) 按鈕放縮動畫
ColorAnimation adjustButtonBackGround(int ori,boolean type) TextButton點擊變色
待續 ... ...

為Button設定放縮動畫:


// 設定中心放縮
button.setZoomCenter(true);
// 三個參數 初始值/放大值/幀數
button.setAnimation(new ZoomCenterButtonAnim(10, 30, 3));

效果圖:

為Button設定顏色動畫:


// 初始顏色 / 按下顏色
button.setAnimation(
new ColorAnimation(UIdefaultData.colorAccent,
UIdefaultData.colorPressed));

效果圖:

3.碰撞檢測和死亡判定

只要使用了addToSpriteGroup(sprite)的精靈對象就會自動進行碰撞檢測,而對碰撞檢測的結果會從
collision中進行發回。


@Override
public void collision(BaseSub baseSub) {
// 獲取與之碰撞的對象
BaseSprite other = (BaseSprite) baseSub.getOffender();
// 獲取ID分組處理
if (baseSub.getIdentifier() == BULLET &&
other.getIdentifier() == ENEMY) {
// 設定死亡
other.setAlive(false);
// 回收
removeFromSpriteGroup(other);
addToRecycleGroup(other);
}
}

其中getOffender()獲得與之碰撞的對象,通過getIdentifier()獲取設定的對象分組,實行邏輯判斷。
開啟Debug模式會看見碰撞線。
效果圖:

4.屏幕掃描模式

屏幕掃描模式是用來優先響應屏幕點擊、Button點擊、和多點觸控而設的,放置在不同情況下都能優化屏幕的刷新。


// 檢測單一移動
SINGLE,
// 檢測Button
BUTTON,
// 多點檢測
FULL,
// 單擊+Button
SINGLE_BUTTON

5.工具類

  • NetUtils 網絡狀態工具類
  • PicUtils 圖片處理工具類
  • ServiceUtils 服務工具類
  • ImageHelper 圖型處理類
  • DisplayUtils 數據轉換類
  • SpUtils Sp簡化工具類(可存儲list和map
  • ValidatorsUtils 正則表達式處理類

有問題反饋

在使用中有任何問題,歡迎反饋給我,可以用以下聯系方式跟我交流

License

Copyright 2015 劉豐愷

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM