Added client socket
This commit is contained in:
@@ -1,9 +1,26 @@
|
||||
<template>
|
||||
<div id="app">
|
||||
<router-view/>
|
||||
<Game v-if="isSocketConnected"/>
|
||||
<div v-else>
|
||||
<h1>Game loadin mate</h1>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Game from './components/Game'
|
||||
export default {
|
||||
name: 'App',
|
||||
components: {
|
||||
Game
|
||||
},
|
||||
computed: {
|
||||
isSocketConnected () {
|
||||
return this.$store.state.game.socketConnected
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style>
|
||||
#app {
|
||||
font-family: Avenir, Helvetica, Arial, sans-serif;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div class="home">
|
||||
<div class="game">
|
||||
<canvas
|
||||
class="game-canvas"
|
||||
ref="canvas">
|
||||
@@ -8,32 +8,16 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { send } from '@/store/socketPlugin'
|
||||
export default {
|
||||
name: 'Home',
|
||||
name: 'Game',
|
||||
data () {
|
||||
return {
|
||||
player: {
|
||||
x: 100,
|
||||
y: 100,
|
||||
angle: 0,
|
||||
targetAngle: 0,
|
||||
walls: [],
|
||||
lastWall: 0,
|
||||
color: 'red'
|
||||
},
|
||||
players: [],
|
||||
mouse: {
|
||||
x: 0,
|
||||
y: 0
|
||||
},
|
||||
settings: {
|
||||
playerSize: 10,
|
||||
playerSpeed: 2,
|
||||
playerTurnSpeed: 10,
|
||||
wallSize: 8,
|
||||
wallUpdate: 5,
|
||||
arenaSize: 1000
|
||||
}
|
||||
renderTimer: null
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
@@ -42,22 +26,30 @@ export default {
|
||||
},
|
||||
context () {
|
||||
return this.canvas.getContext('2d')
|
||||
},
|
||||
settings () {
|
||||
return this.$store.state.game.settings
|
||||
},
|
||||
player () {
|
||||
return this.$store.state.game.player
|
||||
},
|
||||
players () {
|
||||
return this.$store.state.game.players
|
||||
},
|
||||
isPaused () {
|
||||
return this.$store.state.game.paused
|
||||
}
|
||||
},
|
||||
mounted () {
|
||||
this.canvas.width = window.innerWidth
|
||||
this.canvas.height = window.innerHeight
|
||||
this.canvas.addEventListener('mousemove', this.mouseEvent)
|
||||
// setInterval(this.render, 1000 / 60)
|
||||
setInterval(this.render, 1000 / 60)
|
||||
setInterval(this.movePlayer, 100)
|
||||
this.renderTimer = setInterval(this.render, 1000 / 60)
|
||||
},
|
||||
methods: {
|
||||
render () {
|
||||
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
|
||||
this.renderBorders()
|
||||
this.renderPlayer(this.player)
|
||||
this.renderWalls(this.player.walls, this.player.color)
|
||||
|
||||
this.players.forEach(player => {
|
||||
this.renderPlayer(player)
|
||||
@@ -147,6 +139,7 @@ export default {
|
||||
var dy = this.mouse.y - this.canvas.height / 2
|
||||
|
||||
this.player.targetAngle = Math.atan2(dy, dx)
|
||||
send({ message: this.player, type: 'update' })
|
||||
},
|
||||
updatePlayer () {
|
||||
this.player.lastWall++
|
||||
@@ -1,11 +1,11 @@
|
||||
import Vue from 'vue'
|
||||
import App from './App.vue'
|
||||
import store from './store'
|
||||
import './registerServiceWorker'
|
||||
import router from './router'
|
||||
|
||||
Vue.config.productionTip = false
|
||||
|
||||
new Vue({
|
||||
router,
|
||||
store,
|
||||
render: h => h(App)
|
||||
}).$mount('#app')
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import Vue from 'vue'
|
||||
import VueRouter from 'vue-router'
|
||||
import Home from '../views/Home.vue'
|
||||
|
||||
Vue.use(VueRouter)
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'Home',
|
||||
component: Home
|
||||
}
|
||||
]
|
||||
|
||||
const router = new VueRouter({
|
||||
routes
|
||||
})
|
||||
|
||||
export default router
|
||||
69
client/src/store/gameModule.js
Normal file
69
client/src/store/gameModule.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const state = {
|
||||
player: {
|
||||
x: 100,
|
||||
y: 100,
|
||||
angle: 0,
|
||||
targetAngle: 0,
|
||||
walls: [],
|
||||
lastWall: 0,
|
||||
color: 'red'
|
||||
},
|
||||
players: {
|
||||
|
||||
},
|
||||
settings: {
|
||||
playerSize: 10,
|
||||
playerSpeed: 2,
|
||||
playerTurnSpeed: 10,
|
||||
wallSize: 8,
|
||||
wallUpdate: 5,
|
||||
arenaSize: 1000
|
||||
},
|
||||
paused: true,
|
||||
socketConnected: false
|
||||
}
|
||||
|
||||
const getters = {
|
||||
}
|
||||
|
||||
const actions = {
|
||||
socketConnected ({ commit }) {
|
||||
commit('SET_CONNECTED')
|
||||
},
|
||||
login ({ commit }, player) {
|
||||
commit('SET_PLAYER', player)
|
||||
},
|
||||
settings ({ commit }, settings) {
|
||||
commit('SET_SETTINGS', settings)
|
||||
},
|
||||
update ({ commit }, update) {
|
||||
},
|
||||
dead ({ commit }) {
|
||||
},
|
||||
error ({ commit }, error) {
|
||||
alert('Error: ' + error)
|
||||
}
|
||||
}
|
||||
|
||||
const mutations = {
|
||||
SET_PLAYER (state, player) {
|
||||
state.player = player
|
||||
},
|
||||
SET_SETTINGS (state, settings) {
|
||||
state.settings = settings
|
||||
},
|
||||
SET_PAUSE (state) {
|
||||
state.paused = !state.paused
|
||||
},
|
||||
SET_CONNECTED (state) {
|
||||
state.socketConnected = !state.socketConnected
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
getters,
|
||||
actions,
|
||||
mutations
|
||||
}
|
||||
15
client/src/store/index.js
Normal file
15
client/src/store/index.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import socket from './socketPlugin'
|
||||
import game from './gameModule'
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
export default new Vuex.Store({
|
||||
modules: {
|
||||
game
|
||||
},
|
||||
plugins: [
|
||||
socket()
|
||||
]
|
||||
})
|
||||
48
client/src/store/socketPlugin.js
Normal file
48
client/src/store/socketPlugin.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const connection = new WebSocket('ws://localhost:8181/socket')
|
||||
// const connection = new WebSocket('wss://tronio.gltronic.ovh/socket')
|
||||
|
||||
export default function createSocketPlugin () {
|
||||
return store => {
|
||||
connection.onopen = function () {
|
||||
console.log('[WS] connected')
|
||||
store.dispatch('game/socketConnected')
|
||||
}
|
||||
|
||||
connection.onclose = function () {
|
||||
console.log('[WS] closed')
|
||||
}
|
||||
|
||||
connection.onerror = function (error) {
|
||||
console.log('[WS] error ' + error.message)
|
||||
store.dispatch('game/error', 'Connection problem')
|
||||
}
|
||||
|
||||
connection.onmessage = function (message) {
|
||||
console.log('[WS] message', message.data)
|
||||
|
||||
var data = JSON.parse(message.data)
|
||||
|
||||
switch (data.type) {
|
||||
case 'login':
|
||||
store.dispatch('game/login', data.message)
|
||||
break
|
||||
case 'gameSettings':
|
||||
store.dispatch('game/settings', data.message)
|
||||
break
|
||||
case 'gameUpdate':
|
||||
store.dispatch('game/update', data.message)
|
||||
break
|
||||
case 'gamePlayerDead':
|
||||
store.dispatch('game/dead')
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function send (message) {
|
||||
console.log('[WS] send', message)
|
||||
connection.send(JSON.stringify(message))
|
||||
}
|
||||
Reference in New Issue
Block a user