A man armed himself with a hammer, crept into his flatmate's bedroom and smashed in his flatmate's skull while he slept next to his partner, the Crown has argued.
Jiaxin Tu has denied murdering 19-year-old Shane Paul Hawe-Wilson in July last year and has gone on trial at the High Court in Auckland.
In her opening address to the jurors, Crown prosecutor Kirsten Lummis said Mr Tu was obsessed with a young woman and was sick of playing "second fiddle" to her boyfriends.
She said Mr Tu acted in jealousy when he murdered Mr Hawe-Wilson with the hammer.
But Mr Tu's lawyers said their client was so mentally impaired at the time that he was incapable of knowing what he did was morally wrong and that amounted to the legal definition of insanity.
Over the years, Mr Tu showered the woman with gifts, including alcohol and cigarettes.
The relationship became more and more fraught. He later posted a photo of her on his Facebook page and in response to a message from the girl's mother called her "my girl".
Ms Lummis said both families thought the relationship was unhealthy. The young woman did not want a relationship with Mr Tu but at times she and her mother did take advantage of the gifts, as well as borrowing money and Mr Tu's car.
She said Mr Tu wanted the young woman for himself and even hired a hitman to get rid of her boyfriend.
He told people he had offered the hitman $20,000 to do the job and that he wanted to one day marry her.
In early 2015, Mr Tu threatened the woman with a steering wheel lock after he saw her touch her boyfriend's leg. The probation service recognised Mr Tu's obsession with the young woman was unhealthy and they sought a non-association order between the pair.
Ms Lummis said there were also occasions when Mr Tu turned up outside the woman's family home, yelling out that he loved her and that he had killed her boyfriend.
There was also an occasion when Mr Tu broke the windows at the house and she responded by doing the same to Mr Tu's house next door.
It was about this time Mr Tu's mother was diagnosed with terminal cancer and the Tu family wanted him out of the house to save his mother more stress.
The family later trespassed him from the home after a further altercation with his father. But Mr Tu's flatting experience did not go to plan and Ms Lummis said he was eventually kicked out.
About the same time - in June 2015 - the young woman had broken up with her boyfriend and had begun spending time with Shane Hawe-Wilson who had recently moved up to Auckland from Whanganui, Ms Lummis said.
She said the pair started off as friends but the relationship got more intimate and they moved in together to a flat in Panmure.
Mr Tu also ended up moving in, but the flatting situation quickly turned violent. On one occasion Mr Tu poured a drink over the young woman's head. Mr Hawe-Wilson responded by giving Mr Tu a black eye and a swollen cheek.
She said despite the incidents, the young woman and Mr Tu remained friends. She said text messages would show the abusive relationship between her and Mr Tu and there was evidence that at one point she begged Mr Tu to bring her cigarettes and alcohol and offered him sexual favours for money.
There was later also an offer from Mr Tu to pay Mr Hawe-Wilson to return to Whanganui for $5000 so he could be alone with the young woman.
Ms Lummis said the day before the murder the flatmates were drinking and there was more fighting with Mr Hawe-Wilson asking for money from Mr Tu, hitting him and verbally abusing him.
Later that night Mr Tu went for a walk and came across two police officers who had pulled over a car. He told the officers he had been kidnapped and hit in the face.
Ms Lummis said the officers checked Mr Tu's background and found he had mental health issues. They also couldn't see any marks on his face, so recommended he report the incident at the local police station the next day.
Mr Tu returned to the flat where everyone else was asleep. Quietly he searched the house and found a wooden-handled hammer in a kitchen cupboard. Ms Lummis said he then crept into the bedroom and struck Mr Hawe-Wilson two or three times.
She said the death was so sudden Mr Hawe-Wilson hardly moved and the young woman didn't wake "despite the life changing event" that had just happened next to her.
Mr Tu put the hammer back in the cupboard and some time later returned to the bedroom where he got into the couple's bed and tried to remove the woman's jeans. She woke and screamed for help from Mr Hawe-Wilson. Her yelling woke another flatmate who raised the alarm.
She said Mr Tu later told police he had no option but to end Mr Hawe-Wilson's life to save his own.
Mr Tu's lawyer Peter Tomlinson said his client had a history of mental illness.
He said there was no dispute that Mr Tu caused the death of Mr Hawe-Wilson - the question was why.
Mr Tu was so impaired by a mental illness at the time, Mr Tomlinson said, that he didn't know that what he was doing was morally wrong.
Mr Tomlinson said that amounted to a legal defence of insanity.
The trial, before Justice Christian Whata, is expected to last three weeks.