Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
op_16
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
template admin
op_16
Commits
3b71b170
Commit
3b71b170
authored
Nov 30, 2021
by
范雪寒
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 棋子随机移动
parent
ae481e4c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
13 deletions
+67
-13
NetworkHelper.ts
assets/op_16/scene/NetworkHelper.ts
+6
-9
op_16.ts
assets/op_16/scene/op_16.ts
+61
-4
No files found.
assets/op_16/scene/NetworkHelper.ts
View file @
3b71b170
...
...
@@ -9,9 +9,9 @@ export class NetworkHelper {
this
.
_eventListeners
[
eventName
]
=
func
;
}
async
init
()
{
async
init
(
roomType
:
string
,
maxPlayers
:
number
)
{
await
this
.
initRoom
();
return
await
this
.
joinRoom
();
return
await
this
.
joinRoom
(
roomType
,
maxPlayers
);
}
async
startGame
()
{
...
...
@@ -79,22 +79,19 @@ export class NetworkHelper {
});
}
joinRoom
()
{
joinRoom
(
roomType
:
string
,
maxPlayers
:
number
,
customData
:
string
=
''
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
playerName
=
"
Tom
"
+
Math
.
random
();
const
playerInfo
=
{
name
:
playerName
,
customPlayerStatus
:
0
,
customProfile
:
JSON
.
stringify
({
x
:
RandomInt
(
-
640
,
640
),
y
:
RandomInt
(
-
360
,
360
)
}),
customProfile
:
customData
,
};
const
matchRoomPara
=
{
playerInfo
,
maxPlayers
:
5
,
roomType
:
"
1
"
maxPlayers
:
maxPlayers
,
roomType
:
roomType
};
this
.
room
.
matchRoom
(
matchRoomPara
,
event
=>
{
...
...
assets/op_16/scene/op_16.ts
View file @
3b71b170
import
{
asyncDelay
,
onHomeworkFinish
}
from
"
../script/util
"
;
import
{
asyncDelay
,
onHomeworkFinish
,
RandomInt
}
from
"
../script/util
"
;
import
{
MyCocosSceneComponent
}
from
"
../script/MyCocosSceneComponent
"
;
import
{
NetworkHelper
}
from
"
./NetworkHelper
"
;
const
{
ccclass
,
property
}
=
cc
.
_decorator
;
...
...
@@ -27,8 +28,64 @@ export default class SceneComponent extends MyCocosSceneComponent {
this
.
initListener
();
}
initData
()
{
playerId
;
networkHelper
:
NetworkHelper
;
async
initData
()
{
this
.
networkHelper
=
new
NetworkHelper
();
await
this
.
networkHelper
.
initRoom
();
this
.
playerId
=
await
this
.
networkHelper
.
joinRoom
(
'
op_16
'
,
2
);
const
roomInfo
=
this
.
networkHelper
.
getRoomInfo
();
if
(
roomInfo
.
playerList
.
length
==
roomInfo
.
maxPlayers
)
{
if
(
this
.
playerId
==
roomInfo
.
playerList
[
0
].
id
)
{
this
.
networkHelper
.
startGame
();
}
}
roomInfo
.
playerList
.
forEach
(
player
=>
{
// const startPos = JSON.parse(player.customProfile);
// this.createTank(player.id, startPos);
});
this
.
networkHelper
.
on
(
'
playerJoin
'
,
(
event
)
=>
{
const
roomInfo
=
this
.
networkHelper
.
getRoomInfo
();
if
(
roomInfo
.
playerList
.
length
==
roomInfo
.
maxPlayers
)
{
if
(
this
.
playerId
==
roomInfo
.
playerList
[
0
].
id
)
{
this
.
networkHelper
.
startGame
();
}
}
console
.
log
(
"
playerJoin
"
,
event
);
});
this
.
networkHelper
.
on
(
'
playerLeave
'
,
(
event
)
=>
{
console
.
log
(
"
playerLeave
"
,
event
);
});
this
.
networkHelper
.
on
(
'
gameStart
'
,
(
event
)
=>
{
setInterval
(()
=>
{
this
.
networkHelper
.
sendFrame
({
id
:
this
.
playerId
,
pos
:
{
x
:
RandomInt
(
-
300
,
300
),
y
:
RandomInt
(
-
300
,
300
)
}
});
},
1000
);
});
this
.
networkHelper
.
on
(
'
frameEvent
'
,
(
event
)
=>
{
// console.log("frameEvent", event);
const
roomInfo
=
this
.
networkHelper
.
getRoomInfo
();
event
.
data
.
frame
.
items
.
forEach
(
item
=>
{
let
node
;
if
(
item
.
data
.
id
==
roomInfo
.
playerList
[
0
].
id
)
{
node
=
cc
.
find
(
'
Canvas/bg/chessBoard/pieceBlue
'
);
}
else
{
node
=
cc
.
find
(
'
Canvas/bg/chessBoard/pieceRed
'
);
}
cc
.
tween
(
node
)
.
to
(
0.1
,
{
x
:
item
.
data
.
pos
.
x
,
y
:
item
.
data
.
pos
.
y
})
.
start
();
});
});
}
initView
()
{
...
...
@@ -39,7 +96,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
const
canvas
=
cc
.
find
(
'
Canvas
'
);
const
bgImg
=
cc
.
find
(
'
Canvas/bgImg
'
);
bgImg
.
scale
=
Math
.
max
(
canvas
.
width
/
bgImg
.
width
,
canvas
.
height
/
bgImg
.
height
);
const
bg
=
cc
.
find
(
'
Canvas/bg
'
);
bg
.
scale
=
Math
.
min
(
canvas
.
width
/
bg
.
width
,
canvas
.
height
/
bg
.
height
);
}
...
...
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