package engine

import (
	"sort"
	"zworld/engine/rule"
)

type FWorldOption func(*FWorld)

func FTimeRuleOption(rule *rule.FTimeRule) FWorldOption {
	return func(world *FWorld) {
		world.RuleWrap.TimeRule = rule
		world.RuleList = append(world.RuleList, rule)
	}
}
func FActorRuleOption(rule *rule.FActorRule) FWorldOption {
	return func(world *FWorld) {
		world.RuleWrap.ActorRule = rule
		world.RuleList = append(world.RuleList, rule)
	}
}
func FRenderRuleOption(rule *rule.FRenderRule) FWorldOption {
	return func(world *FWorld) {
		//world.RuleWrap = rule
		world.RuleList = append(world.RuleList, rule)
	}
}
func NewWorld(wrap *FWorldRuleWrap, ops ...FWorldOption) *FWorld {
	w := FWorld{}
	if wrap == nil {
		wrap = &FWorldRuleWrap{}
	}
	w.RuleWrap = wrap
	for _, op := range ops {
		op(&w)
	}
	sort.Slice(w.RuleList, func(i, j int) bool {
		pi, pj := w.RuleList[i].GetPriority(), w.RuleList[j].GetPriority()
		if pi != pj {
			return pi < pj
		}
		return i < j
	})
	return &w
}
