android机顶盒游戏开发(一)

目标:键盘导航

机顶盒游戏与手机运行最大的区别是输入方式不同,手机主要是触摸输入,机顶盒是遥控器和游戏手柄输入(等价于键盘输入)

游戏键盘导航主要处理上、下、左、右、回车操作。
tip:模拟器调试请开启软键盘,以保证键盘事件的正确模拟。

"use strict";
define(function(require, exports, module) {
    var gameUtils = {};

    gameUtils.getByteLen = function(val) {
        var len = 0;
        for(var i = 0; i < val.length; i++) {
            var a = val.charAt(i);
            if(a.match(/[^\x00-\xff]/ig) != null) {
                len += 2;
            } else {
                len += 1;
            }
        }
        return len;
    }

    gameUtils.cutStr = function(str, len) {
        var res = "";
        var realLength = 0;
        for(var i = 0; i < str.length; i++) {
            var charCode = str.charCodeAt(i);
            if(charCode >= 0 && charCode <= 128) realLength += 1;
            else realLength += 2;
            if(realLength <= len) {
                res += str[i];
            } else {
                break;
            }
        }
        return res;
    }

    gameUtils.KeyNavigator = function(dimRow, dimCol) {
        if(!dimRow || !dimCol)
            return null;
        this.subNavs = [];
        this.currentSub = null;
        this.dimRow = dimRow;
        this.dimCol = dimCol;
        this.data = new Array();
        this.currentRow = -1;
        this.currentCol = -1;
        for(var i = 0; i < dimRow; i++) {
            this.data[i] = new Array();
        }
    };

    function SelButton(btn, onSel, onUnsel) {
        this.btn = btn;
        this.onSel = onSel;
        this.onUnsel = onUnsel;
    }

    gameUtils.KeyNavigator.prototype = {
        addSubNav: function(name, subNav) {
            if(subNav)
                this.subNavs[name] = subNav;
        },
        doSubNav: function(name) {
            this.hideCurrent();
            this.currentSub = this.subNavs[name];
        },
        endSubNav: function() {
            this.showCurrent();
            this.currentSub = null;
        },
        addButton: function(row, col, btn, onSel, onUnsel, beCurrent) {
            if(row < this.dimRow && col < this.dimCol) {
                this.data[row][col] = new SelButton(btn, onSel, onUnsel);
                if(beCurrent) {
                    this.setCurrent(row, col);
                }
                return true;
            } else {
                return false;
            }
        },
        showCurrent: function() {
            if(this.data[this.currentRow][this.currentCol]) {
                if(this.data[this.currentRow][this.currentCol].onSel)
                    this.data[this.currentRow][this.currentCol].onSel();
            }
        },
        hideCurrent: function() {
            if(this.data[this.currentRow][this.currentCol]) {
                if(this.data[this.currentRow][this.currentCol].onUnsel)
                    this.data[this.currentRow][this.currentCol].onUnsel();
            }

        },
        setCurrent: function(row, col) {
            try {
                if(this.data[row][col]) {
                    if(this.currentRow >= 0 && this.currentCol >= 0 && this.data[this.currentRow][this.currentCol]) {
                        if(this.data[this.currentRow][this.currentCol].onUnsel)
                            this.data[this.currentRow][this.currentCol].onUnsel();

                    }
                    this.currentRow = row;
                    this.currentCol = col;
                    if(this.data[this.currentRow][this.currentCol]) {
                        if(this.data[this.currentRow][this.currentCol].onSel)
                            this.data[this.currentRow][this.currentCol].onSel();
                    }
                    //console.log('current = ' + this.currentRow + " " + this.currentCol)
                    return true;
                } else {
                    return false;
                }
            } catch(err) {
                console.log('utils.KeyNavigator.setCurrent error!');
                throw err;
                return false;
            }
        },
        perform: function(cmd) {
            switch(cmd) {
                case "ENTER":
                    this.data[this.currentRow][this.currentCol].btn.onInputUp.dispatch();
                    break;
            }
        },
        navigate: function(dir) {
            if(this.currentRow < 0 || this.currentCol < 0) {
                return false;
            }
            //console.log('this.currentRow and Col = ' + this.currentRow + " " + this.currentCol)
            switch(dir) {
                case "UP":
                    if(this.currentRow < this.dimRow - 1) {
                        var col = -1;
                        for(var i = this.currentCol; i < this.dimCol; i++) {
                            if(this.data[this.currentRow + 1][i]) {
                                col = i;
                                break;
                            }
                        }
                        if(col < 0) {
                            for(var i = this.currentCol - 1; i >= 0; i--)
                                if(this.data[this.currentRow + 1][i]) {
                                    col = i;
                                    break;
                                }
                        }
                        if(col >= 0) {
                            this.setCurrent(this.currentRow + 1, col);
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    break;
                case "DOWN":
                    if(this.currentRow > 0) {
                        var col = -1;
                        for(var i = this.currentCol; i < this.dimCol; i++) {
                            if(this.data[this.currentRow - 1][i]) {
                                col = i;
                                break;
                            }
                        }
                        if(col < 0) {
                            for(var i = this.currentCol - 1; i >= 0; i--)
                                if(this.data[this.currentRow - 1][i]) {
                                    col = i;
                                    break;
                                }
                        }
                        if(col >= 0) {
                            this.setCurrent(this.currentRow - 1, col);
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    break;
                case "LEFT":
                    var col = this.currentCol - 1;
                    while(col >= 0 && !this.data[this.currentRow][col])
                        col--;
                    if(col >= 0) {
                        this.setCurrent(this.currentRow, col);
                    } else {
                        return false;
                    }
                    break;
                case "RIGHT":
                    var col = this.currentCol + 1;
                    while(col < this.dimCol && !this.data[this.currentRow][col])
                        col++;
                    if(col < this.dimCol) {
                        this.setCurrent(this.currentRow, col);
                    } else {
                        return false;
                    }
                    break;
                default:
                    return false;
            }

        }

    }
    return gameUtils;
});

你可能感兴趣的:(android机顶盒游戏开发(一))