const { ccclass, property } = cc. _decorator;
let actionEvent = {
EVENT1 : { name: "工作" , need: { ap: 1 } , cost: { ap: 1 } , award: { coin: 50 } , count: 7 , max_count: 5 } ,
EVENT2 : { name: "练功" , need: { ap: 1 } , cost: { ap: 1 } , award: { attack: 10 } , count: 7 , max_count: 5 } ,
EVENT3 : { name: "买秘籍" , need: { coin: 500 } , cost: { ap: 1 , coin: 1000 } , award: { attack: 100 } , count: 1 , max_count: 1 } ,
EVENT4 : { name: "吃饭" , need: { coin: 20 } , cost: { coin: 20 } , award: { ap: 2 } , count: 2 , max_count: 2 } ,
}
const reActionEvent = {
EVENT4 : { name: "捡到物品还给失主,获得赏赐" , need: { } , rate: 25 , weight: 1 , count: 5 , cost: { } , award: { coin: 100 } } ,
EVENT1 : { name: "采到稀有药草,卖了好价钱" , need: { } , rate: 25 , weight: 1 , count: 5 , cost: { } , award: { coin: 200 } } ,
EVENT2 : { name: "遇到高僧指点功夫" , need: { day: 10 , coin: 100 } , rate: 10 , weight: 1 , count: 1 , cost: { coin: 100 } , award: { attack: 400 } } ,
EVENT6 : { name: "捡到武功秘籍" , need: { day: 5 , coin: 100 } , rate: 10 , weight: 1 , count: 3 , cost: { } , award: { attack: 50 } } ,
EVENT3 : { name: "遇到小偷" , need: { coin: 1 } , rate: 25 , weight: 1 , count: - 2 , cost: { } , award: { coin: 100 } } ,
EVENT5 : { name: "下雨滑了一跤,就医" , need: { coin: 1 } , rate: 25 , weight: 1 , count: 3 , cost: { } , award: { coin: - 100 } } ,
EVENT_END : { name: "没什么特别的事情发生" } ,
}
const endEvent = {
EVENT4 : { name: "富豪" , need: { day: 30 , coin: 6000 } , weight: 97 , count: - 2 , cost: { } , desc: "达成结局:富豪\n\n成为远近闻名的大富豪" , eading: 1 } ,
EVENT5 : { name: "武林高手" , need: { day: 30 , attack: 1200 } , weight: 99 , count: - 2 , cost: { } , desc: "达成结局:武林高手\n\n战胜上任武林盟主,成为\n江湖人闻风丧胆的武林高手" , eading: 1 } ,
EVENT1 : { name: "侠客" , need: { day: 30 , coin: 10 , attack: 600 } , weight: 96 , count: - 2 , cost: { } , desc: "达成结局:侠客\n\n为弱者不平,成为劫富济贫的侠客" , eading: 1 } ,
EVENT6 : { name: "举人" , need: { day: 30 , attack: 1000 } , weight: 98 , count: - 2 , cost: { } , desc: "达成结局:武举人\n\n成功通过武举人考试,成为武官" , eading: 1 } ,
EVENT7 : { name: "店主" , need: { day: 30 , coin: 1500 } , weight: 95 , count: - 2 , cost: { } , desc: "达成结局:店主\n\n开了一家属于自己的小店,为了过上更好的生活努力着" , eading: 1 } ,
EVENT_END : { name: "普通人" , need: { day: 30 } , weight: 82 , count: - 2 , cost: { } , desc: "达成结局:普通人\n\n继续着平凡的生活" , eading: 1 }
}
const deathEvent = {
EVENT3 : { name: "半道崩卒" , need: { day: 10 } , check: { attack: 100 } , weight: 99 , canSkip: false , count: - 2 , cost: { life: 10 } , desc: "达成结局:强盗当道\n\n遇到强盗袭击战败,暴尸荒野" , eading: 1 } ,
EVENT2 : { name: "无力就医" , need: { day: 5 } , check: { coin: 50 } , rate: 25 , weight: 99 , canSkip: false , count: - 2 , cost: { life: 10 } , desc: "达成结局:无力就医\n\n遇到野兽袭击,没钱支付医药费,遗憾离世" , eading: 1 }
}
const descName = {
coin: "总财产" ,
attack: "武力值" ,
life: "生命值" ,
ap: "行动点" ,
max_ap: "最大行动数" ,
}
@ ccclass
export default class TimePanel extends cc . Component {
@ property ( cc. Label)
tipsNode: cc. Label = null ;
@ property ( cc. Label)
nowData: cc. Label = null ;
@ property ( cc. Label)
time: cc. Label = null ;
@ property ( cc. Node)
btnArr: cc. Node[ ] = [ ] ;
nowDay = 1 ;
defaultPlayData = {
ap: 5 ,
max_ap: 5 ,
coin: 0 ,
attack: 5 ,
life: 10 ,
}
playData = JSON . parse ( JSON . stringify ( this . defaultPlayData) )
monsterData = {
attack: 99 ,
life: 1000 ,
}
end = false ;
onLoad ( ) {
let gameData = cc. sys. localStorage. getItem ( "data" ) || null
if ( ! gameData) {
gameData = this . getSaveData ( )
}
if ( gameData) {
const { actionEventAll, playData, nowDay } = gameData;
if ( actionEventAll) {
for ( const key in actionEventAll) {
if ( Object. prototype. hasOwnProperty . call ( actionEventAll, key) ) {
const element1 = actionEvent[ key] ;
if ( ! element1) continue
element1. count = actionEventAll[ key] . count || 0 ;
}
}
}
if ( playData) this . playData = playData;
if ( nowDay) {
this . nowDay = nowDay;
this . checkPlayEvent ( )
}
}
this . tipsNode. string = ""
this . updateNowData ( )
this . updateTime ( )
this . updateAction ( )
for ( let i = 0 ; i < this . btnArr. length; i++ ) {
let eventName = ` EVENT ${ i + 1 } `
let data = actionEvent[ eventName]
if ( ! data) return
const element = this . btnArr[ i] ;
element. getComponentInChildren ( cc. Label) . string = ` ${ data. name} ( ${ data. count} ) `
element. on ( cc. Node. EventType. TOUCH_START , ( ) => {
this . onEventBtn ( { target: element } , ( i + 1 ) + "" )
} )
}
if ( this . nowDay != 1 ) {
this . checkPlayEvent ( )
}
}
getDesc ( data, needN = true , showAdd = false , isAdd = true ) {
let str = ""
for ( const key in data) {
if ( Object. prototype. hasOwnProperty . call ( data, key) ) {
if ( key. split ( "max_" ) . length > 1 ) continue
const element = data[ key] ;
let symble = isAdd ? ` ${ element > 0 ? "+" : "-" } ${ Math. abs ( element) } ` : ` - ${ Math. abs ( element) } `
if ( ! showAdd) symble = ` ${ Math. abs ( element) } `
str += ( ` ${ descName[ key] || key} ${ symble} ` + ( data[ "max_" + key] ? ` / ${ data[ "max_" + key] } ` : "" ) )
str += ( needN ? "\n" : "," )
}
}
if ( needN) str += ( "\n" )
return str
}
updateNowData ( ) {
let str = this . getDesc ( this . playData, true )
this . nowData. string = str
}
updateTime ( ) {
this . time. string = ` 第 ${ this . nowDay} 年 `
}
updateAction ( data? ) {
if ( ! data) {
this . tipsNode. string = ""
return
}
let str = ""
if ( data. name) {
str += ( ` 行动: ${ data. name} \n\n ` )
}
if ( data. cost && Object. keys ( data. cost) . length > 0 ) {
str += ` ${ this . getDesc ( data. cost, true , true , false ) } `
}
if ( data. award && Object. keys ( data. award) . length > 0 ) {
str += ` ${ this . getDesc ( data. award, true , true ) } `
}
this . tipsNode. string = str
}
updateEvent ( data? ) {
if ( ! data) {
this . tipsNode. string = "无事发生"
return
}
let str = ""
if ( data. name) {
str += ( ` ${ data. name} \n\n ` )
}
if ( data. cost && Object. keys ( data. cost) . length > 0 ) {
str += ` ${ this . getDesc ( data. cost, true , true , false ) } `
}
if ( data. award && Object. keys ( data. award) . length > 0 ) {
str += ` ${ this . getDesc ( data. award, true , true ) } `
}
this . tipsNode. string = str
}
onEventBtn ( event, id) {
if ( this . end) return ;
id = Number ( id)
let eventName = ` EVENT ${ id} `
let data = actionEvent[ eventName]
if ( ! data) return
if ( ! this . checkCanCost ( data, true ) ) {
return
}
this . changeData ( data. cost, false ) ;
this . changeData ( data. award) ;
if ( data. count != - 2 ) data. count-- ;
event. target. getComponentInChildren ( cc. Label) . string = ` ${ data. name} ( ${ data. count} ) `
this . updateAction ( data) ;
this . updateNowData ( ) ;
}
setTips ( desc) {
this . tipsNode. string = desc
}
onNextDayBtn ( ) {
if ( this . end) return ;
this . nowDay++ ;
this . cleadDayData ( )
this . updateTime ( )
this . updateNowData ( )
this . updateAction ( )
this . save ( )
this . checkPlayEvent ( )
}
cleadDayData ( ) {
this . playData. ap = this . playData. max_ap;
for ( const key in actionEvent) {
if ( Object. prototype. hasOwnProperty . call ( actionEvent, key) ) {
const data = actionEvent[ key] ;
data. count = data. max_count;
let node = this . btnArr[ Number ( key. split ( "EVENT" ) [ 1 ] ) - 1 ]
if ( ! node) continue ;
node. getComponentInChildren ( cc. Label) . string = ` ${ data. name} ( ${ data. count} ) `
}
}
}
checkCanCost ( data, showTips = false , isCheck = false ) {
if ( data. count != - 2 && data. count <= 0 ) {
data. count == 0
if ( showTips) this . setTips ( data. name + "次数达到每日上限" )
return false
}
let obj = isCheck ? data. check : data. need
for ( const key in obj) {
if ( Object. prototype. hasOwnProperty . call ( obj, key) ) {
const element = obj[ key] ;
if ( key == "day" ) {
if ( this . nowDay < element) return false
continue
}
if ( ! cc. isValid ( this . playData[ key] ) ) continue ;
if ( this . playData[ key] - element < 0 ) {
if ( showTips) this . setTips ( ` ${ descName[ key] } 不足 ` )
return false ;
}
}
}
for ( const key in data. award) {
if ( Object. prototype. hasOwnProperty . call ( data. award, key) ) {
const element = data. award[ key] ;
if ( ! cc. isValid ( this . playData[ key] ) ) continue ;
if ( cc. isValid ( this . playData[ "max_" + key] ) && this . playData[ key] >= this . playData[ "max_" + key] ) {
if ( showTips) this . setTips ( ` ${ descName[ key] } 已满 ` )
return false ;
}
}
}
return true
}
changeData ( data, isAdd = true ) {
for ( const key in data) {
if ( Object. prototype. hasOwnProperty . call ( data, key) ) {
let element = data[ key] ;
if ( cc. isValid ( data[ key + "_max" ] ) ) element = this . randomInt ( data[ key] , data[ key + "_max" ] ) ;
if ( ! cc. isValid ( this . playData[ key] ) ) continue ;
this . playData[ key] = this . playData[ key] + ( isAdd ? 1 : - 1 ) * element
if ( ! cc. isValid ( this . playData[ "min_" + key] ) && this . playData[ key] < 0 ) this . playData[ key] = 0
if ( cc. isValid ( this . playData[ "max_" + key] ) && this . playData[ key] > this . playData[ "max_" + key] ) {
this . playData[ key] = this . playData[ "max_" + key]
}
if ( ! cc. isValid ( this . playData[ "min_" + key] ) && this . playData[ key] < this . playData[ "min_" + key] ) {
this . playData[ "min_" + key] = this . playData[ "min_" + key]
}
}
}
}
getSaveData ( ) {
let data = {
actionEventAll: actionEvent,
playData: this . playData,
nowDay: this . nowDay
}
return data
}
save ( ) {
let data = this . getSaveData ( )
cc. sys. localStorage. setItem ( "data" , data)
return data
}
checkPlayEvent ( ) {
let data = null ;
let eventList = [ deathEvent, endEvent, reActionEvent]
for ( let i = 0 ; i < eventList. length; i++ ) {
const event = eventList[ i] ;
for ( const key in event) {
if ( Object. prototype. hasOwnProperty . call ( event, key) ) {
const element = event[ key] ;
if ( element. canSkip == false ) {
let show = element. rate ? element. rate > Math. random ( ) * 100 : true
if ( ! show) continue ;
if ( ! this . checkCanCost ( element) ) continue ;
if ( this . checkCanCost ( element, false , true ) ) continue ;
data = element;
this . playEventData ( data)
return ;
}
if ( data && ! element. weight) continue
if ( data && element. weight < data. weight) continue
if ( data && element. eading && element. weight == data. weight && Object. keys ( element. need) . length < Object. keys ( data. need) . length) continue
if ( ! this . checkCanCost ( element) ) continue ;
let show = element. rate ? element. rate > Math. random ( ) * 100 : true
if ( ! show) continue ;
data = element;
}
}
}
this . playEventData ( data)
}
playEventData ( data) {
if ( data) {
this . changeData ( data. cost, false ) ;
this . changeData ( data. award) ;
this . updateEvent ( data) ;
if ( data. count != - 2 ) data. count-- ;
this . updateNowData ( ) ;
if ( data. eading) {
this . setTips ( data. desc)
this . end = true
return data
}
return
}
this . tipsNode. string = reActionEvent. EVENT_END . name
}
reStartBtn ( ) {
cc. sys. localStorage. removeItem ( "data" )
this . nowDay = 1 ;
this . end = false ;
this . updateTime ( )
this . playData = JSON . parse ( JSON . stringify ( this . defaultPlayData) )
this . updateNowData ( )
this . updateAction ( )
this . save ( )
}
randomInt ( min, max? ) {
if ( min == max) return min;
if ( max === undefined ) {
max = min[ 1 ] ;
min = min[ 0 ] ;
}
return Math. floor ( Math. random ( ) * ( max - min + 1 ) ) + min;
}
}