Skip to content

Commit f4ea047

Browse files
committed
physics
1 parent 0611c11 commit f4ea047

File tree

9 files changed

+1707
-35
lines changed

9 files changed

+1707
-35
lines changed
Binary file not shown.
Binary file not shown.

game-dev-ai/dist/bundle.r.js

+1,242-29
Large diffs are not rendered by default.

game-dev-ai/dist/bundle.r.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

game-dev-ai/src/main.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ window.kickStart = function(){
1818
}
1919
};
2020

21-
//game.state.start('Map');
21+
22+
game.state.add('Col', require('./scenes/Col'));
23+
game.state.start('Col');

game-dev-ai/src/scenes/Col.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const Phaser = require('phaser');
2+
const MegaMan = require('../sprites/mm');
3+
4+
/**
5+
* @inherits Phaser.Game
6+
* @constructor
7+
*/
8+
function Col() {
9+
this.player = null;
10+
this.floor = null;
11+
this.jumpTimer = 0;
12+
this.keys = {};
13+
}
14+
15+
Col.prototype = {
16+
preload: function () {
17+
this.load.atlasJSONHash('mm', 'asset/img/megaman.gif', 'asset/sprites/megaman.json');
18+
this.load.image('ground', 'asset/img/ground_1x1.png');
19+
},
20+
create: function () {
21+
this.keys['jump'] = this.input.keyboard.addKey(Phaser.Keyboard.A);
22+
this.keys['up'] = this.input.keyboard.addKey(Phaser.Keyboard.UP);
23+
this.keys['down'] = this.input.keyboard.addKey(Phaser.Keyboard.DOWN);
24+
this.keys['left'] = this.input.keyboard.addKey(Phaser.Keyboard.LEFT);
25+
this.keys['right'] = this.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
26+
27+
this.floor = this.add.sprite(this.world.centerX / 2, this.world.centerY + 75, 'ground');
28+
29+
this.player = new MegaMan(this, this.world.centerX, 0, 'mm', {scale: {x: 2, y: 2}});
30+
this.player.jumping = false;
31+
32+
this.physics.startSystem(Phaser.Physics.ARCADE);
33+
this.physics.arcade.gravity.y = 1000;
34+
35+
this.physics.enable([this.player.sprite, this.floor], Phaser.Physics.ARCADE);
36+
37+
this.player.sprite.body.collideWorldBounds = true;
38+
39+
this.floor.body.allowGravity = false;
40+
this.floor.body.immovable = true;
41+
},
42+
update: function () {
43+
this.player.update(this, this.keys);
44+
this.physics.arcade.collide(this.player.sprite, this.floor, this.collisionHandler);
45+
46+
const touchingDown = this.player.sprite.body.onFloor() || this.player.sprite.body.touching.down;
47+
48+
this.player.sprite.body.velocity.x = 0;
49+
50+
if (this.keys['left'].isDown) {
51+
this.player.sprite.body.velocity.x = -150;
52+
} else if (this.keys['right'].isDown) {
53+
this.player.sprite.body.velocity.x = 150;
54+
}
55+
56+
if (this.keys['jump'].isDown && touchingDown && !this.player.jumping) {
57+
this.player.sprite.body.velocity.y = -550;
58+
this.player.jumping = true;
59+
}
60+
61+
if (touchingDown && this.keys['jump'].isUp) {
62+
this.player.jumping = false;
63+
}
64+
65+
if (touchingDown) {
66+
this.player.standing = true;
67+
} else {
68+
this.player.standing = false;
69+
}
70+
}
71+
};
72+
73+
module.exports = Col;

game-dev-ai/src/scenes/Map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Map.prototype.init = function(){
112112

113113
Map.prototype.preload = function(){
114114
this.load.spritesheet('mm3-wily-02', 'asset/img/mm3-wily-02.png', 32, 32);
115-
this.load.atlasJSONHash('mm', 'asset/img/megaman.gif', '/asset/sprites/megaman.json');
115+
this.load.atlasJSONHash('mm', 'asset/img/megaman.gif', 'asset/sprites/megaman.json');
116116
};
117117

118118
Map.prototype.create = function () {

game-dev-ai/src/sprites/mm.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function MegaMan(game, x, y, texture, opt){
77
this.anim = animMM.anim;
88
this.facingRight = true;
99
this.jumping = false;
10+
this.standing = true;
1011

1112
this.init(opt);
1213
}
@@ -44,11 +45,11 @@ MegaMan.prototype.update = function(game, keys){
4445
left: {}
4546
};
4647

47-
if (keys.jump.isDown || this.jumping) {
48+
if ((keys.jump.isDown || this.jumping) && !this.standing) {
4849
sprite.animations.play(this.facingRight ? states.jumpingRight : states.jumpingLeft);
49-
} else if (keys.right.isDown && !this.jumping) {
50+
} else if (keys.right.isDown && !this.jumping && this.standing) {
5051
sprite.animations.play(states.runningRight);
51-
} else if (keys.left.isDown && !this.jumping) {
52+
} else if (keys.left.isDown && !this.jumping && this.standing) {
5253
sprite.animations.play(states.runningLeft);
5354
} else {
5455
sprite.animations.play(this.facingRight ? states.standingRight : states.standingLeft);

0 commit comments

Comments
 (0)