Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
db-game-template
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔立强
db-game-template
Commits
9072b9dc
Commit
9072b9dc
authored
Sep 25, 2018
by
wildfirecode
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
52ef2046
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
492 additions
and
688 deletions
+492
-688
.DS_Store
egret/libs/.DS_Store
+0
-0
p2Physics.d.ts
egret/libs/p2Physics.d.ts
+356
-670
.DS_Store
egret/resource/.DS_Store
+0
-0
Ball.ts
egret/src/Ball.ts
+8
-2
Brick.ts
egret/src/Brick.ts
+35
-0
Gun.ts
egret/src/Gun.ts
+28
-6
Main.ts
egret/src/Main.ts
+24
-5
World.ts
egret/src/World.ts
+39
-4
consts.ts
egret/src/consts.ts
+2
-1
No files found.
egret/libs/.DS_Store
0 → 100644
View file @
9072b9dc
File added
egret/libs/physics.d.ts
→
egret/libs/p
2P
hysics.d.ts
View file @
9072b9dc
This diff is collapsed.
Click to expand it.
egret/resource/.DS_Store
View file @
9072b9dc
No preview for this file type
egret/src/Ball.ts
View file @
9072b9dc
...
@@ -3,8 +3,14 @@ import { FACTOR } from "./consts";
...
@@ -3,8 +3,14 @@ import { FACTOR } from "./consts";
export
default
class
Ball
extends
p2
.
Body
{
export
default
class
Ball
extends
p2
.
Body
{
_skin
:
egret
.
DisplayObject
;
_skin
:
egret
.
DisplayObject
;
setImpulse
:
boolean
;
constructor
()
{
super
({
mass
:
10
});
this
.
type
=
p2
.
Body
.
DYNAMIC
;
this
.
init
();
}
init
()
{
private
init
()
{
this
.
_skin
=
addImage
(
'ball_png'
);
this
.
_skin
=
addImage
(
'ball_png'
);
this
.
_skin
.
anchorOffsetX
=
this
.
_skin
.
width
/
2
;
this
.
_skin
.
anchorOffsetX
=
this
.
_skin
.
width
/
2
;
this
.
_skin
.
anchorOffsetY
=
this
.
_skin
.
height
/
2
;
this
.
_skin
.
anchorOffsetY
=
this
.
_skin
.
height
/
2
;
...
@@ -13,7 +19,7 @@ export default class Ball extends p2.Body {
...
@@ -13,7 +19,7 @@ export default class Ball extends p2.Body {
const
shape
=
new
p2
.
Circle
({
radius
:
radius
});
const
shape
=
new
p2
.
Circle
({
radius
:
radius
});
this
.
addShape
(
shape
);
this
.
addShape
(
shape
);
this
.
mass
=
1
;
shape
.
collisionGroup
=
2
;
//010与001为0,010与110为1
}
}
updateSkin
()
{
updateSkin
()
{
...
...
egret/src/Brick.ts
0 → 100644
View file @
9072b9dc
import
{
addImage
}
from
"./utils"
;
import
{
FACTOR
}
from
"./consts"
;
/**
* 被打的砖块
*/
export
default
class
Brick
extends
p2
.
Body
{
_skin
:
egret
.
DisplayObject
;
constructor
()
{
super
();
this
.
type
=
p2
.
Body
.
KINEMATIC
;
this
.
init
();
}
init
()
{
this
.
_skin
=
addImage
(
'rect1_png'
);
this
.
_skin
.
anchorOffsetX
=
this
.
_skin
.
width
/
2
;
this
.
_skin
.
anchorOffsetY
=
this
.
_skin
.
height
/
2
;
const
width
=
this
.
_skin
.
width
/
FACTOR
;
const
height
=
this
.
_skin
.
height
/
FACTOR
;
const
shape
=
new
p2
.
Box
({
width
:
width
,
height
:
height
});
this
.
addShape
(
shape
);
shape
.
collisionMask
=
6
;
//010与001为0,010与110为1
}
updateSkin
()
{
this
.
_skin
.
x
=
this
.
position
[
0
]
*
FACTOR
;
this
.
_skin
.
y
=
this
.
position
[
1
]
*
FACTOR
;
this
.
_skin
.
rotation
=
this
.
angle
*
180
/
Math
.
PI
;
}
get
skin
()
{
return
this
.
_skin
}
}
\ No newline at end of file
egret/src/Gun.ts
View file @
9072b9dc
import
{
addImage
}
from
"./utils"
;
import
{
addImage
}
from
"./utils"
;
import
Ball
from
"./Ball"
;
import
Ball
from
"./Ball"
;
import
World
from
"./World"
;
import
World
from
"./World"
;
import
{
FACTOR
}
from
"./consts"
;
import
Brick
from
"./Brick"
;
export
default
class
Gun
extends
egret
.
Sprite
{
export
default
class
Gun
extends
egret
.
Sprite
{
_root
:
egret
.
DisplayObjectContainer
;
_root
:
egret
.
DisplayObjectContainer
;
_world
:
World
;
_world
:
World
;
_line
:
egret
.
Bitmap
;
_line
:
egret
.
Bitmap
;
_gun
:
egret
.
Bitmap
;
_gun
:
egret
.
Bitmap
;
vec
:
egret
.
Point
constructor
(
root
:
egret
.
DisplayObjectContainer
,
world
:
World
)
{
constructor
(
root
:
egret
.
DisplayObjectContainer
,
world
:
World
)
{
super
();
super
();
this
.
_root
=
root
;
this
.
_root
=
root
;
...
@@ -14,18 +17,28 @@ export default class Gun extends egret.Sprite {
...
@@ -14,18 +17,28 @@ export default class Gun extends egret.Sprite {
this
.
initUI
();
this
.
initUI
();
}
}
fire
()
{
fire
(
vec
:
egret
.
Point
)
{
egret
.
Tween
.
get
(
this
.
_gun
)
egret
.
Tween
.
get
(
this
.
_gun
)
.
to
({
scaleY
:
0.6
},
100
)
.
to
({
scaleY
:
0.6
},
100
)
.
to
({
scaleY
:
1
},
100
);
.
to
({
scaleY
:
1
},
100
);
this
.
addBall
();
const
ball
=
this
.
addBall
();
ball
.
mass
=
0
;
this
.
_world
.
bodies
.
forEach
(
body
=>
{
if
(
body
instanceof
Brick
)
{
this
.
_world
.
setMaterial
(
ball
,
body
);
}
})
ball
.
applyImpulse
([
vec
.
x
,
vec
.
y
],
[
0
,
0
]);
}
}
addBall
()
{
addBall
()
{
const
ball
=
new
Ball
();
const
ball
=
new
Ball
();
ball
.
init
();
ball
.
position
=
[
this
.
x
/
FACTOR
,
this
.
y
/
FACTOR
];
ball
.
skin
.
x
=
this
.
x
;
ball
.
skin
.
y
=
this
.
y
;
this
.
_world
.
addBody
(
ball
);
this
.
_world
.
addBody
(
ball
);
this
.
_root
.
addChild
(
ball
.
skin
);
this
.
_world
.
skin
.
addChild
(
ball
.
skin
);
return
ball
;
}
}
enable
()
{
enable
()
{
...
@@ -39,10 +52,19 @@ export default class Gun extends egret.Sprite {
...
@@ -39,10 +52,19 @@ export default class Gun extends egret.Sprite {
this
.
stage
.
once
(
egret
.
TouchEvent
.
TOUCH_END
,
this
.
onTouchEnd
,
this
);
this
.
stage
.
once
(
egret
.
TouchEvent
.
TOUCH_END
,
this
.
onTouchEnd
,
this
);
}
}
onTouchEnd
():
any
{
onTouchEnd
(
e
:
egret
.
TouchEvent
):
any
{
this
.
hideLine
();
this
.
hideLine
();
this
.
stage
.
removeEventListener
(
egret
.
TouchEvent
.
TOUCH_MOVE
,
this
.
onTouchMove
,
this
);
this
.
stage
.
removeEventListener
(
egret
.
TouchEvent
.
TOUCH_MOVE
,
this
.
onTouchMove
,
this
);
// this.fire();
const
point
=
new
egret
.
Point
(
e
.
stageX
,
e
.
stageY
)
var
pos
=
new
egret
.
Point
(
point
.
x
-
this
.
x
,
point
.
y
-
this
.
y
);
var
angle
:
number
=
Math
.
atan2
(
pos
.
x
,
pos
.
y
)
var
dis
:
number
=
10000
;
var
x
:
number
=
Math
.
ceil
(
Math
.
sin
(
angle
)
*
dis
);
var
y
:
number
=
Math
.
ceil
(
Math
.
cos
(
angle
)
*
dis
);
const
vec
=
new
egret
.
Point
(
x
,
y
);
this
.
vec
=
vec
;
this
.
fire
(
vec
);
}
}
onTouchMove
(
e
:
egret
.
TouchEvent
)
{
onTouchMove
(
e
:
egret
.
TouchEvent
)
{
...
...
egret/src/Main.ts
View file @
9072b9dc
import
{
getResPath
}
from
"./utils"
;
import
{
getResPath
}
from
"./utils"
;
import
Gun
from
"./Gun"
;
import
Gun
from
"./Gun"
;
import
World
from
"./World"
;
import
World
from
"./World"
;
import
Brick
from
"./Brick"
;
import
{
FACTOR
}
from
"./consts"
;
export
class
Main
extends
eui
.
UILayer
{
export
class
Main
extends
eui
.
UILayer
{
private
_gun
:
Gun
;
private
_gun
:
Gun
;
private
_world
:
World
;
private
_world
:
World
;
protected
createGameScene
():
void
{
protected
createGameScene
():
void
{
this
.
_world
=
new
World
(
this
);
this
.
_world
=
new
World
();
this
.
_gun
=
new
Gun
(
this
,
this
.
_world
);
this
.
_gun
=
new
Gun
(
this
,
this
.
_world
);
this
.
_world
.
gun
=
this
.
_gun
;
this
.
_world
.
skin
=
new
egret
.
Sprite
();
this
.
addChild
(
this
.
_world
.
skin
);
this
.
addChild
(
this
.
_gun
);
this
.
addChild
(
this
.
_gun
);
this
.
_gun
.
x
=
this
.
stage
.
stageWidth
>>
1
;
this
.
_gun
.
x
=
this
.
stage
.
stageWidth
>>
1
;
this
.
_gun
.
y
=
100
;
this
.
_gun
.
y
=
100
;
this
.
_gun
.
enable
();
this
.
_gun
.
enable
();
setInterval
(()
=>
{
this
.
_gun
.
fire
();
this
.
updateBrickBody
();
},
500
);
//添加游戏帧频事件
//添加游戏帧频事件
this
.
addEventListener
(
egret
.
Event
.
ENTER_FRAME
,
this
.
loop
,
this
);
this
.
addEventListener
(
egret
.
Event
.
ENTER_FRAME
,
this
.
loop
,
this
);
}
}
updateBrickBody
():
void
{
for
(
var
i
=
0
;
i
<
1
;
i
++
)
{
var
brick
=
new
Brick
();
var
x
=
this
.
stage
.
stageWidth
>>
1
;
var
y
=
1000
;
brick
.
position
=
[
x
/
FACTOR
,
y
/
FACTOR
];
brick
.
updateSkin
();
brick
.
angle
=
(
-
Math
.
random
()
*
Math
.
PI
/
4
)
+
(
Math
.
random
()
*
Math
.
PI
/
4
);
this
.
_world
.
skin
.
addChild
(
brick
.
skin
);
this
.
_world
.
addBody
(
brick
);
}
}
protected
createChildren
():
void
{
protected
createChildren
():
void
{
super
.
createChildren
();
super
.
createChildren
();
this
.
runGame
().
catch
(
e
=>
{
this
.
runGame
().
catch
(
e
=>
{
...
@@ -29,7 +48,7 @@ export class Main extends eui.UILayer {
...
@@ -29,7 +48,7 @@ export class Main extends eui.UILayer {
}
}
loop
()
{
loop
()
{
this
.
_world
.
step
(
1
);
this
.
_world
.
step
(
1
/
60
);
this
.
_world
.
loop
();
this
.
_world
.
loop
();
}
}
...
...
egret/src/World.ts
View file @
9072b9dc
import
Ball
from
"./Ball"
;
import
Ball
from
"./Ball"
;
import
Gun
from
"./Gun"
;
export
default
class
World
extends
p2
.
World
{
export
default
class
World
extends
p2
.
World
{
_root
:
egret
.
DisplayObjectContainer
;
material
:
p2
.
Material
;
//碰撞时的弹性变化
constructor
(
root
:
egret
.
DisplayObjectContainer
,
options
?:
any
)
{
_skin
:
egret
.
DisplayObjectContainer
;
gun
:
Gun
;
constructor
(
options
?:
any
)
{
super
(
options
);
super
(
options
);
this
.
_root
=
root
;
this
.
init
();
this
.
init
();
}
}
protected
onHitBegin
(
evt
):
void
{
var
ball
:
Ball
;
if
(
evt
.
bodyA
instanceof
Ball
)
ball
=
evt
.
bodyA
;
if
(
evt
.
bodyB
instanceof
Ball
)
ball
=
evt
.
bodyB
;
if
(
ball
&&
ball
.
mass
==
0
)
{
ball
.
setImpulse
=
true
;
//可以设置给小球冲量
ball
.
mass
=
200
;
}
if
(
ball
&&
ball
.
setImpulse
)
{
const
vec
=
this
.
gun
.
vec
;
ball
.
applyImpulse
([
vec
.
x
*
2
,
vec
.
y
],
[
0
,
0
]);
ball
.
setImpulse
=
false
;
}
}
/**
* 设置刚体碰撞的弹性
* */
setMaterial
(
body1
:
p2
.
Body
,
body2
:
p2
.
Body
):
void
{
body1
.
shapes
[
0
].
material
=
this
.
material
;
body2
.
shapes
[
0
].
material
=
this
.
material
;
var
roleAndStoneMaterial
=
new
p2
.
ContactMaterial
(
this
.
material
,
this
.
material
,
{
restitution
:
0.7
,
friction
:
0
});
//弹性,摩擦力
this
.
addContactMaterial
(
roleAndStoneMaterial
);
}
loop
():
any
{
loop
():
any
{
this
.
bodies
.
forEach
(
body
=>
{
this
.
bodies
.
forEach
(
body
=>
{
if
(
body
instanceof
Ball
)
{
if
(
body
instanceof
Ball
)
{
...
@@ -17,6 +47,11 @@ export default class World extends p2.World {
...
@@ -17,6 +47,11 @@ export default class World extends p2.World {
}
}
init
()
{
init
()
{
this
.
gravity
=
[
0
,
10
];
this
.
gravity
=
[
0
,
200
];
this
.
material
=
new
p2
.
Material
(
0
);
this
.
on
(
"beginContact"
,
this
.
onHitBegin
.
bind
(
this
));
}
}
set
skin
(
val
:
egret
.
DisplayObjectContainer
)
{
this
.
_skin
=
val
}
get
skin
()
{
return
this
.
_skin
}
}
}
\ No newline at end of file
egret/src/consts.ts
View file @
9072b9dc
export
const
FACTOR
=
30
;
export
const
FACTOR
=
1
;
\ No newline at end of file
// export const FACTOR = 30;
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment