You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
5.3 KiB

2 years ago
class Item extends GameObject{
//Superior class for everything apperaring on the gameboard
//after a period of time, for a period of time
//Settings
settings: Settings.Item
//General
color: Serialized.Color
time: TimeProcess
fadeTime: number
//Position and dimensions
pos: Vector
radius: number
currentRadius: number
constructor(settings: Settings.Item){
super(randomToken())
this.radius = settings.radius
this.fadeTime = settings.fadeTime
this.currentRadius = 1
this.color = {
stroke: settings.color.stroke,
fill: settings.color.fill
}
this.time = new TimeProcess(settings.duration)
}
get centerX(): number{return this.pos.x}
get centerY(): number{return this.pos.y}
get size(): number{return this.currentRadius * 2}
static fromSerialized(item: Serialized.GameObject, settings: Settings.Item, Type: typeof Item): Item{
let i = new Type(settings)
i.pos = new Vector(0, 0)
i.applyValues(item)
return i
}
static fromNew(players: Player[], settings: Settings.Item, Type: typeof Item): Item{
let i = new Type(settings)
i.pos = i.getStartPosition(players)
return i
}
getStartPosition(players: Player[]): Vector{
let left: number,
right: number,
top = this.radius,
bottom = p.height - this.radius
for (let p of players){
if (p.isLeft) left = p.rightSide + this.radius
if (p.isRight) right = p.leftSide - this.radius
if (p.isTop) top = p.downSide + this.radius
if (p.isBottom) bottom = p.upSide - this.radius
}
let x = p.random(left, right),
y = p.random(top, bottom)
return new Vector(x, y)
}
update(): void{
this.time.update()
if (this.time.finished) this.destroy()
if (this.time.timeLeft() < this.fadeTime){
this.currentRadius = p.pow(this.time.timeLeft() / this.fadeTime, 2) * this.radius
} else if (this.time.now < this.fadeTime){
this.currentRadius = p.pow(this.time.now / this.fadeTime, 2) * this.radius
}
}
destroy(): void{}
serialized(): Serialized.Item{
return {
id: this.id,
radius: this.radius,
pos: this.pos.serialized(),
color: this.color,
time: this.time,
currentRadius: this.currentRadius
}
}
}
class TimeProcess{
//Numbers in Milliseconds
max: number
now: number
finished: boolean
constructor(max){
this.max = max
this.now = 0
this.finished = false
}
update(): void{
this.now += 1000 / p.frameRate()
if (this.now >= this.max) this.finished = true
}
progress(): number{
return this.now / this.max
}
timeLeft(): number{
return this.max - this.now
}
serialized(): Serialized.TimeProcess{
return {
max: this.max,
now: this.now,
finished: this.finished
}
}
}
class Boost extends Item{
//Class for the boost objects appearing on the game board
//Inherits following properties from Item:
/*
id: string
pos: Vector
radius: number
color: Serialized.Color
settings: Settings.Item
time: TimeProcess
*/
type: string
positive: boolean
bool: string
effect: Settings.Effect
constructor(settings: Settings.Boost){
super(settings)
this.type = p.random(settings.types)
this.positive = ranBool(2)
this.bool = this.positive ? "positive" : "negative"
this.effect = settings.effect[this.type]
this.color = {
stroke: settings.color[this.bool].stroke,
fill: settings.color[this.bool].fill
}
}
destroy(): void{
let index = game.boosts.indexOf(this)
game.boosts.splice(index, 1)
}
serialized(): Serialized.Boost{
let item = super.serialized() as Serialized.Boost
item.type = this.type
item.bool = this.bool
return item
}
show(): void{
let angle = p.TWO_PI * (1 - this.time.now / this.time.max),
size = this.size
p.push()
p.translate(this.centerX, this.centerY)
p.image(icons[this.type][this.bool], -size / 2, -size / 2, size, size)
p.pop()
}
}
class ActiveBoost{
time: TimeProcess
type: string
positive: boolean
bool: string
player: Player
color: Settings.Color
effect: {[key: string]: number}
reset: {[key: string]: number}
id: string
constructor(player: Player, boost: Boost){
this.id = randomToken()
this.time = new TimeProcess(boost.effect.duration)
this.type = boost.type
this.color = {
stroke: boost.color.stroke,
fill: boost.color.fill
}
this.positive = boost.positive
this.bool = boost.bool
this.player = player
if (boost.positive) this.effect = boost.effect.positive
if (!boost.positive) this.effect = boost.effect.negative
this.reset = boost.effect.standard
}
serialized(): Serialized.ActiveBoost{
return {
time: this.time.serialized(),
type: this.type,
bool: this.bool,
positive: this.positive,
color: this.color,
id: this.id
}
}
update(): void{
this.player.applyValues(this.effect)
this.time.update()
if (this.time.finished){
this.player.applyValues(this.reset)
for (let b of this.player.boosts){
if (b.id === this.id){
let index = this.player.boosts.indexOf(b)
this.player.boosts.splice(index, 1)
break
}
}
}
}
}