-
Notifications
You must be signed in to change notification settings - Fork 5
/
V1.html
193 lines (152 loc) · 6.31 KB
/
V1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!DOCTYPE html>
<html>
<head>
<center><script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script></center>
</head>
<body style="background-color:black;">
<script>
//var Swidth = screen.width;
// var Sheight = screen.height;
var Sheight = window.screen.availHeight;
var Swidth = window.screen.availWidth;
var config = {
type: Phaser.AUTO,
width: Swidth*1,
height: Sheight*.85,
// game.scale.pageAlignHorizontally = true;
// game.scale.pageAlignVertically = true;
//game.scale.refresh();
physics: {
default: 'arcade',
},
scene: {
preload: preload,
create: create,
update: update,
}
};
var game = new Phaser.Game(config);
//Game Objects
var platforms;
var map;
var player;
var seacreatures = [];
//Keyboard controls
var cursors;
var keys;
var space;
var graphics;
function preload()
{
this.load.image('sky', 'https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/thumbnails%2Fdeepblue.png?1658777114449');
this.load.image('platform', 'https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/thumbnails%2Fplatform.png?1658777114483');
this.load.atlas('slime', 'https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/slimesheet.png?1658777114399', 'slimesheet.json');
this.load.atlas('seacreatures', 'https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/thumbnails%2Fseacreatures_json.png?1658777114673', 'seacreatures_json.json');
}
function create()
{
//Set the background origin to be at (0, 0) or top left corner of the image rather than the center of the image asset
let background = this.add.tileSprite(0, 0, game.scale.width, game.scale.height, 'sky').setOrigin(0, 0);
//create all animations
this.anims.create({key: 'jellyfish', frames: this.anims.generateFrameNames('seacreatures', {prefix: 'blueJellyfish', end: 32, zeroPad: 4}), repeat: -1});
this.anims.create({key: 'octopus', frames: this.anims.generateFrameNames('seacreatures', {prefix: 'octopus', end: 24, zeroPad: 4}), repeat: -1});
let playerAnimations = {};
var idle = this.anims.create({key: 'slime_idle', frames: this.anims.generateFrameNames('slime', {prefix: 'slime_idle', end: 5, zeroPad: 2}), repeat: -1, frameRate: 10});
playerAnimations['idle'] = idle;
var left = this.anims.create({key: 'slime_left', frames: this.anims.generateFrameNames('slime', {prefix: 'slime_left', end: 5, zeroPad: 2}), repeat: -1, frameRate: 10});
playerAnimations['left'] = left;
var right = this.anims.create({key: 'slime_right', frames: this.anims.generateFrameNames('slime', {prefix: 'slime_right', end: 5, zeroPad: 2}), repeat: -1, frameRate: 10});
playerAnimations['right'] = right;
//Create the platforms and the player character set to collide with the platforms
createPlatforms(this);
player = new Player(this, 400, 400, 'slime', playerAnimations);
this.physics.add.collider(player, platforms);
player.play(player.animations['idle']);
let jellyfish = new SeaCreature(this, 200, 150, 'seacreatures', 'jellyfish');
jellyfish.addTween(this, jellyfish.x, jellyfish.y+20, 2000);
seacreatures.push(jellyfish);
let octopus = new SeaCreature(this, 700, 300, 'seacreatures', 'octopus');
octopus.addTween(this, octopus.x+400, octopus.y, 2000);
seacreatures.push(octopus);
//Set up user input
cursors = this.input.keyboard.createCursorKeys();
keys = this.input.keyboard.addKeys('A, D');
space = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
//space.on('down', jump);
graphics = this.add.graphics();
}
function createPlatforms(scene)
{
platforms = scene.physics.add.staticGroup();
//basePlatform is the floor of the game
let basePlatform = platforms.create(game.scale.width/2, game.scale.height-30, 'platform');
basePlatform.setScale(3, 1).refreshBody();
platforms.create(250, 250, 'platform');
platforms.create(950, 400, 'platform');
}
function update()
{
//Player will not move in the x-axis unless a movement key is being pressed
//player.setVelocityX(0);
//Player has "drag" on the x-axis meaning they slide a bit after an input
player.setDragX(1500);
//Handle player movements
if (cursors.left.isDown || keys.A.isDown)
{
player.setVelocityX(-300);
player.play(player.animations['left'], true);
} else if (cursors.right.isDown || keys.D.isDown)
{
player.setVelocityX(300);
player.play(player.animations['right'], true);
} else
{
player.play(player.animations['idle'], true);
}
//let bounds = player.getBounds();
//graphics.clear();
//graphics.lineStyle(1, 0xff0000);
//graphics.strokeRectShape(bounds);
if (player.body.touching.down){
jump();
}
}
function jump(event)
{
if (player.body.touching.down)
{
player.setVelocityY(-600);
}
}
class Player extends Phaser.Physics.Arcade.Sprite
{
animations;
constructor(scene, x, y, spritesheet, animations)
{
super(scene, x, y, spritesheet);
this.animations = animations;
scene.add.existing(this);
scene.physics.add.existing(this);
this.setScale(2);
this.setCollideWorldBounds(true);
this.setGravityY(1000);
}
}
class SeaCreature extends Phaser.Physics.Arcade.Sprite
{
constructor(scene, x, y, spritesheet, animation)
{
super(scene, x, y, spritesheet, animation);
scene.add.existing(this);
scene.physics.add.existing(this);
this.setScale(0.8);
this.play(animation);
}
addTween(scene, xDist, yDist, time)
{
scene.tweens.add({ targets: this, x: xDist, y: yDist, duration: time, ease: 'Sine.easeInOut', repeat: -1, yoyo: true });
}
}
</script>
</body>
</html>