fancy animations! :p

This commit is contained in:
Zutatensuppe 2021-07-18 16:27:31 +02:00
parent d51b40457f
commit 2fb2c45202
9 changed files with 90 additions and 55 deletions

BIN
src/assets/lantern1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 822 B

BIN
src/assets/lantern2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 820 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 710 B

BIN
src/assets/player1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 B

BIN
src/assets/player2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 439 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 454 B

View file

@ -19,6 +19,11 @@ var hud = new PIXI.Container();
var endC = new PIXI.Container(); var endC = new PIXI.Container();
endC.visible = false endC.visible = false
const PLAYER_ANIM_SPEED_MOVING = .1
const PLAYER_ANIM_SPEED_STILL_1 = .01
const PLAYER_ANIM_SPEED_STILL_2 = .1
const resources = PIXI.loader.resources const resources = PIXI.loader.resources
var player, state, objects, maxX, lastKey, PTS_WATER, PTS_COWS, END_TXT var player, state, objects, maxX, lastKey, PTS_WATER, PTS_COWS, END_TXT
@ -26,18 +31,24 @@ var lanterns
var moos var moos
var shader, quad var shader, quad
PIXI.loader PIXI.loader
.add("assets/player.png") .add("assets/player1.png")
.add("assets/player_still.png") .add("assets/player2.png")
.add("assets/player_still_eyesclosed.png") .add("assets/player_still1.png")
.add("assets/player_still2.png")
.add("assets/cow.png") .add("assets/cow.png")
.add("assets/water.png") .add("assets/water.png")
.add("assets/vodkaguy.png") .add("assets/vodkaguy.png")
.add("assets/grass.png") .add("assets/grass.png")
.add("assets/lantern.png") .add("assets/lantern1.png")
.add("assets/lantern2.png")
.load(setup); .load(setup);
function now () {
return new Date().getTime()
}
function setup () { function setup () {
const playerTexture = resources['assets/player_still.png'].texture
const cowTexture = resources['assets/cow.png'].texture const cowTexture = resources['assets/cow.png'].texture
const waterTexture = resources['assets/water.png'].texture const waterTexture = resources['assets/water.png'].texture
@ -63,12 +74,14 @@ function setup () {
hudCow.height = 24 hudCow.height = 24
hudCow.y = 50 hudCow.y = 50
hudCow.x = 20 hudCow.x = 20
hudCow.anchor.set(0,0)
const hudWater = new PIXI.Sprite(resources['assets/water.png'].texture) const hudWater = new PIXI.Sprite(resources['assets/water.png'].texture)
hudWater.width = 24 hudWater.width = 24
hudWater.height = 24 hudWater.height = 24
hudWater.y = 20 hudWater.y = 20
hudWater.x = 20 hudWater.x = 20
hudWater.anchor.set(0,0)
hud.addChild(PTS_COWS) hud.addChild(PTS_COWS)
hud.addChild(hudCow) hud.addChild(hudCow)
hud.addChild(hudWater) hud.addChild(hudWater)
@ -83,7 +96,12 @@ function setup () {
grass.x = 0 grass.x = 0
grass.y = 0 grass.y = 0
player = new PIXI.Sprite(playerTexture);
player = new PIXI.AnimatedSprite([
resources['assets/player_still1.png'].texture,
resources['assets/player_still2.png'].texture,
]);
setPlayerMode('still')
player.anchor.x = 0.5 player.anchor.x = 0.5
player.anchor.y = 0.5 player.anchor.y = 0.5
@ -104,51 +122,51 @@ function setup () {
left.press = () => { left.press = () => {
player.vx = -SPEED player.vx = -SPEED
lastKey = new Date().getTime() lastKey = now()
} }
left.release = () => { left.release = () => {
if (!right.isDown) { if (!right.isDown) {
player.vx = 0 player.vx = 0
} }
lastKey = new Date().getTime() lastKey = now()
} }
right.press = () => { right.press = () => {
player.vx = SPEED player.vx = SPEED
lastKey = new Date().getTime() lastKey = now()
} }
right.release = () => { right.release = () => {
if (!left.isDown) { if (!left.isDown) {
player.vx = 0 player.vx = 0
} }
lastKey = new Date().getTime() lastKey = now()
} }
up.press = () => { up.press = () => {
player.vy = -SPEED player.vy = -SPEED
lastKey = new Date().getTime() lastKey = now()
} }
up.release = () => { up.release = () => {
if (!down.isDown) { if (!down.isDown) {
player.vy = 0 player.vy = 0
} }
lastKey = new Date().getTime() lastKey = now()
} }
down.press = () => { down.press = () => {
player.vy = SPEED player.vy = SPEED
lastKey = new Date().getTime() lastKey = now()
} }
down.release = () => { down.release = () => {
if (!up.isDown) { if (!up.isDown) {
player.vy = 0 player.vy = 0
} }
lastKey = new Date().getTime() lastKey = now()
} }
space.release = () => { space.release = () => {
player.taps.push(true) player.taps.push(true)
lastKey = new Date().getTime() lastKey = now()
} }
f5.release = () => { f5.release = () => {
@ -214,24 +232,29 @@ function setup () {
const lanternDist = availableWidth / (LANTERN_COUNT-1) const lanternDist = availableWidth / (LANTERN_COUNT-1)
lanterns = [] lanterns = []
for (let i = 0; i < LANTERN_COUNT; i++) {
// lantern on bottom
const lantern = new PIXI.Sprite(resources['assets/lantern.png'].texture)
lantern.anchor.set(.5, .5)
lantern.x = TARGET_AREA_WIDTH + (lantern.width / 2) + (i * lanternDist)
lantern.y = HEIGHT - lantern.height /2
lanterns.push(lantern)
container.addChild(lantern)
}
for (let i = 0; i < LANTERN_COUNT; i++) {
// lantern on top
const lantern = new PIXI.Sprite(resources['assets/lantern.png'].texture)
lantern.anchor.set(.5, .5)
lantern.x = TARGET_AREA_WIDTH + (lantern.width / 2) + (i * lanternDist)
lantern.y = lantern.height /2
lanterns.push(lantern) const makeLantern = () => {
container.addChild(lantern) const lantern = new PIXI.AnimatedSprite([
resources['assets/lantern1.png'].texture,
resources['assets/lantern2.png'].texture,
]);
lantern.animationSpeed = .02
lantern.play();
lantern.anchor.set(.5, .5)
return lantern
}
for (let i = 0; i < LANTERN_COUNT; i++) {
const bottom = makeLantern()
bottom.x = TARGET_AREA_WIDTH + (bottom.width / 2) + (i * lanternDist)
bottom.y = HEIGHT - bottom.height /2
const top = makeLantern()
top.x = TARGET_AREA_WIDTH + (top.width / 2) + (i * lanternDist)
top.y = top.height /2
lanterns.push(bottom)
lanterns.push(top)
container.addChild(bottom)
container.addChild(top)
} }
container.addChild(player) container.addChild(player)
@ -351,8 +374,36 @@ function end(delta) {
} }
var time = 0 var time = 0
var blinkStart function setPlayerMode(mode) {
const BLINK_DUR = 250 if (player.mode === mode) {
return
}
player.mode = mode
if (mode === 'still') {
player.textures = [
resources['assets/player_still1.png'].texture,
resources['assets/player_still2.png'].texture,
]
player.gotoAndPlay(0)
player.animationSpeed = PLAYER_ANIM_SPEED_STILL_1
player.onFrameChange = () => {
if (player.currentFrame === 1) {
player.animationSpeed = PLAYER_ANIM_SPEED_STILL_2
} else {
player.animationSpeed = PLAYER_ANIM_SPEED_STILL_1
}
}
} else if (mode === 'moving') {
player.textures = [
resources['assets/player1.png'].texture,
resources['assets/player2.png'].texture,
]
player.gotoAndPlay(0)
player.animationSpeed = PLAYER_ANIM_SPEED_MOVING
player.onFrameChange = () => {}
}
}
function play(delta) { function play(delta) {
player.x += player.vx player.x += player.vx
player.y += player.vy player.y += player.vy
@ -374,28 +425,12 @@ function play(delta) {
player.scale.set(1, 1) player.scale.set(1, 1)
} }
if (player.vy === 0 && player.vx === 0) { if (player.vy === 0 && player.vx === 0) {
player.texture = resources['assets/player_still.png'].texture setPlayerMode('still')
} else { } else {
player.texture = resources['assets/player.png'].texture setPlayerMode('moving')
} }
const _now = now()
let now = new Date().getTime()
if (now - lastKey > IDLE) {
if (blinkStart) {
// is blinking
if (now - blinkStart > BLINK_DUR) {
// should stop blinking
blinkStart = null
lastKey = now
player.texture = resources['assets/player_still.png'].texture
}
} else {
// is not blinking
player.texture = resources['assets/player_still_eyesclosed.png'].texture
blinkStart = now
}
}
container.x = -(player.x + player.width/2 - WIDTH/2) container.x = -(player.x + player.width/2 - WIDTH/2)
@ -466,8 +501,8 @@ function play(delta) {
for (const c of objects) { for (const c of objects) {
if (c.type === 'cow') { if (c.type === 'cow') {
if (c.vrot > 0 && c.ticks > 0) { if (c.vrot > 0 && c.ticks > 0) {
if (c.lastmoo === 0 || (now - c.lastmoo > 500)) { if (c.lastmoo === 0 || (_now - c.lastmoo > 500)) {
c.lastmoo = now c.lastmoo = _now
const moo = new PIXI.Text('!moooo', {fontFamily : 'Arial', fontSize: 14, fill : 0xff0000, align : 'center'}) const moo = new PIXI.Text('!moooo', {fontFamily : 'Arial', fontSize: 14, fill : 0xff0000, align : 'center'})
moo.x = c.x moo.x = c.x
moo.y = c.y moo.y = c.y