Commit 44a684fb authored by wildfirecode's avatar wildfirecode

1

parent 7395d708
......@@ -27,6 +27,24 @@
}
],
"children": [
{
"name": "pic",
"components": [
{
"script": "components/base/Transform",
"properties": {
"alpha": 0
}
},
{
"script": "components/renderer/TextureRenderer",
"properties": {
"texture": "res|5367ae30-00ee-4296-8779-158f8fbd68f2"
}
}
],
"uuid": "fb53fa37-a942-4474-901a-582dccd14c93"
},
{
"name": "blue",
"components": [
......@@ -41,7 +59,7 @@
}
}
],
"uuid": "fb53fa37-a942-4474-901a-582dccd14c93"
"uuid": "bd5eee18-b584-4e7b-817f-17924c204a24"
},
{
"name": "purple",
......
......@@ -3,9 +3,11 @@ import { Entity } from 'scilla/src';
import Body from './Body';
export default class BallItem extends Body {
private score = 0;
private _storedScore;
private _currentScore = 0;
scoreTxt: Entity;
isBig = true;
size: BallSizeType;
constructor() {
super();
this.gravity = 0.225;
......@@ -14,24 +16,54 @@ export default class BallItem extends Body {
onAwake() {
super.onAwake();
}
reduceScore() {
this.score--;
if (this.score < 0)
this.score = 0;
this._currentScore--;
if (this._currentScore < 0)
this._currentScore = 0;
this.updateScoreTxt();
return this.score;
return this._currentScore;
}
resetScore (score){
this.score = score;
resetScore(score) {
this._currentScore = score;
this._storedScore = score;
this.updateScoreTxt();
}
updateScoreTxt() {
const tr = this.scoreTxt.getComponent(TextRenderer);
tr.text = this.score.toString();
tr.text = this._currentScore.toString();
}
childNames = ['blue', 'purple', 'red', 'green', 'darkBlue', 'yellow'];
color: BallType;
updateColor(color: BallType) {
this.color = color;
this.childNames.forEach(name => {
const child = this.entity.getChildrenByName(name)[0];
if (name != color)
child.enabled = false;
});
const child = this.entity.getChildrenByName(color)[0];
child.enabled = true;
}
get storedScore() { return this._storedScore }
}
export enum BallSizeType {
min = 0,
size1,
size2,
max,
}
export enum BallType {
blue = 'blue',
purple = 'purple',
red = 'red',
green = 'green',
darkBlue = 'darkBlue',
yellow = 'yellow',
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ import Transform from "scilla-components/src/base/Transform";
import { Entity, instantiate, resource } from "scilla/src";
import { alien } from "../navigator/StackNavigator";
import { INavigatorViewBase } from "../navigator/VirtualNavigator";
import { getX, getY, setX, setXY } from "../transformUtils";
import { getX, getY, setX, setXY, getScale } from "../transformUtils";
import BallItem from "./BallItem";
import Body from "./Body";
import BallManager from "./scenePlay/BallManager";
......@@ -24,7 +24,7 @@ export default class ScenePlay extends InteractComponent implements INavigatorVi
score = 0;
drops: Body[];
_carShootAni: CarShootAni;
_bulletManager: CarBulletAni;
_ballManager: BallManager;
......@@ -66,8 +66,8 @@ export default class ScenePlay extends InteractComponent implements INavigatorVi
const ball = ballList[j];
const x0 = Math.abs(getX(ball.entity) - getX(bullet.entity));
const y0 = Math.abs(getY(ball.entity) - getY(bullet.entity));
const { scale: ballScale } = ball.getComponent(Transform);
const { height: ballH } = ball.entity.getChildrenByName('blue')[0].getComponent(Transform);
const ballScale = getScale(ball);
const { height: ballH } = ball.entity.getChildrenByName(ball.color)[0].getComponent(Transform);
const x1 = bulletWidth / 2 + ballH / 2 * ballScale.x;
const y1 = bulletHeight / 2 + ballH / 2 * ballScale.x;
if (x0 < x1 && y0 < y1) { //子弹和球碰撞了,不再便利其他球
......@@ -83,7 +83,7 @@ export default class ScenePlay extends InteractComponent implements INavigatorVi
}
onBallSplit(ball: BallItem) {
this._ballManager.removeBall(ball);
this._ballManager.onBallSplit(ball);
}
checkBallCollideGroud() {
......@@ -91,9 +91,9 @@ export default class ScenePlay extends InteractComponent implements INavigatorVi
for (let i = 0; i < ballList.length; i++) {
const ball = ballList[i];
const { position } = ball.getComponent(Transform);
const pic = ball.entity.getChildrenByName('blue')[0];
const { height, scale } = pic.getComponent(Transform);
const r = height / 2 * scale.y;//刚体的半径
const pic = ball.entity.getChildrenByName(ball.color)[0];
const { height } = pic.getComponent(Transform);
const r = height / 2 * getScale(ball).x;//刚体的半径 213
if (position.y + r > getY(this.bottomline)) {
position.y = getY(this.bottomline) - r;
ball.revertY();
......@@ -149,7 +149,7 @@ export default class ScenePlay extends InteractComponent implements INavigatorVi
setInterval(() => {
this._ballManager.createBigBall();
}, 1 * 1000);
}, 2 * 1000);
}
onGameOver() {
......
......@@ -4,4 +4,9 @@ export const removeFromList = (item, list: any[]) => {
list.splice(index, 1);
}
return item;
}
export const pickFromList = (list: any[]) => {
const index = Math.floor(Math.random() * list.length);
return list[index]
}
\ No newline at end of file
import { getTween } from 'assets/scripts/getTween';
import { getY, setXY } from 'assets/scripts/transformUtils';
import { getY, setXY, setX, setScale, getX } from 'assets/scripts/transformUtils';
import ScillaComponent from 'components/base/ScillaComponent';
import { Transform } from 'scilla-components/src';
import { Entity, instantiate, resource } from 'scilla/src';
import BallItem from '../BallItem';
import { removeFromList } from '../_';
import BallItem, { BallSizeType } from '../BallItem';
import { removeFromList, pickFromList } from '../_';
export default class BallManager extends ScillaComponent {
removeBall(ball: BallItem) {
onBallSplit(ball: BallItem) {
ball.enabled = false;
removeFromList(ball, this.ballList);
this.entity.removeChild(ball.entity);
this._freeBallList.push(ball);
console.log('this.ballList', this.ballList.length, 'this._freeBallList', this._freeBallList.length)
console.log('this.ballList', this.ballList.length, 'this._freeBallList', this._freeBallList.length);
//开始分裂
if (ball.size > BallSizeType.min) {
const ball1 = this.createSplitedBall(ball);
const ball2 = this.createSplitedBall(ball);
ball1.velocityX = -5;
ball2.velocityX = 5;
}
}
BallItemPrefab: resource;
......@@ -21,28 +29,53 @@ export default class BallManager extends ScillaComponent {
ballList: BallItem[];
private _freeBallList: BallItem[];
createSplitedBall(parentBall: BallItem) {
const score = Math.ceil(parentBall.storedScore / 2);
const ball = this.getBall();
ball.reset();
ball.resetScore(score);
ball.updateColor(parentBall.color)
setScale(ball, .5);
setXY(ball, getX(parentBall), getY(parentBall));
this.ballList.push(ball);
this.entity.addChild(ball.entity);
ball.enabled = true;
ball.velocityY = -10;
ball.size = BallSizeType.min;
return ball;
}
async createBigBall() {
const score = 20;
let ball: BallItem;
if (this._freeBallList.length > 0) {
ball = this._freeBallList.pop();
ball.reset();
} else {
ball = this.getBallItem();
console.log('ball created')
}
const score = Math.ceil(20 + Math.random() * 20);
const ball = this.getBall();
ball.reset();
ball.resetScore(score);
ball.updateColor(pickFromList(ball.childNames))
// setScale(ball, scale);
let dir = Math.random() > 0.5 ? -1 : 1;
setXY(ball.entity, -375, getY(this.topline));
ball.enabled = false;
this.ballList.push(ball);
this.entity.addChild(ball.entity);
ball.size = BallSizeType.max;
await getTween(this, ball.getComponent(Transform), { x: -375 + 200 }, 700);
ball.velocityX = 5;
ball.enabled = true;
}
getBallItem() {
getBall() {
let ball: BallItem;
if (this._freeBallList.length > 0) {
ball = this._freeBallList.pop();
} else {
ball = this.createBall();
console.log('ball created')
}
return ball;
}
createBall() {
const ball = instantiate(this.BallItemPrefab);
const body = ball.getComponent(BallItem);
return body;
......
import { Entity } from 'scilla/src';
import ScillaComponent from 'components/base/ScillaComponent';
import { Transform } from 'scilla-components/src';
import { Entity } from 'scilla/src';
export const setX = (entity: Entity, x) => {
entity.getComponent(Transform).position.x = x;
}
export const setScale = (val: Entity | ScillaComponent, x) => {
if (val instanceof Entity) {
val.getComponent(Transform).scale.x = x;
val.getComponent(Transform).scale.y = x;
}
else if (val instanceof ScillaComponent) {
val.entity.getComponent(Transform).scale.x = x;
val.entity.getComponent(Transform).scale.y = x;
}
}
export const getScale = (val: Entity | ScillaComponent) => {
if (val instanceof Entity) {
return val.getComponent(Transform).scale;
}
else if (val instanceof ScillaComponent) {
return val.getComponent(Transform).scale;
}
}
export const setY = (entity: Entity, y) => {
entity.getComponent(Transform).position.y = y;
}
export const getX = (entity: Entity) => {
// if(!entity.getComponent(Transform))debugger
export const getX = (val: Entity | ScillaComponent) => {
let entity: Entity;
if (val instanceof Entity)
entity = val;
else
entity = val.entity;
return entity.getComponent(Transform).position.x
}
export const getY = (entity: Entity) => {
return entity.getComponent(Transform).position.y;
export const getY = (val: Entity | ScillaComponent) => {
let entity: Entity;
if (val instanceof Entity)
entity = val;
else
entity = val.entity;
return entity.getComponent(Transform).position.y
}
export const setXY = (entity: Entity,x, y) => {
setX(entity,x);
setY(entity,y);
export const setXY = (val: Entity | ScillaComponent, x, y) => {
let entity: Entity;
if (val instanceof Entity)
entity = val;
else
entity = val.entity;
setX(entity, x);
setY(entity, y);
}
\ No newline at end of file
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment