|
import Phaser from 'phaser'; |
|
import axios from 'axios'; |
|
|
|
const config = { |
|
type: Phaser.AUTO, |
|
width: 800, |
|
height: 600, |
|
scene: { |
|
preload: preload, |
|
create: create, |
|
update: update |
|
} |
|
}; |
|
|
|
const game = new Phaser.Game(config); |
|
|
|
let score = 0; |
|
let scoreText; |
|
|
|
function preload() { |
|
|
|
} |
|
|
|
function create() { |
|
|
|
"link": "https://phaser.io/examples/v3/category/physics/arcade", |
|
"user_has_request": false |
|
} |
|
scoreText = this.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#000' }); |
|
|
|
|
|
words = this.physics.add.group(); |
|
|
|
|
|
sentence.split(' ').forEach((word, index) => { |
|
let wordObject = this.physics.add.sprite(100 + index * 50, 100, 'word'); |
|
wordObject.setBounce(0.2); |
|
wordObject.setCollideWorldBounds(true); |
|
wordObject.word = word; |
|
words.add(wordObject); |
|
}); |
|
|
|
|
|
categories = this.physics.add.staticGroup(); |
|
|
|
|
|
['sustantivo', 'verbo', 'adjetivo', 'artículo'].forEach((category, index) => { |
|
let categoryObject = this.physics.add.sprite(100 + index * 200, 500, 'category'); |
|
categoryObject.category = category; |
|
categories.add(categoryObject); |
|
}); |
|
|
|
|
|
this.physics.add.collider(words, categories, checkCategorization, null, this); |
|
} |
|
|
|
function checkCategorization(wordObject, categoryObject) { |
|
|
|
axios.post('/api/game_logic', { |
|
sentence: sentence, |
|
user_word: wordObject.word, |
|
user_pos: categoryObject.category |
|
}) |
|
.then(function (response) { |
|
let correct = response.data.correct; |
|
if (correct) { |
|
increaseScore(10); |
|
wordObject.destroy(); |
|
} else { |
|
|
|
let hint = response.data.hint; |
|
displayHint(hint); |
|
} |
|
}) |
|
.catch(function (error) { |
|
console.log(error); |
|
}); |
|
} |
|
|
|
function update() { |
|
|
|
scoreText.setText(`Score: ${score}`); |
|
} |
|
|
|
function increaseScore(points) { |
|
score += points; |
|
} |
|
|
|
function displayHint(hint) { |
|
|
|
} |
|
|