0.1.5 • Published 9 months ago

dmc-gacha-game v0.1.5

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

使用 Matter-js 制作的抽奖小游戏模块

之前项目中使用Matter.js制作的抽奖小游戏,现在单独抽离成一个模块,参数可配置化,方便复用.

安装

npm install dmc-gacha-game --save-prod

使用

// 引用
import GachaGame from "dmc-gacha-game";
// 参数见下表
const option = {
    container: "#c-gacha-game-container",
    viewportWidth: 375,
    ballImages: [...],
    ballSpriteScale: 0.8,
    decorateImage: {...},
    separatorOffsetY: 0.12,
    canvasStyle: {
      left: 101.25,
      top: 0,
      width,
      height: width * 2.78,
    },
    // separatorWidth: 100,
    // jumpCount: 10,
    // jumpInterval: 500,
    // ballCount: 20,
    // ballRadius: Math.floor(width / 10),
    // devicePixelRatio: window.devicePixelRatio,
    debug: false,
    timeScale: true,
};
// 传入option初始化,返回game实例
const game = new GachaGame(option);
// 监听游戏状态变化
game.addEventListener("changeGameStatus", (status) => {
  // status 类型见下方代码
  console.log("status: ", status);
});
// 开始游戏
game.start();
// 重置游戏, <option> 可选,覆盖初始化的参数
game.reset(<option>);

Option

KeyExplanationTypeDefaultRequired
container游戏的容器元素,DOM 元素或 queryString.DOM / queryStringBodytrue
canvasStylecanvas 位置大小{left, top,width,height}Object{}true
viewportWidth示口大小,之后的所有大小都是以该值为基础来计算Number0-∞375true
debug开启 debug,装饰图片会变成半透明,可以看见 matter-js 游戏参数和图形,方便调试Booleanfalsefalse
timeScale弹跳时开启时间加速与放缓
ballImages球的精灵图数组{id:...,url:..}...或者imgUrl,....ArrayString/Objcet[]false
ballCount球的数量Number0-∞15false
ballRadius球的半径Number0-∞Math.floor(canvasStyle.width / 10)
devicePixelRatio设备像素比Number0-∞window.devicePixelRatio || 2false
gameOverTimeout游戏结束等待时间Number0-∞3000(m s)
ballSpriteScale球的精灵图的缩放比例,有时设计给精灵图的四周有空白区域,会导致贴图大小对不上,可以使用该参数调节Number0-11false
decorateImage装饰图片对象,包含了四个类型,见下方代码Object{}false
decorateImage.inner内饰图片Object{}false
decorateImage.exit出口图片Object{}false
decorateImage.button按钮图片Object{}false
decorateImage.body机身图片Object{}false
separatorOffsetY隔板 Y 轴偏差值Number0-10false
separatorWidth隔板宽度Number0-∞1000false
jumpCount球弹跳次数Number0-∞8false
jumpInterval弹跳时间间隔Number0-∞1000(m s)false
matterConfigMatter-js 配置参数,见下方代码Object{}false

decorateImage

const decorateImage = {
  inner: {
    url: "...",
    style: {
      width: 172.5,
      left: 105,
      top: 127.5,
    },
  },
  exit: {
    style: {
      width: 60,
      left: 101.25 + 0,
      top: 431.25,
    },
  },
  button: {
    style: {
      width: 75,
      left: 157.5,
      top: 340,
    },
    status: {
      "R,A,O": {
        url: `https://dfmz-new.oss-cn-shanghai.aliyuncs.com/manghe/img/38-theme/%E8%89%B2%E7%9B%B8%EF%BC%8F%E9%A5%B1%E5%92%8C%E5%BA%A6%202%20%E6%8B%B7%E8%B4%9D%206%402x.png`,
        style: {
          width: 75,
          left: 157.5,
          top: 340,
        },
      },
    },
    onClick(e) {
      if (this.gameStatus === "N") {
        this.start();
      } else {
        this.reset();
      }
    },
  },
  body: {
    url: "...",
    style: {
      width: 375,
      left: 0,
      top: 0,
    },
  },
};

matterConfig

matterConfig: {
      EngineCreate: {
        gravity: {
          scale: 0.002, // 重力比例
        },
      },
      RenderCreate: {
        hasBounds: true,
        options: {
          wireframes: false,
          showPerformance: false,
          background: "rgba(0,0,0,0)",
          // showAngleIndicator: true,
          // showCollisions: true,
          // showVelocity: true,
        },
      },
      RenderLookAt: {
        min: { x: 0, y: 0 },
      },
      RunnerCreate: {},
      BodiesCircle: {
        //   density: 1, //刚体的质量
        frictionAir: 0.0, //表示物体空气摩擦力(空气阻力)的数字
        restitution: 0.8, // 反弹动能系数
        friction: 0.0001, //表示物体摩擦力的数字
      },
    },
}

gameStatusMap

{
      L: {
        disc: "load",
      },
      N: {
        desc: "not started",
      },
      R: {
        desc: "running",
      },
      A: {
        desc: "await",
      },
      O: {
        desc: "over",
      },
}
0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago